首先安裝引入alipay-sdk
,並配置alipay-sdk
npm
alipay-sdk
文檔地址 www.npmjs.com/package/ali…api
alipay-sdk
文檔上並無寫給出app支付實例demo,我這邊從網上各類查資料,琢磨了很久才實現。app
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支付的通知ui
//獲取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'
}
};
複製代碼
支付寶異步參數格式以下,字段名稱請參考支付寶的文檔(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' }
複製代碼