ebuypay-sdk
v1.0.18
Published
完整调用示例
Downloads
3
Readme
ebuypay
完整调用示例
Omise:
const { ebuyPay } = require('ebuypay-sdk')
let createdOpt = {
appid: '11201f3d0f282314',
type: "Omise", //Omise || icbc
omiseParams: {
publicKey: 'pkey_test_5ncjyug9xqska4qf7fl',
secretKey: 'skey_test_5ncjy0xr7oqiso9kd7m',
payType: 'paynow'
},
publicKey: '-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK3IzsWHh4W4Ee3VW/bFcRC7gp9wgYeK\nNDW2AiP9Vz/acD6jmeWWCXU4o6t2ompHX/I31D5ZmTzdFZDQ7U7w10MCAwEAAQ==\n-----END PUBLIC KEY-----',
body: {
amount: '100', currency: 'sgd', description: '充值描述',
expiredAt:new Date((new Date()).getTime() + 15 * 60 * 1000)
}
}
let refundOpt = {
appid: '11201f3d0f282314',
type: "Omise", //Omise || icbc
omiseParams: {
publicKey: 'pkey_test_5ncjyug9xqska4qf7fl',
secretKey: 'skey_test_5ncjy0xr7oqiso9kd7m',
payType: 'paynow'
},
publicKey: '-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK3IzsWHh4W4Ee3VW/bFcRC7gp9wgYeK\nNDW2AiP9Vz/acD6jmeWWCXU4o6t2ompHX/I31D5ZmTzdFZDQ7U7w10MCAwEAAQ==\n-----END PUBLIC KEY-----',
body: { amount: '100', chargeId: 'charge_253a0c3d06021c34', description: '退款描述' }
}
const a = async () => {
let res = await ebuyPay.createCharge(createdOpt) //返回二维码,chargeId,createdAt等参数
let res = await ebuyPay.createRefund(refundOpt)
}
icbc:
const { ebuyPay } = require('ebuypay-sdk')
let createdOpt = {
appid: '11201f3d0f282314',
type: "icbc", //Omise || icbc
publicKey: '-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK3IzsWHh4W4Ee3VW/bFcRC7gp9wgYeK\nNDW2AiP9Vz/acD6jmeWWCXU4o6t2ompHX/I31D5ZmTzdFZDQ7U7w10MCAwEAAQ==\n-----END PUBLIC KEY-----',
body: { uen: '100', amount: '100', company: '1', description: '充值描述' }
}
const a = async () => {
let res = await ebuyPay.createCharge(createdOpt)
}
1.创建支付
cdk调用示例
let opt = {
appid: '11201f3d0f282314',
type: "Omise", //Omise || icbc
omiseParams: {
publicKey: 'pkey_test_5ncjyug9xqska4qf7fl',
secretKey: 'skey_test_5ncjy0xr7oqiso9kd7m',
payType: 'paynow'
},
publicKey: '-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK3IzsWHh4W4Ee3VW/bFcRC7gp9wgYeK\nNDW2AiP9Vz/acD6jmeWWCXU4o6t2ompHX/I31D5ZmTzdFZDQ7U7w10MCAwEAAQ==\n-----END PUBLIC KEY-----',
body: { amount: '100', currency: 'sgd', description: '描述' }
}
const a = async () => {
let res = await ebuyPay.createCharge(opt)
}
a()
支付流程:
1. 创建预支付订单,订单状态未生效
cdk:
// 整理cdk传入的参数,加密,调用创建订单接口,获取私钥加密后传回的预支付订单数据
// 使用公钥解密数据
let baseUrl = `${key.baseApiUrl}charges/addCharge`
opt.body.type = opt.type
//将opt中的body参数升序排序并加密
let sortStr = objFun.ksortAndToString(opt.body)
//加密,并向服务器发送请求,创建订单
let rpOpt = {
method: 'POST',
uri: baseUrl,
body: {
appid: opt.appid,
data: crypt.encrypt(sortStr, opt.publicKey)
},
json: true
}
//接受服务器返回的加密数据,解码后得到chargeId
let res = await rp(rpOpt)
pay-server:
- 通过appid获取用户的公私钥数据
- 使用私钥机密data
- 创建预支付订单
- 加密订单创建后的结果,返回给调用者
//创建订单接口接受到的body数据格式为:
//{
// appid:"11201f3d0f282314",
// data: '公钥加密后参数字符串'
//}
let payload = {
...ctx.request.body
}
//通过 appid 获取私钥
let cryptRes = await Crypt.findOne({ appid: payload.appid })
if (!cryptRes) {
const err = new Error('没有该appid')
err.code = 409
throw err
}
//解码
let payloadObj = objFun.parseUrlObj(crypt.decrypt(payload.data, cryptRes.privateKey))
//创建预支付订单的字段
let createPayload = {
appid: payload.appid,
type: payloadObj.type,
chargeId: 'charge_' + uuid.getAppId(),
amount: payloadObj.amount,
description: payloadObj.description,
status: '未生效',
expiredAt: new Date(new Date().getTime() + 15 * 60 * 1000) //15min中失效
}
//创建
let res = await Services.addCharge(createPayload)
// 私钥加密
let sortStr = objFun.ksortAndToString(res)
res = crypt.sEncrypt(sortStr, cryptRes.privateKey)
ctx.body = res
2.生成支付二维码
2.1 传入的支付类型为icbc时,由pay-sdk生成支付二维码,并存储至oss云服务
//获取paynow字符串
let QRstring = await getString(opt.uen, opt.amount, res.chargeId, opt.company)
let code = await getCode(QRstring) //生成二维码
let qrcode = await api.toSave(code) //存储至阿里云
2.2 传入的支付类型为Omise时,调用Omise,的到Omise二维码
const createOmiseCharge = async (omise, opt) => {
try {
let source = await omise.sources.create({
'type': opt.omiseParams.payType,
'amount': opt.body.amount,
'currency': opt.body.currency
})
let charge = await omise.charges.create({
'amount': opt.body.amount,
'description': opt.body.description,
'currency': opt.body.currency,
"source": source.id
})
console.log(2222, charge);
return charge
} catch (error) {
return error
}
}
let publicKey = opt.omiseParams.publicKey
let secretKey = opt.omiseParams.secretKey
const omise = Omise({ "publicKey": publicKey, "secretKey": secretKey })
let omiseRes = await createOmiseCharge(omise, opt)
//Omise订单已生成,更新ebuyPay状态及二维码地址,
let qrcode = omiseRes.source.scannable_code.image.download_uri
3.更新预支付订单,状态未支付,修改anotherId
cdk:
//调用更新函数,加需要更新的字段值加密后传给pay-server
//并将得到的结果通过公钥解密
let updateRes = await updateOrderQRcode({
appid: opt.appid,
publicKey: opt.publicKey,
body: { qrcode, chargeId: res.chargeId, anotherId: omiseRes.id }
}
)
let baseUrl = `${key.baseApiUrl}charges/updateOrderQRcode`
//将opt中的body参数升序排序并加密
let sortStr = objFun.ksortAndToString(opt.body)
//加密,并向服务器发送请求,创建订单
let rpOpt = {
method: 'PUT',
uri: baseUrl,
body: {
appid: opt.appid,
data: crypt.encrypt(sortStr, opt.publicKey)
},
json: true
}
let res = await rp(rpOpt)
res = crypt.pDecrypt(res, opt.publicKey) // 公钥解密
res = objFun.parseUrlObj(res)
resolve(res)
pay-server:
let payload = {
...ctx.request.body
}
//通过 appid 获取私钥, 解码
let cryptRes = await Crypt.findOne({ appid: payload.appid })
let payloadObj = objFun.parseUrlObj(crypt.decrypt(payload.data, cryptRes.privateKey))
let chargeId = payloadObj.chargeId
//更新订单
let res = await Services.updateOrderQRcode(chargeId, payloadObj)
// 将结果私钥加密,返回
let sortStr = objFun.ksortAndToString(res)
res = crypt.sEncrypt(sortStr, cryptRes.privateKey)
ctx.body = res
4. 支付订单创建成功,cdk返回二维码地址和chargeId等其他数据,等待二维码被支付
2.支付成功回调
2.1 支付类型为icbc
2.2 支付类型为Omise
成功支付后,Omise向pay-server的接口发送支付成功的请求,接口地址:https://dashboard.ebuy.company/test/api/ebuyPay/pushMsg/OmiseBack ,更据接受的参数,判断是哪一个订单被支付了,更新对应的订单状态,最后请求ebuyPay-sdk使用者配置的回调,通知其订单被支付。
if (payload.key === 'charge.complete') {
// 先判断本地是否有这个订单
let chargeRes = await Charge.findOne({ anotherId: payload.data.id })
let crypt = await Crypt.findOne({ appid: chargeRes.appid })
//先确认第三方回调是否配置
if (!crypt.hookback) {
const err = new Error('请先为该appid配置回调地址,再进行支付')
err.code = 409
throw err
}
if (!chargeRes) {
const err = new Error('没有该订单,请确认订单chargeId')
err.code = 409
throw err
}
//存在则更新订单状态,
let updatePayload = {
status: '已支付',
paidAt: payload.created_at
}
let updateRes = await Charge.updateOne({ anotherId: payload.data.id }, updatePayload, { new: true })
//更新后,向第三方发送回调
let backPayload = {
...updateRes._doc
}
let sortStr = objFun.ksortAndToString(backPayload)
if (crypt.hookback) {
let rpOpt = {
method: 'POST',
uri: crypt.hookback,
body: {
appid: opt.appid,
data: crypt.sEncrypt(sortStr, crypt.privateKey)
},
json: true
}
let cdkBack = await rp(rpOpt)
}
return { msg: 'success' }
}
3.创建退款
cdk调用示例:
let opt = {
appid: '11201f3d0f282314',
type: "Omise", //Omise || icbc
omiseParams: {
publicKey: 'pkey_test_5ncjyug9xqska4qf7fl',
secretKey: 'skey_test_5ncjy0xr7oqiso9kd7m',
payType: 'paynow'
},
publicKey: '-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK3IzsWHh4W4Ee3VW/bFcRC7gp9wgYeK\nNDW2AiP9Vz/acD6jmeWWCXU4o6t2ompHX/I31D5ZmTzdFZDQ7U7w10MCAwEAAQ==\n-----END PUBLIC KEY-----',
body: { amount: '100', chargeId: 'charge_253a0c3d06021c34', description: '退款描述' },
}
const a = async () => {
let res = await ebuyPay.createRefund(opt)
console.log(7878, res);
}
a()
3.1 退款类型为icbc
3.2 退款类型为Omise
1.创建预退款订单
cdk:
//加密cdk传入的参数,调用创建后解密返回值
let baseUrl = `${key.baseApiUrl}charges/addRefund`
//将opt中的body参数升序排序并加密
opt.body.type = opt.type
let sortStr = objFun.ksortAndToString(opt.body)
//加密,并向服务器发送请求,创建订单
let rpOpt = {
method: 'POST',
uri: baseUrl,
body: {
appid: opt.appid,
data: crypt.encrypt(sortStr, opt.publicKey)
},
json: true
}
let res = await rp(rpOpt)
res = crypt.pDecrypt(res, opt.publicKey) // 公钥解密
res = objFun.parseUrlObj(res)
resolve(res)
pay-server:
- 通过appid获取用户的公私钥数据
- 使用私钥机密data
- 创建预退款订单,退款订单状态未退款,并更新支付订单为退款中
- 加密订单创建后的结果,返回给调用者
//
//通过 appid 获取私钥
let cryptRes = await Crypt.findOne({ appid: payload.appid })
if (!cryptRes) {
const err = new Error('没有该appid')
err.code = 409
throw err
}
let payloadObj = objFun.parseUrlObj(crypt.decrypt(payload.data, cryptRes.privateKey))
let createPayload = {
appid: payload.appid,
type: payloadObj.type,
chargeId: payloadObj.chargeId,
amount: payloadObj.amount,
description: payloadObj.description,
status: '未退款'
}
let res = await Services.addRefund(createPayload)
// 私钥加密
let sortStr = objFun.ksortAndToString(res)
res = crypt.sEncrypt(sortStr, cryptRes.privateKey)
ctx.body = res
2.调用Omise退款,并在其回调中更新订单状态
omise.charges.createRefund(refundOrder.anotherId, { 'amount': refundOrder.amount },
async (error, refund) => {
/* Response. */
console.log(66666, error, refund)
if (refund && refund.status == 'closed') {
//退款成功,更新退款订单的状态
result = await updateRefund({ appid: opt.appid, publicKey: opt.publicKey, body: { chargeId: refundOrder.chargeId, anotherId: refundOrder.anotherId } })
}
}
);