共计 4066 个字符,预计需要花费 11 分钟才能阅读完成。
router
// 支付
router.get(‘/alipay/pay’, initMiddleware, controller.default.alipay.pay);
// 支付成功回调
router.get(‘/alipay/alipayReturn’, initMiddleware, controller.default.alipay.alipayReturn);
// 支付成功异步通知 注意关闭 csrf 验证
router.post(‘/alipay/alipayNotify’, initMiddleware, xmlparseMiddleware, controller.default.alipay.alipayNotify);
config 配置
config/config.default.js
// 支付宝支付的配置
exports.alipayOptions = {
app_id: ‘2018122062672017’,
appPrivKeyFile: “MIIEowIBAAKCAQEAytRAWUJE+t7Xg62PFPpwCxaIBwO942bZX2ehlHbdLSs0i1H3xHlIGTF/0pAYksLuXq8ovyGW263MqvAjt5n97JPjD1ip9eFuIZhZ2wbrOac+MerE+x7agDOBgmJGwdffxbGRRkjz/OtwrIfIVf7TcAm/MPSAuD3RAIkvVXzQO16x6BnBnev1JR+HybeyUssMCk1y6JZ2pZ4H62gGKGvDQcV8NW0q7g4qu2CwQKMVhbnMpG/wRuIla/MOB9MPZiV4CINsxNGya5mmzkXTemjheRl9me6dEZEgKU+tcgTH5Y36faRphbQQ9ATAt3EZQXw4gkoO9vHyEsf7mAAOofQyVQIDAQABAoIBAG+PpUEzKRvPnDyqJuwD/8KphvJMxZIhjOhj6MTvSCJDBGipEh24E8b/qe3YIhv/KftcXo4aXI7CHrPa19px0e/hO9/CBeHfN6M02B+Xw6P3cEcmeWgihU5EhjR/96lBIqzrSRuensz7dwL+wFtEiWmzgrzbjz1HiwC/dBCSUTqFlT7+M+xx8N6w3gQbZ8bpW33XY4KB26C5G8/g6ImEMUbNez8p+24qVztszHWfDHmGJp//Z4g6dgyd2RNrNZdzCyNmlsFRYRXgKa1WbC7qi2ihPUMmYLhlD5OFwZkGbk7bPy+GTYfAK6JjHRjONtdcE06pVFPUMlr7OL96MsABt1UCgYEA8YEPuvbE0Y4i+YlHrYxJ8iJ8WceWL1mNp6QFG6cNQ06+ohPhVVjEdKSFLQIz03aRGQI+E6Kuki5JVwlUeU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nqqHh6ETL1nHVpxiAY2LGer+RWpVYD8v+We+VWsrfJP7bO0xr2pwizldepo8YNYPgcIAIwd7KiveypL1pA0xWgSjUHzrkVh1j/nSnvJgKSdydU/VRcaVt/Mt8wwIDAQAB”
}
exports.alipayBasicParams = {
return_url: ‘http://127.0.0.1:7001/alipay/alipayReturn’, // 支付成功返回地址
notify_url: ‘http://127.0.0.1:7001/alipay/alipayNotify’ // 支付成功异步通知地址
}
支付前
controller
app/controller/default/alipay.js
async pay() {
// this.ctx.body=’ 支付宝支付 ’;
var d = new Date();
const data = {
subject: ‘ 辣条 111’,
out_trade_no: d.getTime().toString(),
total_amount: ‘0.1’
}
var url = await this.service.alipay.doPay(data);
this.ctx.redirect(url);
}
service
app/service/alipay.js
async doPay(orderData,) {
return new Promise((resolve, reject) => {
// 实例化 alipay
const service = new Alipay(this.config.alipayOptions);
// 获取返回的参数
// this.config.alipayBasicParams
service.createPageOrderURL(orderData, this.config.alipayBasicParams)
.then(result => {
console.log(result);
resolve(result.data);
})
})
}
支付后
支付后跳转的地址
controller
app/controller/default/alipay.js
async alipayReturn() {
this.ctx.body = ‘ 支付成功 ’;
// 接收异步通知
}
支付宝支付成功异步通知
引入 koa-xml-body 解析支付成功后的回调 xml 格式数据
app/middleware/xmlparse.js
module.exports = require(‘koa-xml-body’);
controller
支付成功以后更新订单 必须正式上线
app/controller/default/alipay.js
async alipayNotify() {
const params = this.ctx.request.body; // 接收 post 提交的 XML
console.log(params);
var result = await this.service.alipay.alipayNotify(params);
console.log(‘————-‘);
console.log(result);
if (result.code == 0) {
if (params.trade_status == ‘TRADE_SUCCESS’) {
// 更新订单
}
}
// 接收异步通知
}
service
验证异步通知的数据是否正确
app/service/alipay.js
alipayNotify(params) {
// 实例化 alipay
const service = new Alipay(this.config.alipayOptions);
return service.makeNotifyResponse(params);
}
流程
点击 支付宝支付
订单信息
传入订单名称,订单号,总价
const data = {
subject: ‘ 辣条 111’,
out_trade_no: d.getTime().toString(),
total_amount: ‘0.1’
}
点击去支付
支付宝把我们的订单参数,加密封装,之后返回一个 url, 我们重定向,跳转到这个 url, 就是支付的页面
支付宝支付页面
自动跳转到支付页面,url 为支付宝自动生成
扫码支付
支付成功后,跳转的地址
支付成功后,返回的 xml 参数, 解析成 json