首页 > 解决方案 > 错误:使用 intent=capture 来使用客户端捕获 [Paypal Javascript SDK]

问题描述

我正在使用 PayPal javascript SDK 为我的网页创建一个 Paypal 订阅按钮,在使用沙盒帐户测试按钮时出现问题,其中在执行所有订阅付款后出现以下错误:

Error: Use intent=capture to use client-side capture ...

这是我用来显示我的支付 PayPal 按钮的 HTML 脚本

 <script src="https://www.paypal.com/sdk/js?client-id=MY_CLIENT_ID&vault=true&intent=subscription&components=buttons"></script>
 <div id="paypal-button-container"></div>

这是我的 js 文件,它根据付款的操作和状态执行相应的处理程序:

paypal.Buttons({
    style: {
        shape: 'rect',
        color: 'black',
        layout: 'vertical',
        label: 'subscribe'
    },
    createSubscription: function(data, actions) {
      if (discordUser.value === "") {
        alert("Introduce tu usuario de discord");
        return;
      }

      slotDecremented = true;
      alterSlots();
      
      return actions.subscription.create({
        'plan_id': 'P-ID' // here is my plan id
      });
    },
    onApprove: function(data, actions) { 
      return actions.order.capture().then(function(details) {
        sendEmailToken(details.payer.email_address, discordUser.value, data.subscriptionID);
      });
    },
    onCancel: function(data) {
      alterSlots(true);
      slotDecremented = false;
    },
    onError: function(data) {
      if (!slotDecremented) {
        return;
      }

      alterSlots(true);
      slotDecremented = false;
    }
}).render('#paypal-button-container');

alterSlot() 函数使用 AJAX 对我的特定端点进行 API 调用,如果需要,只需提供额外信息

编辑:

sendEmailToken()函数向托管在我的服务器上的 API 发出 AJAX 请求,以便服务器执行正确的操作并确保安全(只是用于了解该部分发生了什么的元数据)

标签: javascripthtmlajaxpaypalpaypal-sandbox

解决方案


    onApprove: function(data, actions) { 
      return actions.order.capture().then(function(details) {
        sendEmailToken(details.payer.email_address, discordUser.value, data.subscriptionID);
      });
    },

actions.order.capture()用于客户端一次性付款按钮。它不属于订阅按钮,因为没有要捕获的顺序。

onApprove请参阅例如 PayPal 订阅文档,该文档在其示例中只有一个基本的成功警报: https ://developer.paypal.com/docs/subscriptions/integrate/#create-the-subscription


由于您的意图似乎是执行服务器端操作,例如在订阅批准后发送电子邮件,因此您应该从您的服务器激活所述订阅,以确保订阅不会在没有正确通知您的服务器的情况下被激活。以下是一些信息:https ://stackoverflow.com/a/65139331/2069605


推荐阅读