【问题标题】:paypal-rest-sdk "MALFORMED_REQUEST_JSON" on executionpaypal-rest-sdk "MALFORMED_REQUEST_JSON" 执行时
【发布时间】:2020-10-08 15:41:49
【问题描述】:

我正在尝试在我的网站上实施 PayPal 付款,我可以在用户请求后创建付款并向他发送重定向批准 URL,在客户付款后,我使用付款 ID 和买家 ID 在我的后端调用执行,但我收到以下错误

{"response":{"name":"VALIDATION_ERROR","message":"Invalid request - see details","debug_id":"4f3a6da7e0c7d","details":[{"location":"body","issue":"MALFORMED_REQUEST_JSON"}],"links":[],"httpStatusCode":400},"httpStatusCode":400}

我已经尝试了几个小时,我尝试从任何地方复制创建付款 JSON,包括 PayPal API 示例,但仍然没有任何效果。

另外,我想在交易被批准(执行)后将客户端重定向到成功页面,它必须由前台处理?

代码

const paypal = require('paypal-rest-sdk');
const dbService = require('../../services/mongodb-service');
const { ObjectId } = require('mongodb');

const getProducts = async () => {
    const products = await dbService.getCollection('products');
    return await products.find({}).toArray();
}

paypal.configure({
    'mode': 'sandbox',
    'client_id': 'id',
    'client_secret': 'secret'
});

const createPayment = async (productId) => {
    const products = await dbService.getCollection('products');
    const product = await products.findOne({ '_id': ObjectId(productId) })
    if (!product) return Promise.reject('Product not found');
    const payment = {
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"
        },
        "transactions": [{
            "amount": {
                "currency": "USD",
                "total": product.price,
            },
            "description": product.description,
            "payment_options": {
                "allowed_payment_method": "IMMEDIATE_PAY"
            },
            "item_list": {
                "items": [{
                    "name": product.name,
                    "description": product.description,
                    "quantity": 1,
                    "price": product.price,
                    "tax": 0,
                    "sku": product._id,
                    "currency": "USD"
                }]
            }
        }],
        "redirect_urls": {
            "return_url": "http://localhost:3000/purchase-success",
            "cancel_url": "http://localhost:3000/purchase-error"
        }
    }
    const transaction = await _createPay(payment);
    const redirect = transaction.links.find(link => link.method === 'REDIRECT');
    return redirect;
}

const _createPay = (payment) => {
    return new Promise((resolve, reject) => {
        paypal.payment.create(payment, (err, payment) => err ? reject(err) : resolve(payment));
    });
}

const executePayment = async (paymentId, payerId) => {
    try {
        const execute = await _executePay(paymentId, payerId);
        console.log(execute);
        return execute;
    } catch (err) { console.log(JSON.stringify(err)) }
}


const _executePay = (paymentId, payerId) => {
    return new Promise((resolve, reject) => {
        console.log(paymentId, payerId);
        paypal.payment.execute(paymentId, payerId, (error, payment) => {
            return error ? reject(error) : resolve(JSON.stringify(payment));
        })
    })
}


module.exports = {
    createPayment,
    executePayment,
    getProducts
}

【问题讨论】:

    标签: node.js json paypal-rest-sdk


    【解决方案1】:

    应该是

    const _executePay = (paymentId, payerId) => {
        return new Promise((resolve, reject) => {
            console.log(paymentId, payerId);
    
            var payerIdObj = { payer_id: payerId };
    
            paypal.payment.execute(paymentId, payerIdObj, (error, payment) => {
                return error ? reject(error) : resolve(JSON.stringify(payment));
            })
        })
    }
    

    the doc

    【讨论】:

    • 已经有一段时间了,但我试了一下 :) 这似乎是正确的解决方案
    【解决方案2】:

    我可以通过发布请求来解决它,代码:

    const _executePay = async (paymentId, payerId) => {
        const response = await axios.post(`https://api.sandbox.paypal.com/v1/payments/payment/${paymentId}/execute`, { 'payer_id': payerId }, {
            auth: {
                username: CLIENT_ID,
                password: CLIENT_SECRET
            }
        })
        return response;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 2013-09-10
      • 2020-08-18
      • 2016-02-10
      • 1970-01-01
      • 2016-08-11
      • 2019-02-28
      • 2017-10-01
      相关资源
      最近更新 更多