微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

paypal checkout v2 - 捕获订单 Nodejs

如何解决paypal checkout v2 - 捕获订单 Nodejs

我一直在尝试在我正在开发的网站上实施 PayPal。我可以创建一个订单并将用户重定向到 PayPal 页面进行付款,但是一旦用户确认付款并重定向到成功页面,该页面就会持续加载,直到我收到内部服务器错误并且控制台中没有任何内容。不知道出了什么问题,因为我从文档中做了所有的事情。

这是我的代码

var dotenv = require('dotenv');
var express = require('express');
var router = express.Router({ mergeParams: true });
var async = require('async');


const paypal = require('@paypal/checkout-server-sdk');

// Creating an environment
let clientId = 'SECRET1';
let clientSecret =
    'SECRET2';
// This sample uses SandBoxEnvironment. In production,use LiveEnvironment
let environment = new paypal.core.SandBoxEnvironment(clientId,clientSecret);
let client = new paypal.core.PayPalHttpClient(environment);

router.post('/donate',(req,res) => {
    var selectedamount = req.body.options;

    let request = new paypal.orders.OrdersCreateRequest();
    request.requestBody({
        intent: 'CAPTURE',application_context: {
            return_url: 'https://localhost:3000/success',cancel_url: 'https://localhost:3000/cancel',brand_name: 'Example site',user_action: 'CONTINUE',},purchase_units: [
            {
                reference_id: 'ws_123',description: 'sample product',amount: {
                    currency_code: 'USD',value: selectedamount,],});

    // Call API with your client and get a response for your call
    let createOrder = async function () {
        let response = await client.execute(request);
        
        for (let i = 0; i < response.result.links.length; i++) {
            if (response.result.links[i].rel === 'approve') {
                res.redirect(response.result.links[i].href);
            }
        }
    };
    createOrder();
    console.log('Creating Order...');
    
});

router.get('/success',res) =>{
    
    let captureOrder =  async function(orderId) {
    request = new paypal.orders.OrdersCaptureRequest(orderId);
    request.requestBody({});
    // Call API with your client and get a response for your call
    let response = await client.execute(request);
    console.log(`Response: ${JSON.stringify(response)}`);
    // If call returns body in response,you can get the deserialized version from the result attribute of the response.
    console.log(`Capture: ${JSON.stringify(response.result)}`);
}
        
});

module.exports = router;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。