简体   繁体   English

JavaScript 和 PayPal 结帐按钮问题

[英]JavaScript & PayPal Checkout Button Question

I am currently testing out the sandbox for the PayPal checkout integration using Javascript.我目前正在使用 Javascript 测试 PayPal 结账集成的沙箱。

I see how to use the "Sandbox" mode versus using the "Live" mode when testing our buttons out in Javascript.在 Javascript 中测试我们的按钮时,我看到了如何使用“沙盒”模式与“实时”模式。

What I'm asking is how do I set the product's title so that the buyer can see exactly what they're about to purchase prior to clicking the "Pay Now" button to finalize the transaction?我要问的是如何设置产品的标题,以便买家可以在单击“立即付款”按钮完成交易之前准确地看到他们将要购买的商品?

Here is the code that I'm currently working with as shown below:这是我目前正在使用的代码,如下所示:

//HTML GOES HERE TO CONTAIN THE PAYPAL BUTTON
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?&client-id=[MY-CLIENT-ID-GOES-HERE]&merchant-id=[MY-MERCHANT-EMAIL-ADDRESS-GOES-HERE]&currency=USD"></script>


paypal.Buttons({
    style: {
        layout:  'vertical',
        color:   'gold',
        shape:   'pill',
        label:   'buynow'
    },

    // Sets up the transaction when a payment button is clicked
    createOrder: function(data, actions) {
      return actions.order.create({

        purchase_units: [{
          amount: {
            value: 50, // Can reference variables or functions. Example: `value: document.getElementById('...').value`
            currency: 'USD'
          },

        }]
        
      });
      
    },
    
    // Finalize the transaction after payer approval
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(orderData) {
        // Successful capture! For dev/demo purposes:
            console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
            var transaction = orderData.purchase_units[0].payments.captures[0];
            alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');

        // When ready to go live, remove the alert and show a success message within this page. For example:
        // var element = document.getElementById('paypal-button-container');
        // element.innerHTML = '';
        // element.innerHTML = '<h3>Thank you for your payment!</h3>';
        
        actions.redirect('https://SomeWebsiteURLiWillForwardThemTo.com');
      });
    }
  }).render('#paypal-button-container');

In paypals docs you can see the purchase unit object.在 paypals 文档中,您可以看到购买单位对象。 https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit inside that object you can see that you can pass an items array. https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit在该对象中,您可以看到您可以传递一个 items 数组。 This is an array of objects that you can view here https://developer.paypal.com/docs/api/orders/v2/#definition-item .这是一组对象,您可以在此处查看https://developer.paypal.com/docs/api/orders/v2/#definition-item You can define the parameters of each product including the name.您可以定义每个产品的参数,包括名称。 So in your case I think you can try something like the following:因此,在您的情况下,我认为您可以尝试以下操作:

purchase_units: [{
  amount: {
    value: 50, 
    currency: 'USD'
  },
  items: [
    { 
      name: 'Product Name'
    },
  ]
}]

NOTE: you may need to set the price, tax and other things but I think you can just state the product name.注意:您可能需要设置价格、税费和其他内容,但我认为您可以只说明产品名称。 I'm not sure you will have to test it out and see what you need.我不确定您是否必须对其进行测试并查看您需要什么。 Paypals documentation is kind of like following the breadcrumbs so just look through the docs for what you need. Paypals 文档有点像跟踪面包屑,因此只需查看文档即可找到您需要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM