【问题标题】:Is it impossible to cancel the transaction when my server is on error当我的服务器出错时是否无法取消交易
【发布时间】:2019-06-28 10:01:30
【问题描述】:

标题:我的服务器出错时是否无法取消交易

我尝试做paypal checkout docs。 这是客户端代码

import React, { Component } from 'react';
import axios from 'axios';
import SmartPaymentButtons, { PayPalSDKWrapper } from 'react-smart-payment-buttons';

class PayPal extends Component {


    render() {
        return (
            <PayPalSDKWrapper clientId="<client_ID>">
                <SmartPaymentButtons
                    createOrder={(data, actions) => {
                        return actions.order.create({
                            purchase_units: [{
                                amount: {
                                    currency_code: "USD",
                                    value: "0.09"
                                }
                            }]
                        });
                    }}
                    onApprove={ (data, actions) => {

                        return actions.order.capture()
                            .then(async (details) => {
                            alert("Transaction completed by " + details.payer.name.given_name);
                            return axios({
                                method: 'POST',
                                url: 'http://localhost:5002/paypal/paypal-transaction-complete',
                                data: {
                                    orderID: data.orderID
                                },
                            }).then(res =>   {
                                return res.data;
                            })
                        });
                    }}
                />
            </PayPalSDKWrapper>
        );
    }
}

export default PayPal;

这是 nodejs 代码

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

const payPalClient = require('../Common/payPalClient');

module.exports = async function handleRequest(req, res) {

  const orderID = req.body.orderID;

  let request = new checkoutNodeJssdk.orders.OrdersGetRequest(orderID);

  let order;
  try {
    order = await payPalClient.client().execute(request);
  } catch (err) {
    console.error(err);
    return res.send(500);
  }

  // sending error on purpose
  if (order.result.purchase_units[0].amount.value !== '220.00') {
    return res.send(400);
  }

  // Save the transaction in your database
  // <...saving orderID code...>

  return res.send(200);
}

我故意在我的服务器上犯了一个错误。所以orderID 没有保存。这是我所期望的。但是paypal付了钱。我没有在我的数据库中获得 orderID,但已经在 paypal 中付款。

有没有办法解决这个问题?

【问题讨论】:

  • 我认为唯一的解决方案是使用退款 API。也许你每天或每小时运行一次 cronjob 来找出这类请求。然后为这些订单运行退款 API。大多数公司通过支持人员这样做

标签: node.js reactjs paypal


【解决方案1】:

一旦您的这行代码运行,付款即被执行且无法“取消”。需要手动或通过 API 退款。

order = await payPalClient.client().execute(request);

看起来您正在使用服务器端实现来批准交易,这很好,但您正在使用客户端 (createOrder) 实现来设置交易。

您应该向服务器发出 POST 请求以创建事务,而不是调用“actions.order.create”,类似于您的 onApprove 方法。从这里,您可以将价格、商品名称、送货地址等所有变量传递给您的服务器,然后在 onApprove 方法中批准付款之前执行您的验证。

您的客户端 createOrder 函数应该与此类似(来自 PayPal 文档):

createOrder: function() {
  return fetch('/my-server/create-paypal-transaction', {
    method: 'post',
    headers: {
      'content-type': 'application/json'
    },
    body: {
       value: 200.00
    }
  }).then(function(res) {
    return res.json();
  }).then(function(data) {
    return data.orderID;
  });
}

来自 PayPal 文档的示例服务器端代码,在第 2 步和第 3 步之间带有您的价值验证 IF 语句:

// 1. Set up your server to make calls to PayPal

// 1a. Import the SDK package
const paypal = require('@paypal/checkout-server-sdk');

// 1b. Import the PayPal SDK client that was created in `Set up Server-Side 
  SDK`.
  /**
   *
   * PayPal HTTP client dependency
   */
  const payPalClient = require('../Common/payPalClient');

// 2. Set up your server to receive a call from the client
module.exports = async function handleRequest(req, res) {

// sending error on purpose
if (req.body.value !== '220.00') {
  return res.send(400);
}

  // 3. Call PayPal to set up a transaction
  const request = new paypal.orders.OrdersCreateRequest();
  request.prefer("return=representation");
  request.requestBody({
    intent: 'CAPTURE',
    purchase_units: [{
      amount: {
        currency_code: 'USD',
        value: '220.00'
      }
    }]
  });

  let order;
  try {
    order = await payPalClient.client().execute(request);
  } catch (err) {

    // 4. Handle any errors from the call
    console.error(err);
    return res.send(500);
  }

  // 5. Return a successful response to the client with the order ID
  res.status(200).json({
    orderID: order.result.id
  });
}

https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/

【讨论】:

  • 我解决了您为createOrdercapture transactiononApprove 编写的链接。非常感谢。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 2011-09-11
  • 2016-11-28
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
相关资源
最近更新 更多