PayPal支付开发之退款处理(node.js---Koa2)

应该也有人跟我一样拿回调的PaymentID去退款的。然后就报错。找不到订单。

第一步:

获取saleid,这个是支付成功之后在execute方法里返回的参数。(payment.transactions[0].related_resources[0].sale.id)

业务层:

async function MerchantsArefund_PayPal(ctx) {
    var saleid = ctx.request.body.PayerID //退款ID
    var data = { amount: { total: ctx.request.body.refundAmount, currency: 'USD' } }
    var whereStr = {
        openId: "nick_chen123456789",
        physicalOrderId: "202006301518123456789",
        Status: 1
    }
    var updateStr = {//修改订单状态
        $set: {
            Status: 4,
            e_expressTime: util.getTime()
        }
    }
    await physicalImpl.RefundDirectly_PayPalImpl(saleid, data, whereStr, updateStr).then(res => {
        ctx.body = res
    }).catch(e => {
        console.log(e);
        ctx.body = { code: 201, msg: "服务器繁忙!" };
        throw e;
    })
}

逻辑层:

async function RefundDirectly_PayPalImpl(saleid, data, whereStr, updateStr) {
    var paypal = require('paypal-rest-sdk');
    require('../configuretion');
    return new Promise((resolv) => {
        paypal.sale.refund(saleid, data, function (error, refund) {
            if (error) {
                console.error(JSON.stringify(error));
                resolv({ code: 201, msg: "退款失败" })
            } else {
                console.log("Refund Sale Response");
                console.log(refund);
                if (refund.state == "completed") {
                    MongoClient.connect(config.Mongose.url, { useNewUrlParser: true }, function (err, db) {
                        if (err) throw err;
                        var dbo = db.db("petshop");
                        dbo.collection("physicalOrders").updateOne(whereStr, updateStr, function (err, res) {
                            if (err) throw err;
                            if (res.result.ok == 1 && res.result.n == 1) {
                                resolv({ code: 200, msg: "退款成功" })
                            } else {
                                resolv({ code: 201, msg: "退款失败" })
                            }
                        })
                    })
                } else {
                    resolv({ code: 201, msg: "退款失败" })
                }
            }
        })
    })
}

 

你可能感兴趣的:(支付,node.js,MongoDB)