在 PayPal 订单和 PayPal 订阅之间切换

Posted

技术标签:

【中文标题】在 PayPal 订单和 PayPal 订阅之间切换【英文标题】:Toggle between PayPal Order and PayPal subscription 【发布时间】:2021-09-23 00:22:44 【问题描述】:

我一直在使用 paypal 结账,但遇到了困难,我的公司提供的服务可以一次性购买,也可以重复购买,我设计了结账页面并使用下拉菜单,他们可以选择级别服务和频率,好吧,我现在注意到在实施贝宝时,我必须使用“intent=subscription”来进行订阅,但如果我有,则无法下正常订单,如果我同时包含两个脚本,那么我在结帐时收到 500 错误。无论如何我可以在按钮更改时卸载/重新加载我需要的脚本,这就是我必须更改按钮

$(".product-info :input").change(function () 
             if($( ".productselect" ).val() == "basic")
                 $( "#basic" ).show();
                 $( "#plus" ).hide();
                 $( "#premier" ).hide();
             else if ($( ".productselect" ).val() == "plus")
                 $( "#basic" ).hide();
                 $( "#plus" ).show();
                 $( "#premier" ).hide();
             else if ($( ".productselect" ).val() == "premier")
                 $( "#basic" ).hide();
                 $( "#plus" ).hide();
                 $( "#premier" ).show();
             
             if($( ".timingselect" ).val() == "Single")
                paypalsingle();
                $(".totamount").html("$" + $(".productselect").find(':selected').data('cost'));
             else if ($( ".timingselect" ).val() == "Bi")
                 paypalmulti($(".productselect").find(':selected').data('ppbi'));
                $(".totamount").html("$" + $(".productselect").find(':selected').data('costbi'));
             else if ($( ".timingselect" ).val() == "Week")
                 paypalmulti($(".productselect").find(':selected').data('ppweek'));
                $(".totamount").html("$" + $(".productselect").find(':selected').data('costweek'));
             
        );
        function paypalsingle()
            document.getElementById('paypal-button-container').innerHTML = null;
            document.getElementById('paypal-payment-button').innerHTML = null;
            paypal.Buttons(
                style:
                    color:'blue',
                    shape:'pill'
                ,
                createOrder: function (data, actions) 
                    var cost = parseFloat(document.getElementsByClassName('totamount')[0].innerText.replace('$',''));
                    var address = document.getElementsByClassName('basictitle')[0].innerText;

                    return actions.order.create(
                        purchase_units : [
                            amount: 
                                name: '######### Services',
                                description: "Lawn mowing at: " + address,
                                value: cost
                            
                        ]
                    );
                ,
                onApprove: function (data, actions) 
                    return actions.order.capture().then(function (details) 
                        console.log(details);
                        var prod, timing;
                         if($( ".productselect" ).val() == "basic")
                             prod ="basic";
                         else if ($( ".productselect" ).val() == "plus")
                             prod ="plus";
                         else if ($( ".productselect" ).val() == "premier")
                             prod ="premier";
                         
                         if($( ".timingselect" ).val() == "Single")
                             timing ="single";
                         else if ($( ".timingselect" ).val() == "Bi")
                             timing ="bi";
                         else if ($( ".timingselect" ).val() == "Week")
                             timing ="weekly";
                         
                        window.location = "paymentmade.php?UserID=<?php echo $userid ?>&orderID="+data.orderID+"&multi=true&timing="+timing+"&prod="+prod;
                    )
                ,
                onCancel: function (data) 
                    window.location.replace("quote.php?fname=<?php echo $fname ?> &lname=<?php echo $lname ?>&email=<?php echo $email ?>&tel=<?php echo $tel ?>&lot=<?php echo $lot ?>&building=<?php echo $building ?>&lotID=<?php echo $lotid ?>")
                
            ).render('#paypal-payment-button');
        
        function paypalmulti(ppid)
            document.getElementById('paypal-button-container').innerHTML = null;
            document.getElementById('paypal-payment-button').innerHTML = null;
            paypal.Buttons(
                style: 
                    shape: 'pill',
                    color:'blue',
                    layout: 'vertical',
                    label: 'paypal'
                ,
                createSubscription: function(data, actions) 
                    return actions.subscription.create(
                        /* Creates the subscription */
                        plan_id: ppid
                    );
                ,
                onApprove: function (data, actions) 
                    return actions.order.capture().then(function (details) 
                        console.log(details);
                        var prod, timing;
                         if($( ".productselect" ).val() == "basic")
                             prod ="basic";
                         else if ($( ".productselect" ).val() == "plus")
                             prod ="plus";
                         else if ($( ".productselect" ).val() == "premier")
                             prod ="premier";
                         
                         if($( ".timingselect" ).val() == "Single")
                             timing ="single";
                         else if ($( ".timingselect" ).val() == "Bi")
                             timing ="bi";
                         else if ($( ".timingselect" ).val() == "Week")
                             timing ="weekly";
                         
                        window.location = "paymentmade.php?UserID=<?php echo $userid ?>&orderID="+data.orderID+"&multi=true&timing="+timing+"&prod="+prod;
                    )
                ,
                onCancel: function (data) 
                    window.location.replace("quote.php?fname=<?php echo $fname ?> &lname=<?php echo $lname ?>&email=<?php echo $email ?>&tel=<?php echo $tel ?>&lot=<?php echo $lot ?>&building=<?php echo $building ?>&lotID=<?php echo $lotid ?>")
                
            ).render('#paypal-button-container'); // Renders the PayPal button
        

【问题讨论】:

【参考方案1】:

您可以使用如下辅助函数来动态加载/重新加载 SDK:

function loadAsync(url, callback) 
  var s = document.createElement('script');
  s.setAttribute('src', url); s.onload = callback;
  document.head.insertBefore(s, document.head.firstElementChild);


// Usage Example:

loadAsync('https://www.paypal.com/sdk/js?client-id=test&currency=USD', function() 
  paypal.Buttons(

    // Set up the transaction
    createOrder: function(data, actions) 
        return actions.order.create(
            purchase_units: [
                amount: 
                    value: '0.01'
                
            ]
        );
    ,

    // Finalize the transaction
    onApprove: function(data, actions) 
        return actions.order.capture().then(function(details) 
            // Show a success message to the buyer
            alert('Transaction completed by ' + details.payer.name.given_name);
        );
    

  ).render('body');  // Replace with selector of desired container to render in
);

(您的回调当然可以是一个命名函数,例如您的paypalsingle,而不是上面使用示例中的匿名函数)

【讨论】:

以上是关于在 PayPal 订单和 PayPal 订阅之间切换的主要内容,如果未能解决你的问题,请参考以下文章

如何验证 PayPal 订阅

未调用 PayPal 沙盒订阅返回 url

PayPal:是不是可以仅通过订单 ID/详细信息为订单付款

Paypal IPN 初始和后续响应

PayPal 订阅 API - 如何应用折扣

用户批准后如何取消Paypal订单? (API v2)