Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
ydl-packages
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
许振钊
ydl-packages
Commits
8a3ffbd2
Commit
8a3ffbd2
authored
Sep 15, 2022
by
郑炬波
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 支付统一化
parent
d0df7fdc
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
66 additions
and
25 deletions
+66
-25
notHaveBalance.test.ts
packages/toolkit/__tests__/payment/notHaveBalance.test.ts
+7
-6
units.test.ts
packages/toolkit/__tests__/payment/units.test.ts
+31
-1
package.json
packages/toolkit/package.json
+4
-2
API.ts
packages/toolkit/src/Payment/API.ts
+15
-5
Defined.ts
packages/toolkit/src/Payment/Defined.ts
+6
-11
Payment.ts
packages/toolkit/src/Payment/Payment.ts
+0
-0
Utils.ts
packages/toolkit/src/Utils/Utils.ts
+3
-0
No files found.
packages/toolkit/__tests__/payment/notHaveBalance.test.ts
View file @
8a3ffbd2
...
...
@@ -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,21 @@ beforeAll(async () => {
* 测试账号:15706780002 ydl123456
* 请保证账户余额为0元
*/
Utils
.
setCookie
(
UID
,
'1309
59612
'
)
Utils
.
setCookie
(
ACCESS_TOKEN
,
'
33441f9c4be74a357b1d416d4aa616ea
MjIwNw'
)
globalDatabase
.
payment
=
new
Payment
()
Utils
.
setCookie
(
UID
,
'1309
60056
'
)
Utils
.
setCookie
(
ACCESS_TOKEN
,
'
eccf89e37fb96ef67c4c2cfbe53a05ce
MjIwNw'
)
globalDatabase
.
payment
=
new
Payment
(
{
supportCombination
:
true
}
)
})
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 +81,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'
,
...
...
packages/toolkit/__tests__/payment/units.test.ts
View file @
8a3ffbd2
...
...
@@ -2,6 +2,21 @@ import {describe, expect, test} from '@jest/globals'
import
{
BACK_ORDER_ID
,
BACK_PAY_ID
,
PayChannel
,
PayError
,
PayErrorMessage
,
Payment
,
PayType
}
from
"@/Payment/Payment"
;
describe
(
'测试: 创建实例'
,
()
=>
{
test
(
'supportCombination'
,
()
=>
{
const
payment1
=
new
Payment
({
supportCombination
:
false
})
expect
(
payment1
.
supportCombination
).
toBe
(
false
)
const
payment2
=
new
Payment
({
supportCombination
:
true
})
expect
(
payment2
.
supportCombination
).
toBe
(
true
)
const
payment3
=
new
Payment
()
expect
(
payment3
.
supportCombination
).
toBe
(
true
)
})
test
(
'payChannels'
,
()
=>
{
const
payment
=
new
Payment
({
payChannels
:
[
PayChannel
.
WX_MWEB
,
PayChannel
.
WX_JSAPI
]
...
...
@@ -33,7 +48,7 @@ test('测试订单查询链接', () => {
expect
(
backUrl
).
toContain
(
window
.
location
.
origin
)
})
test
(
'测试价格计算'
,
()
=>
{
test
(
'测试
组合支付
价格计算'
,
()
=>
{
const
payment
=
new
Payment
()
const
goodsPrice
=
0.3
const
balance
=
0.1
...
...
@@ -42,6 +57,15 @@ test('测试价格计算', () => {
expect
(
payBalance
).
toBe
(
0.1
)
})
test
(
'测试非组合支付价格计算'
,
()
=>
{
const
payment
=
new
Payment
({
supportCombination
:
false
})
const
goodsPrice
=
0.3
payment
.
balance
=
0.1
const
{
payAmount
,
payBalance
}
=
payment
.
computeAmount
(
goodsPrice
)
expect
(
payAmount
).
toBe
(
0.3
)
expect
(
payBalance
).
toBe
(
0
)
})
describe
(
'测试支付入参校验'
,
()
=>
{
const
othersParams
=
{
orderId
:
111111111111
,
...
...
@@ -133,3 +157,8 @@ test('测试微信浏览器支付方式', () => {
expect
(
methods
.
length
).
toBe
(
1
)
expect
(
methods
[
0
].
value
).
toBe
(
PayType
.
WECHAT
)
})
test
(
'测试时间接口'
,
async
()
=>
{
const
interval
=
await
Payment
.
PayCheckIntervaL
()
expect
(
interval
).
toBe
(
2000
)
})
\ No newline at end of file
packages/toolkit/package.json
View file @
8a3ffbd2
{
"name"
:
"@ydl-packages/toolkit"
,
"version"
:
"1.0.
1-next.15
"
,
"version"
:
"1.0.
2
"
,
"description"
:
""
,
"main"
:
"./dist/index.umd.js"
,
"scripts"
:
{
...
...
@@ -8,7 +8,9 @@
"test"
:
"npx jest __tests__"
,
"doc"
:
"npx typedoc"
,
"prepublishOnly"
:
"npm run lib"
,
"commit"
:
"git add . && npx cz-customizable"
"commit"
:
"git add . && npx cz-customizable"
,
"async"
:
"npm run lib && yalc push"
,
"watch"
:
"nodemon --ignore dist/ --ignore node_modules/ --watch src/ -C -e ts --debug -x npm run async"
},
"keywords"
:
[
"pay"
,
...
...
packages/toolkit/src/Payment/API.ts
View file @
8a3ffbd2
const
BASE_URL_JAVA
=
process
.
env
.
NODE_ENV
===
'production'
?
'https://api.ydl.com'
:
'https://testapi.ydl.com'
const
BASE_GATEWAY
=
process
.
env
.
NODE_ENV
===
'production'
?
'https://app2.yidianling.com'
:
'https://testapp2.yidianling.com'
let
BASE_URL_JAVA
=
""
;
let
BASE_GATEWAY
=
""
;
const
isDev
=
typeof
window
!==
'undefined'
?
window
.
location
.
hostname
.
indexOf
(
"dev"
)
>
-
1
:
false
;
const
isTest
=
typeof
window
!==
'undefined'
?
window
.
location
.
hostname
.
indexOf
(
"test"
)
>
-
1
:
false
;
if
(
isDev
||
isTest
){
BASE_URL_JAVA
=
'https://testapi.ydl.com'
;
BASE_GATEWAY
=
'https://testapp2.yidianling.com'
}
else
{
BASE_URL_JAVA
=
'https://api.ydl.com'
;
BASE_GATEWAY
=
'https://app2.yidianling.com'
}
/**
* doc: http://47.96.181.183:8806/api/swagger-ui.html#!/finance45v45245controller/doUnityPayUsingPOST
*/
...
...
@@ -19,3 +26,5 @@ export const COMMIT_ORDER = `${BASE_URL_JAVA}/api/consult/consult/commit-order`
export
const
IS_PAY
=
`
${
BASE_URL_JAVA
}
/api/auth/Order/isPay`
export
const
MY_BALANCE
=
`
${
BASE_GATEWAY
}
/v3/uc/mybalance`
export
const
PAY_CHECK_INTERVAL
=
`
${
BASE_URL_JAVA
}
/api/pay/auth/pay/payCheckInterval`
\ No newline at end of file
packages/toolkit/src/Payment/Defined.ts
View file @
8a3ffbd2
...
...
@@ -13,10 +13,11 @@ export type BackInfo = {
orderId
:
number
;
payId
:
number
}
export
type
OrderStateCallBack
=
(
params
:
BackInfo
&
{
isPay
:
boolean
})
=>
void
export
type
OrderStateCallBack
=
(
params
:
BackInfo
&
{
isPay
:
boolean
,
isLimit
?:
boolean
})
=>
void
export
type
PaymentParams
=
{
payChannels
?:
PayChannel
[]
payChannels
?:
PayChannel
[],
supportCombination
?:
boolean
}
export
enum
PayType
{
...
...
@@ -94,11 +95,6 @@ export type ToPayParams = {
*/
redirectUrl
?:
string
;
/**
* 用户在微信公众号或小程序的openId
*/
openId
?:
string
;
appId
?:
string
;
/**
* 支付记录id, 采用余额支付的时候如果前端有此值就带过来
*/
payId
?:
number
;
...
...
@@ -118,21 +114,19 @@ export enum PayErrorMessage {
WECHAT_REDIRECT_URL_MISSING
=
`请填写微信回调地址页面 'redirectUrl`
,
WECHAT_REDIRECT_URL_ENCODE
=
'redirectUrl需要encode'
,
WECHAT_PAY_CHANNEL_MISSING
=
`缺少渠道参数:'WX_MWEB','WX_JSAPI'`
,
WECHAT_APPID_MISSING
=
'缺少微信APPID入参'
,
ALIPAY_QUIT_URL_MISSING
=
`缺少支付失败回调地址:'quitUrl'`
,
ALIPAY_RETURN_URL_MISSING
=
`缺少支付成功回调地址:'returnUrl'`
,
ALIPAY_PAY_CHANNEL_MISSING
=
`缺少持的渠道参数:'ALI_WAP'`
,
WECHAT_JSSDK_PAY_ERROR
=
'微信JSSDK支付失败'
,
WECHAT_H5_PAY_BREAK
=
'微信h5支付中断'
,
ALIPAY_H5_PAY_BREAK
=
'支付宝支付中断'
,
ALIPAY_APPID_MISSING
=
'缺少微信APPID入参'
,
}
export
type
ToPayReturns
=
{
errorMessage
?:
PayErrorMessage
errorType
:
PayError
success
:
boolean
redirect
Url
?:
string
pay
Url
?:
string
}
export
interface
DoUnifiedParams
extends
Omit
<
ToPayParams
,
'totalAmount'
|
'redirectUrl'
>
{
...
...
@@ -191,6 +185,6 @@ export const FULL_PAY_METHODS: PayMethod[] = [
label
:
'花呗支付'
,
value
:
PayType
.
HUABEI_BALANCE
,
icon
:
'https://static.ydlcdn.com/m/order/icon-pay-hb.png'
,
disabled
:
false
disabled
:
false
}
]
\ No newline at end of file
packages/toolkit/src/Payment/Payment.ts
View file @
8a3ffbd2
This diff is collapsed.
Click to expand it.
packages/toolkit/src/Utils/Utils.ts
View file @
8a3ffbd2
...
...
@@ -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
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment