/**
* 统一下单
* @param ctx
* 商户订单号 - outTradeNo
* 订单总金额 - totalAmount
* 订单标题 - subject
*/
async pcpay(ctx) {
console.log("统一下单:" + JSON.stringify(ctx.request.body));
let outTradeNo = ctx.request.body.outTradeNo;
let totalAmount = ctx.request.body.totalAmount;
let subject = ctx.request.body.subject;
// 填写订单基础信息
const bizContent = {
product_code: "FAST_INSTANT_TRADE_PAY",
out_trade_no: outTradeNo,
total_amount: totalAmount,
subject: subject
}
// 获取支付宝官方支付链接
const formData = new AlipayFormData();
formData.setMethod("get");
formData.addField("bizContent", bizContent);
//支付成功或失败的返回链接
formData.addField("returnUrl", "http://qjzjne.natappfree.cc/api/payment/callback");
let message = "下单成功";
const resultUrl = await alipaySdk.exec(
"alipay.trade.page.pay",
{},
{ formData: formData }
).catch(error => console.error('订单生成失败!', error), message = "订单生成失败!" );
//保存订单数据
const data = {
outTradeNo: outTradeNo,
totalAmount: totalAmount,
subject: subject,
tradeStatus: 'WAIT_BUYER_PAY',
}
await strapi.entityService.create('api::payment.payment', { data: data });
ctx.send({
code: 200,
url: resultUrl,
message: message
});
},
/**
* 支付回调
* @param ctx
* 商户订单号 - outTradeNo
* 平台订单号 - tradeNo
*/
async callback(ctx) {
console.log("支付回调:" + JSON.stringify(ctx.query));
const signRes = alipaySdk.checkNotifySign(ctx.query);
console.log("验签:"+signRes);
const outTradeNo = ctx.query.out_trade_no;
const tradeNo = ctx.query.trade_no;
if(signRes){
let message = "支付回调成功";
const payments = await strapi.entityService.findMany(
'api::payment.payment',
{ filters: { outTradeNo: outTradeNo } }
).catch(error => console.error('支付回调失败!', error), message = "支付回调失败!" );
let payment;
if (payments || payments.length > 0) {
payment = payments[0];
}
const entry = await strapi.entityService.update('api::payment.payment', payment.id, {
data: {
tradeNo: tradeNo,
},
});
ctx.send({
code: 200,
data: entry,
message: message
});
}else{
ctx.send({
code: 200,
message: "验签失败"
});
}
},
/**
* 订单查询
* @param ctx
* 商户订单号 - outTradeNo
* 平台订单号 - tradeNo
*/
async queryOrder(ctx) {
console.log("订单查询:" + JSON.stringify(ctx.request.body));
let outTradeNo = ctx.request.body.outTradeNo;
let tradeNo = ctx.request.body.tradeNo;
let message;
const formData = new AlipayFormData();
formData.setMethod("get");
const bizContent = {
outTradeNo,
tradeNo
};
formData.addField("bizContent", bizContent);
const queryResult = await alipaySdk.pageExec('alipay.trade.query', {
method: 'GET',
bizContent
})
const result = await axios.get(queryResult)
const dataResult = result.data.alipay_trade_query_response;
console.log("交易查询:" + JSON.stringify(dataResult))
if (dataResult.code == 10000) {
switch (dataResult.trade_status) {
case 'WAIT_BUYER_PAY':
message = "交易创建,等待买家付款"
break;
case 'TRADE_FINISHED':
message = "交易结束,不可退款"
break;
case 'TRADE_SUCCESS':
message = "交易支付成功"
break;
case 'TRADE_CLOSED':
message = "未付款交易超时关闭,或支付完成后全额退款";
break;
}
//更新交易状态值
const payments = await strapi.entityService.findMany(
'api::payment.payment',
{ filters: { outTradeNo: outTradeNo } }
);
const entry = await strapi.entityService.update('api::payment.payment', payments[0].id, {
data: {
tradeStatus: dataResult.trade_status,
},
});
ctx.send({
code: 200,
data: entry,
message: message
})
} else if (dataResult.code == 40004) {
ctx.send({
code: 404,
message: "交易不存在"
})
}
}
Demo下载地址:https://download.csdn.net/download/u013772876/88376302