簡體   English   中英

在 PayPal 訂單和 PayPal 訂閱之間切換

[英]Toggle between PayPal Order and PayPal subscription

我一直在使用 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
        }

您可以使用如下所示的輔助函數來動態加載/重新加載 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而不是上面使用示例中的匿名函數)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM