Commit 1297ea82 by 郑炬波

feat: zf

parent 51a8abe5
...@@ -10,6 +10,7 @@ const globalDatabase: any = { ...@@ -10,6 +10,7 @@ const globalDatabase: any = {
* https://testnewm.ydl.com/consult/#/pages/jieyou/DownOrder?product_id=88220606058062&uid=130959612&accessToken=33441f9c4be74a357b1d416d4aa616eaMjIwNw&ffrom=m * https://testnewm.ydl.com/consult/#/pages/jieyou/DownOrder?product_id=88220606058062&uid=130959612&accessToken=33441f9c4be74a357b1d416d4aa616eaMjIwNw&ffrom=m
*/ */
MOCK_GOODS: { MOCK_GOODS: {
orderId: 94220829005145,
productId: 88220606058062, productId: 88220606058062,
productSpecs: [ productSpecs: [
{ {
...@@ -33,21 +34,24 @@ beforeAll(async () => { ...@@ -33,21 +34,24 @@ beforeAll(async () => {
* 测试账号:15706780002 ydl123456 * 测试账号:15706780002 ydl123456
* 请保证账户余额为0元 * 请保证账户余额为0元
*/ */
Utils.setCookie(UID, '130959612') Utils.setCookie(UID, '130960056')
Utils.setCookie(ACCESS_TOKEN, '33441f9c4be74a357b1d416d4aa616eaMjIwNw') Utils.setCookie(ACCESS_TOKEN, 'eccf89e37fb96ef67c4c2cfbe53a05ceMjIwNw')
globalDatabase.payment = new Payment() globalDatabase.payment = new Payment({
wx_appId: "wx2bc3e1648beb8caa",
zfb_appId: "2021002115687047"
})
}) })
describe('测试无余额支付情况', () => { describe('测试无余额支付情况', () => {
test('test:获取账户余额', async () => { test('test:获取账户余额', async () => {
const balance = await globalDatabase.payment.getBalance() const balance = await globalDatabase.payment.getBalance()
expect(balance).toBe(0) expect(balance).toBe(1.99)
}) })
test('test: 测试余额支付', async () => { test('test: 测试余额支付', async () => {
const res = await globalDatabase.payment.toPay({ const res = await globalDatabase.payment.toPay({
totalAmount: 1, totalAmount: 3,
payType: PayType.BALANCE, payType: PayType.BALANCE,
orderId: globalDatabase.MOCK_GOODS.orderId, orderId: globalDatabase.MOCK_GOODS.orderId,
redirectUrl: window.encodeURIComponent('https://www.baidu.com'), redirectUrl: window.encodeURIComponent('https://www.baidu.com'),
...@@ -80,7 +84,7 @@ describe('测试无余额使用微信支付', () => { ...@@ -80,7 +84,7 @@ describe('测试无余额使用微信支付', () => {
const {orderId, money} = await createOrder() const {orderId, money} = await createOrder()
const res = await globalDatabase.payment.toPay({ const res = await globalDatabase.payment.toPay({
totalAmount: money, totalAmount: money,
payType: PayType.WECHAT_BALANCE, payType: PayType.WECHAT,
orderId, orderId,
redirectUrl: window.encodeURIComponent('https://www.baidu.com'), redirectUrl: window.encodeURIComponent('https://www.baidu.com'),
returnUrl: 'https://www.baidu.com', returnUrl: 'https://www.baidu.com',
......
...@@ -34,12 +34,12 @@ test('测试订单查询链接', () => { ...@@ -34,12 +34,12 @@ test('测试订单查询链接', () => {
}) })
test('测试价格计算', () => { test('测试价格计算', () => {
const payment = new Payment() const payment = new Payment();
const goodsPrice = 0.3 const goodsPrice = 0.01;
const balance = 0.1 const payType = PayType.WECHAT;
const {payAmount, payBalance} = payment.computeAmount(goodsPrice, balance) const {payAmount, payBalance} = payment.computeAmount(goodsPrice, payType)
expect(payAmount).toBe(0.2) expect(payAmount).toBe(0.01)
expect(payBalance).toBe(0.1) expect(payBalance).toBe(0)
}) })
describe('测试支付入参校验', () => { describe('测试支付入参校验', () => {
......
{ {
"name": "@ydl-packages/toolkit", "name": "@ydl-packages/toolkit",
"version": "1.0.1-next.16", "version": "1.0.1-next.20",
"description": "", "description": "",
"main": "./dist/index.umd.js", "main": "./dist/index.umd.js",
"scripts": { "scripts": {
......
const BASE_URL_JAVA = process.env.NODE_ENV === 'production' ? 'https://api.ydl.com' : 'https://testapi.ydl.com' const BASE_URL_JAVA = 'https://testapi.ydl.com';
const BASE_GATEWAY = process.env.NODE_ENV === 'production' ? 'https://app2.yidianling.com' : 'https://testapp2.yidianling.com' const BASE_GATEWAY = 'https://testapp2.yidianling.com'
/** /**
......
...@@ -100,8 +100,6 @@ export type ToPayParams = { ...@@ -100,8 +100,6 @@ export type ToPayParams = {
/** /**
* 支付记录id, 采用余额支付的时候如果前端有此值就带过来 * 支付记录id, 采用余额支付的时候如果前端有此值就带过来
*/ */
wx_appId?: number;
zfb_appId?: number;
payId?: number; payId?: number;
} }
...@@ -133,14 +131,13 @@ export type ToPayReturns = { ...@@ -133,14 +131,13 @@ export type ToPayReturns = {
errorMessage?: PayErrorMessage errorMessage?: PayErrorMessage
errorType: PayError errorType: PayError
success: boolean success: boolean
redirectUrl?: string payUrl?: string
} }
export interface DoUnifiedParams extends Omit<ToPayParams, 'totalAmount' | 'redirectUrl'> { export interface DoUnifiedParams extends Omit<ToPayParams, 'totalAmount' | 'redirectUrl'> {
payChannel?: PayChannel, payChannel?: PayChannel,
payBalance: number, payBalance: number,
payAmount: number, payAmount: number
appId: number|undefined
} }
export interface WechatPayParams { export interface WechatPayParams {
......
...@@ -81,10 +81,10 @@ const requestForJava = defaultRequest ...@@ -81,10 +81,10 @@ const requestForJava = defaultRequest
export class Payment { export class Payment {
balance = 0; balance = 0;
limitPayChannels: PayChannel[] = [] limitPayChannels: PayChannel[] = [];
constructor(paymentParams?: PaymentParams) { constructor(paymentParams?: PaymentParams) {
this.limitPayChannels = paymentParams?.payChannels ?? Object.keys(PayChannel).map(key => key) as PayChannel[] this.limitPayChannels = paymentParams?.payChannels ?? Object.keys(PayChannel).map(key => key) as PayChannel[];
} }
public async getBalance(): Promise<number> { public async getBalance(): Promise<number> {
...@@ -105,7 +105,7 @@ export class Payment { ...@@ -105,7 +105,7 @@ export class Payment {
case PayType.BALANCE: case PayType.BALANCE:
return this.balance > 0 return this.balance > 0
case PayType.WECHAT: case PayType.WECHAT:
return true return !Utils.isAlipay()
case PayType.ALIPAY: case PayType.ALIPAY:
return !Utils.isWechat() return !Utils.isWechat()
case PayType.HUABEI: case PayType.HUABEI:
...@@ -124,21 +124,21 @@ export class Payment { ...@@ -124,21 +124,21 @@ export class Payment {
}) })
} }
computeAmount(totalAmount: number, balance: number): { payAmount: number, payBalance: number } { computeAmount(totalAmount: number, payType: PayType): { payAmount: number, payBalance: number } {
let payBalance = 0, payAmount = 0 let payBalance = 0, payAmount = 0
if (balance === 0) { if(payType == PayType.BALANCE){
payAmount = totalAmount payBalance = totalAmount;
} else if (balance > 0 && balance < totalAmount) { payAmount = 0;
}
else{
payBalance = 0; payBalance = 0;
payAmount = totalAmount; payAmount = totalAmount;
} else {
payBalance = totalAmount
} }
return {payAmount, payBalance} return {payAmount, payBalance}
} }
async toPay(params: ToPayParams): Promise<ToPayReturns> { async toPay(params: ToPayParams): Promise<ToPayReturns> {
const {totalAmount, payType, returnUrl, orderId, openId, wx_appId, zfb_appId, payId} = params const {totalAmount, payType, returnUrl, orderId, redirectUrl, openId, payId} = params
let quitUrl = params.quitUrl let quitUrl = params.quitUrl
// validate start >>> // validate start >>>
switch (payType) { switch (payType) {
...@@ -150,10 +150,16 @@ export class Payment { ...@@ -150,10 +150,16 @@ export class Payment {
case PayType.WECHAT_BALANCE: case PayType.WECHAT_BALANCE:
case PayType.WECHAT: case PayType.WECHAT:
if (this.limitPayChannels.includes(PayChannel.WX_MWEB) && this.limitPayChannels.includes(PayChannel.WX_JSAPI)) { if (this.limitPayChannels.includes(PayChannel.WX_MWEB) && this.limitPayChannels.includes(PayChannel.WX_JSAPI)) {
if (!wx_appId) { if (!redirectUrl) {
return { return {
errorMessage: PayErrorMessage.WECHAT_REDIRECT_URL_MISSING,
success: false,
errorType: PayError.VALIDATE
}
} else if (decodeURIComponent(redirectUrl) === redirectUrl) {
return {
errorMessage: PayErrorMessage.WECHAT_REDIRECT_URL_ENCODE,
success: false, success: false,
errorMessage: PayErrorMessage.WECHAT_APPID_MISSING,
errorType: PayError.VALIDATE errorType: PayError.VALIDATE
} }
} }
...@@ -184,12 +190,6 @@ export class Payment { ...@@ -184,12 +190,6 @@ export class Payment {
success: false, success: false,
errorType: PayError.VALIDATE errorType: PayError.VALIDATE
} }
} else if (!zfb_appId) {
return {
success: false,
errorMessage: PayErrorMessage.ALIPAY_APPID_MISSING,
errorType: PayError.VALIDATE
}
} }
} else { } else {
return { return {
...@@ -203,22 +203,14 @@ export class Payment { ...@@ -203,22 +203,14 @@ export class Payment {
// validate end <<< // validate end <<<
// hack, 后端微信redirectUrl用的quitUrl // hack, 后端微信redirectUrl用的quitUrl
const payChannel = this.getPayChannel(payType) const payChannel = this.getPayChannel(payType)
let appId;
if (payChannel === PayChannel.ALI_WAP) {
appId = zfb_appId;
}
if (payChannel === PayChannel.WX_MWEB || payChannel === PayChannel.WX_JSAPI) {
appId = wx_appId;
}
const doUnifiedParams: DoUnifiedParams = { const doUnifiedParams: DoUnifiedParams = {
returnUrl, returnUrl,
quitUrl, quitUrl,
orderId, orderId,
payType, payType,
openId, openId,
appId,
payId, payId,
...this.computeAmount(totalAmount, this.balance), ...this.computeAmount(totalAmount, payType),
} }
if (payChannel !== null) { if (payChannel !== null) {
doUnifiedParams.payChannel = payChannel doUnifiedParams.payChannel = payChannel
...@@ -228,7 +220,7 @@ export class Payment { ...@@ -228,7 +220,7 @@ export class Payment {
if (params.payType === PayType.BALANCE && res.data) { if (params.payType === PayType.BALANCE && res.data) {
return {success: res.data.result, errorType: PayError.BALANCE} //返回回调地址 return {success: res.data.result, errorType: PayError.BALANCE} //返回回调地址
} else if (payChannel === PayChannel.WX_JSAPI) { } else if (payChannel === PayChannel.WX_JSAPI) {
const isPaySucc = await this.wechatPayJSSDK(res.data) const isPaySucc = await this.wechatPayJSSDK(res.data.content)
return { return {
errorMessage: isPaySucc ? undefined : PayErrorMessage.WECHAT_JSSDK_PAY_ERROR, errorMessage: isPaySucc ? undefined : PayErrorMessage.WECHAT_JSSDK_PAY_ERROR,
success: isPaySucc, success: isPaySucc,
...@@ -236,14 +228,14 @@ export class Payment { ...@@ -236,14 +228,14 @@ export class Payment {
} }
} else if (payChannel === PayChannel.WX_MWEB) { } else if (payChannel === PayChannel.WX_MWEB) {
return { return {
redirectUrl: res.data.content.split("mwebUrl\":\"")[1].split(",")[0], payUrl: res.data.content.link,
success: false, success: false,
errorType: PayError.WECHAT_H5_BREAK, errorType: PayError.WECHAT_H5_BREAK,
errorMessage: PayErrorMessage.WECHAT_H5_PAY_BREAK errorMessage: PayErrorMessage.WECHAT_H5_PAY_BREAK
} }
} else if (payChannel === PayChannel.ALI_WAP) { } else if (payChannel === PayChannel.ALI_WAP) {
return { return {
redirectUrl: res.data.content, payUrl: res.data.content.link,
success: false, success: false,
errorType: PayError.ALIPAY_H5_BREAK, errorType: PayError.ALIPAY_H5_BREAK,
errorMessage: PayErrorMessage.ALIPAY_H5_PAY_BREAK errorMessage: PayErrorMessage.ALIPAY_H5_PAY_BREAK
...@@ -269,7 +261,8 @@ export class Payment { ...@@ -269,7 +261,8 @@ export class Payment {
} }
private wechatPayJSSDK(wechatPayParams: WechatPayParams): Promise<boolean> { private wechatPayJSSDK(wechatPayParams: WechatPayParams): Promise<boolean> {
const {appId, timeStamp, nonceStr, signType, paySign, package: packageAlias} = wechatPayParams const {appId, timeStamp, nonceStr, signType, paySign, package: packageAlias} = wechatPayParams;
return new Promise((resolve) => { return new Promise((resolve) => {
window.WeixinJSBridge.invoke( window.WeixinJSBridge.invoke(
"getBrandWCPayRequest", "getBrandWCPayRequest",
......
...@@ -4,6 +4,9 @@ export class Utils { ...@@ -4,6 +4,9 @@ export class Utils {
static isWechat(): boolean { static isWechat(): boolean {
return /MicroMessenger/i.test(window.navigator.userAgent) return /MicroMessenger/i.test(window.navigator.userAgent)
} }
static isAlipay(): boolean {
return /AlipayClient/i.test(window.navigator.userAgent)
}
static getCookie(key: string): string | null { static getCookie(key: string): string | null {
return Cookies.get(key) || null return Cookies.get(key) || null
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment