# 快速上手 - 安装步骤
友情提醒:
在对接自己的项目之前,建议先下载 vk-uni-pay
的示例项目,先将示例项目跑通之后再来对接你自己的项目。
点击查看示例项目运行教程 (opens new window)
如果你已经跑通了示例项目,则继续往下看!
# 1、 下载安装
- 1、从插件市场安装
vk-uni-pay
插件到你的项目中。插件市场传送门 (opens new window) - 2、在
uniCloud/cloudfunctions/common/vk-uni-pay
目录右键选择管理公共模块依赖
菜单,引入这2个模块uni-config-center
、uni-pay
- 3、在需要引入支付API的云函数右键选择
管理公共模块依赖
菜单,至少引入这4个模块uni-config-center
、uni-id
、uni-pay
、vk-uni-pay
- 4、配置支付参数文件地址:
uniCloud/cloudfunctions/common/uni-config-center/uni-pay/config.js
(没有则新建)(注意这里是config.js
) 查看支付参数
- 5、从支付示例项目中 复制
使用帮助/vk-pay云函数示例代码/service/
目录内的所有文件粘贴到 你的项目的uniCloud/cloudfunctions/vk-pay/service/
目录(没有目录则新建) - 6、上传公共模块
vk-uni-pay
(右键,上传公共模块) - 7、上传云函数
vk-pay
(右键,上传云函数) - 8、完成
# 2、 配置uni-id里的小程序参数
因为小程序支付需要获取用户openid
- 打开文件
cloudfunctions/common/uni-config-center/uni-id/config.json
(没有则新建)(注意这里是config.json
)
- 完整的uni-id配置代码如下
# 注意:获取 openId
使用的 uni-id
接口,故 uni-id
和 uni-pay
都是要配置的
# 微信小程序支付时需要获取 openId
,故需要配置 mp-weixin
# 支付宝小程序支付时需要获取 openId
,故需要配置 mp-alipay
{
"passwordSecret": "passwordSecret-demo",
"tokenSecret": "tokenSecret-demo",
"tokenExpiresIn": 7200,
"tokenExpiresThreshold": 600,
"passwordErrorLimit": 6,
"bindTokenToDevice": false,
"passwordErrorRetryTime": 3600,
"autoSetInviteCode": true,
"forceInviteCode": false,
"app-plus": {
"tokenExpiresIn": 2592000,
"oauth" : {
"weixin" : {
"appid" : "weixin appid",
"appsecret" : "weixin appsecret"
},
"apple": {
"bundleId": "your APP bundleId"
}
}
},
"mp-weixin": {
"oauth" : {
"weixin" : {
"appid" : "weixin appid",
"appsecret" : "weixin appsecret"
}
}
},
"mp-alipay": {
"oauth" : {
"alipay" : {
"appid" : "alipay appid",
"privateKey" : "alipay privateKey"
}
}
},
"service": {
"sms": {
"name": "DCloud",
"codeExpiresIn": 300,
"smsKey": "your sms key",
"smsSecret": "your sms secret"
},
"univerify": {
"appid":"your appid",
"apiKey": "your apiKey",
"apiSecret": "your apiSecret"
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
- 配置完后,还需要到项目根目录的
manifest.json
文件中再次配置一下微信小程序的appid
,如下图所示
# 3、 配置uni-pay支付参数
- 1、打开文件
cloudfunctions/common/uni-config-center/uni-pay/config.js
(没有则新建)
- 复制下方代码到
uni-pay/config.js
const fs = require('fs');
const path = require('path');
module.exports = {
/**
* 统一支付回调地址,格式为 "服务空间SpaceID":"URL化完整地址"
* 这里的本地开发并不是指 http://localhost:8080/ 的地址,而是另外一个服务空间的ULR化地址(如果你本地开发和线上环境共用同一个服务空间则只需要填线上环境的即可)
* 回调的云函数地址,建议填 /http/vk-pay,因为vk-pay云函数已经写好了回调处理的逻辑,否则你需要自己写全部的回调逻辑。
* 其中vk-pay是可以改名的,只需要修改 uniCloud/cloudfunctions/vk-pay/package.json 文件中的 "path": "/http/vk-pay", 把 /http/vk-pay 改成 /http/xxxx 即可(需要重新上传云函数vk-pay)。
*/
"notifyUrl": {
// 本地开发环境,如果你本地开发和线上环境共用同一个服务空间则只需要填线上环境的即可
"a4f90532-ac60-4a43-81c1-a5c4s3fbs66": "https://a4f90532-ac60-4a43-81c1-a5c4s3fbs66.bspapp.com/http/vk-pay",
// 线上正式环境
"b5a9s861-ba20-dca5-56a2-bd3fbsasxz6": "https://b5a9s861-ba20-dca5-56a2-bd3fbsasxz6.bspapp.com/http/vk-pay"
},
/**
* 微信
* 公共参数说明
* appId 微信后台的appId
* secret 微信后台的secret
* mchId 微信支付的商户id
* key 微信支付的api密钥(V2版本)
* pfx 微信支付的p12证书(退款需要)
*/
"wxpay": {
// 微信 - 小程序支付
"mp-weixin": {
"appId": "",
"secret": "",
"mchId": "",
"key": "",
"pfx": fs.readFileSync(__dirname + '/wxpay/wxpay.p12')
},
// 微信 - APP支付
"app-plus": {
"appId": "",
"secret": "",
"mchId": "",
"key": "",
"pfx": fs.readFileSync(__dirname + '/wxpay/wxpay.p12')
},
// 微信 - H5网站二维码支付
"h5": {
"appId": "",
"secret": "",
"mchId": "",
"key": "",
"pfx": fs.readFileSync(__dirname + '/wxpay/wxpay.p12')
},
// 微信 - 公众号支付
"h5-weixin": {
"appId": "wx2ebf03d174875bed",
"secret": "84119e740cc4e98b84e088c5051d05bb",
"mchId": "",
"key": "",
"pfx": fs.readFileSync(__dirname + '/wxpay/wxpay.p12')
},
// 微信 - 手机外部浏览器H5支付
"mweb": {
"appId": "",
"secret": "",
"mchId": "",
"key": "",
"pfx": fs.readFileSync(__dirname + '/wxpay/wxpay.p12'),
// 场景信息,必填
"sceneInfo": {
"h5_info": {
"type": "Wap", // 此值固定Wap
"wap_url": "https://www.xxxxxx.com", // 你的H5首页地址,必须和你发起支付的页面的域名一致。
"wap_name": "网站名称", // 你的H5网站名称
}
}
}
},
/**
* 支付宝(证书记得选java版本)
* 公共参数说明
* mchId 支付宝商户id 2088开头的那个(此参数可以去除)
* appId 支付宝开放平台的应用appId
* secret 支付宝开放平台的应用secret
* privateKey 应用私钥
* alipayPublicCertPath 支付宝公钥证书路径地址 与之对应的 alipayPublicCertContent 为支付宝公钥证书内容(值可以是字符串也可以是Buffer)
* alipayRootCertPath 支付宝根证书路径地址 与之对应的 alipayRootCertContent 为支付宝根证书内容(值可以是字符串也可以是Buffer)
* appCertPath 应用证书路径地址 与之对应的 appCertPathContent 为应用证书内容(值可以是字符串也可以是Buffer)
* sandbox 是否沙箱模式 true 沙箱模式 false 正常模式
*/
"alipay": {
// 支付宝 - 小程序支付配置
"mp-alipay": {
"mchId": "",
"appId": "",
"privateKey": "",
"alipayPublicCertPath": path.join(__dirname, 'alipay/alipayCertPublicKey_RSA2.crt'),
"alipayRootCertPath": path.join(__dirname, 'alipay/alipayRootCert.crt'),
"appCertPath": path.join(__dirname, 'alipay/appCertPublicKey.crt'),
"sandbox": false
},
// 支付宝 - APP支付配置
"app-plus": {
"mchId": "",
"appId": "",
"privateKey": "",
"alipayPublicCertPath": path.join(__dirname, 'alipay/alipayCertPublicKey_RSA2.crt'),
"alipayRootCertPath": path.join(__dirname, 'alipay/alipayRootCert.crt'),
"appCertPath": path.join(__dirname, 'alipay/appCertPublicKey.crt'),
"sandbox": false
},
// 支付宝 - H5支付配置(包含:网站二维码、手机H5,需申请支付宝当面付接口权限)
"h5": {
"mchId": "",
"appId": "",
"privateKey": "",
"alipayPublicCertPath": path.join(__dirname, 'alipay/alipayCertPublicKey_RSA2.crt'),
"alipayRootCertPath": path.join(__dirname, 'alipay/alipayRootCert.crt'),
"appCertPath": path.join(__dirname, 'alipay/appCertPublicKey.crt'),
"sandbox": false
},
// 支付宝 - 转账到支付宝等资金转出接口,其中 appCertSn 和 alipayRootCertSn 通过工具获取
"transfer": {
"mchId": "",
"appId": "",
"privateKey": "",
"appCertSn": "", // 应用证书的序列号
"alipayRootCertSn": "", // 支付宝根证书的序列号
"sandbox": false
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
- 2、打开
cloudfunctions/common/uni-config-center/uni-pay/alipay/
目录(没有则新建) - 3、将
appCertPublicKey.crt
、alipayRootCert.crt
、alipayCertPublicKey_RSA2.crt
3个证书放在此目录下(这里用的是最新最安全的公钥证书模式,且使用的java版的证书) - 4、打开
cloudfunctions/common/uni-config-center/uni-pay/wxpay/
目录(没有则新建) - 5、将
wxpay.p12
1个证书放在此目录下
# notifyUrl 设置异步回调URL
复制服务空间ID

进入服务空间详情

复制vk-pay支付回调函数的URL路径

# 4、支付宝证书生成指南
# 5、特别注意
# 注意一
- 因支付宝转账接口需要用到
appCertSn
和alipayRootCertSn
appCertSn
和alipayRootCertSn
可以通过gitee
导入下方的项目,将你的alipayRootCert.crt
和appCertPublicKey.crt
放在test1/alipay
目录下,本地运行test1云函数获取- gitee地址:https://gitee.com/vk-uni/getAlipayAppCertSn.git (opens new window)
# 这么做是为了不导入第三方npm包(因为从证书中解析序列号需要导入额外一些npm包,增大代码体积)
# 注意二
- 支付宝H5网站扫码支付需签约 支付宝当面付(非PC网站支付)
- 支付宝H5移动支付需签约 支付宝当面付(非移动网站支付)
# 注意三
- 每次修改了支付参数后,需要重新上传公共模块
uni-config-center
# 注意四
- 如果提示找不到 xxx 模块,如
uni-id
模块,则 - 1、在
uniCloud/cloudfunctions/common/vk-uni-pay
目录右键选择管理公共模块依赖
菜单,引入这2个模块uni-config-center
、uni-pay
- 2、在需要引入支付API的云函数右键选择
管理公共模块依赖
菜单,至少引入这4个模块uni-config-center
、uni-id
、uni-pay
、vk-uni-pay
- 3、重新上传公共模块
vk-uni-pay
- 4、重新上传云函数
vk-pay
← 关于vk-uni-pay 配置支付参数文件 →