首先安装引入alipay-sdk
,并配置alipay-sdk
alipay-sdk
文档地址 https://www.npmjs.com/package/alipay-sdk
alipay-sdk
文档上并没有写给出app支付实例demo,我这边从网上各种查资料,琢磨了好久才实现。
const AlipaySdk = require('alipay-sdk').default
const aliUtil = require('alipay-sdk/lib/util')
const {
appId,//支付宝的appId
privateKey,//商户私钥
alipayPublicKey,//支付宝公钥
sandbox
} = require('./config').aliPay
const aliPayOptins = {
appId,//支付宝的appId
privateKey,//商户私钥
alipayPublicKey//支付宝公钥
}
// 如果使用沙箱环境需要设置呢网关为 https://openapi.alipaydev.com/gateway.do
const alipaySdk = new AlipaySdk({
...aliPayOptins
})
签名并加密 返回给app客户端,app客户端拿到这串字符调用支付宝就可以了
exports.signOrder = data => {
const params = {
bizContent: {
out_trade_no: '12312312321212',
subject: encodeURIComponent('苹果电脑'),
total_amount: 0.2,
body: encodeURIComponent('Mac Pro1'),
product_code: 'QUICK_MSECURITY_PAY',
notifyUrl:'http://.../api/orderNotify'
}
}
const config = alipaySdk.config
const signData = aliUtil.sign('alipay.trade.app.pay', params, config)
const {
url,
execParams
} = alipaySdk.formatUrl('', signData)
const orderInfo = (url + '&biz_content=' + encodeURIComponent(execParams.biz_content)).substr(1)
return orderInfo
}
获取app支付的通知
//获取App支付的同步通知, POST, https://docs.open.alipay.com/204/105301
exports.confirmOrder = (dataString) => {
if (alipaySdk.checkResponseSign(dataString, 'alipay_trade_app_pay_response')) {
return dataString;
} else {
return {
error: true,
message: 'Payment not verified.'
}
}
}
//获取支付宝的异步通知
exports.notifyOrder = dataObject => {
if (alipaySdk.checkNotifySign(dataObject)) {
return 'success'
} else {
return 'false'
}
};
支付宝异步参数格式如下,字段名称请参考支付宝的文档(https://docs.open.alipay.com/204/105465/)
{ gmt_create: '2019-09-19 13:17:36',
charset: 'utf-8',
seller_email: 'hfpwqu7216@sandbox.com',
subject: '苹果电脑',
sign:
'SgJYDQjp3IFqiYa0Bo1oFRICIRV/JPS3ICAMVyAq9xr7QuCbd0M3zudyokr8guOi+S9Fgg2sOOM1X91XV48lQRj4sPD9c5seW/MKD1+X7w6/OCs9Ft+WRooAfN3lw4hRj3uM1hkW4r0erF25fNhyureM3G2HjEgO6j+8F53OPABWOARUtMu+rHcZeC1FQng1JFcSF/mPYVDO
O2B4lnqYw4Up3CbuBt9B0a+iNaD35YKOJQjT5ZbM0IGUswixqLG1lUvWFM640thtewMzPe87Oo6Tra5jVyD69Y1eKwPkVgo/j6TL6FnXXobJgZysOpBMzcB/y2NqWOUUj1mYLSoVBw==',
body: 'Mac Pro1',
buyer_id: '2088102179638911',
invoice_amount: '0.20',
notify_id: '2019091900222131739038911000543202',
fund_bill_list: '[{"amount":"0.20","fundChannel":"ALIPAYACCOUNT"}]',
notify_type: 'trade_status_sync',
trade_status: 'TRADE_SUCCESS',
receipt_amount: '0.20',
app_id: '2016101200667922',
buyer_pay_amount: '0.20',
sign_type: 'RSA2',
seller_id: '2088102179218511',
gmt_payment: '2019-09-19 13:17:38',
notify_time: '2019-09-19 13:17:39',
version: '1.0',
out_trade_no: '1568870195707',
total_amount: '0.20',
trade_no: '2019091922001438911000052152',
auth_app_id: '2016101200667922',
buyer_logon_id: 'mvu***@sandbox.com',
point_amount: '0.00' }
网友评论