Commit 1297ea82 by 郑炬波

feat: zf

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