2015-10-19 33 views
1

我有下面的表單,我想知道是否有方法,當用戶點擊提交按鈕時,將數據保存到數據庫中,例如$ shipping_price,$ invoice_id和將用戶重定向到paypal付款。PHP和貝寶。當點擊提交時,將數據保存到數據庫並重定向到PayPal

<form action="https://secure.paypal.com/uk/cgi-bin/webscr" method="post" name="paypal" id="paypal"> 
    <!-- Prepopulate the PayPal checkout page with customer details, --> 
    <input type="hidden" name="first_name" value="<?php echo Firstname?>"> 
    <input type="hidden" name="last_name" value="<?php echo Lastname?>"> 
    <input type="hidden" name="email" value="<?php echo Email?>"> 
    <input type="hidden" name="address1" value="<?php echo Address?>"> 
    <input type="hidden" name="address2" value="<?php echo Address2?>"> 
    <input type="hidden" name="city" value="<?php echo City?>"> 
    <input type="hidden" name="zip" value="<?php echo Postcode?>"> 
    <input type="hidden" name="day_phone_a" value=""> 
    <input type="hidden" name="day_phone_b" value="<?php echo Mobile?>"> 

    <!-- We don't need to use _ext-enter anymore to prepopulate pages --> 
    <!-- cmd = _xclick will automatically pre populate pages --> 
    <!-- More information: https://www.x.com/docs/DOC-1332 --> 
    <input type="hidden" name="cmd" value="_xclick" /> 
    <input type="hidden" name="business" value="[email protected]" /> 
    <input type="hidden" name="cbt" value="Return to Your Business Name" /> 
    <input type="hidden" name="currency_code" value="GBP" /> 

    <!-- Allow the customer to enter the desired quantity --> 
    <input type="hidden" name="quantity" value="1" /> 
    <input type="hidden" name="item_name" value="Name of Item" /> 

    <!-- Custom value you want to send and process back in the IPN --> 
    <input type="hidden" name="custom" value="<?php echo session_id().?>" /> 

    <input type="hidden" name="shipping" value="<?php echo $shipping_price; ?>" /> 
    <input type="hidden" name="invoice" value="<?php echo $invoice_id ?>" /> 
    <input type="hidden" name="amount" value="<?php echo $total_order_price; ?>" /> 
    <input type="hidden" name="return" value="http://<?php echo $_SERVER['SERVER_NAME']?>/shop/paypal/thankyou"/> 
    <input type="hidden" name="cancel_return" value="http://<?php echo $_SERVER['SERVER_NAME']?>/shop/paypal/cancelled" /> 

    <!-- Where to send the PayPal IPN to. --> 
    <input type="hidden" name="notify_url" value="http://<?php echo $_SERVER['SERVER_NAME']?>/shop/paypal/process" /> 
</form> 
+0

查看'cURL'。這是可以做到的事情。基本上將數據發佈到您自己的頁面,在數據庫中輸入詳細信息,然後使用curl將數據發佈到PayPal。目前爲止,您的問題太模糊,無法提供完整的答案。 – Twisty

+0

爲什麼隱藏所有的輸入字段? – gidim

+0

@gidim,因爲我只是想向付款人顯示按鈕而不是整個表單。 –

回答

0

有兩種方法可以實現這一點。第一個(更簡單的)版本是在將用戶重定向到PayPal之前通過AJAX將表單提交給您的服務器。這個解決方案的問題在於,如果客戶由於某種原因沒有完成購買,那麼您的數據庫中將會有錯誤的數據,並且您需要找出一種回滾方式。

爲了實現這一點,你可以做這樣的事情:

$(document).ready(function() { 
     $('#submit').click(function(e) { 
      if ($(this).hasClass('sent')) { 
       e.preventDefault(); 
       $(this).removeClass('sent'); 
       $.post("saveToDB.php", { 
        data: $("#paypal").serialize() 
       }, function(success) { 
        if (success) { 
         $('#submit').click(); 
         return true; 
        } 
       }); 
      } else { 
       $('#paypal').submit(); 
       $(this).addClass('sent'); 
      } 
     }); 
    }) 

然後添加「發送」您的提交按鈕的類。

第二種方法是異步處理整個過程,然後僅在過程成功時記錄結果。見下面的例子PayPal API Documentation採取:

HTML:

<div class="row product"> 
     <div class="col-md-4"> 
      <h3>Toy Story Jessie T-Shirt</h3> 
      <p> 
      <form class="ajaxasync" method="POST" action="http://166.78.8.98/cgi-bin/aries.cgi?live=1&returnurl=http://166.78.8.98/cgi-bin/return.htm&cancelurl=http://166.78.8.98/cgi-bin/cancel.htm"> 
       <button id="t2" type="submit" class="checkout" class="paypal-button-hidden"> 
        <img src="https://www.paypalobjects.com/fr_FR/i/btn/btn_xpressCheckout.gif"> 
       </button> 
      </form> 
      </p> 
     </div> 
    </div> 

JS:

window.paypalCheckoutReady = function() { 
    paypal.checkout.setup("6XF3MPZBZV6HU", { 
     environment: 'sandbox', 
     click: function(event) { 
      event.preventDefault(); 

      paypal.checkout.initXO(); 
      $.support.cors = true; 
      $.ajax({ 
       url: "http://166.78.8.98/cgi-bin/aries.cgi?sandbox=1&direct=1&returnurl=http://166.78.8.98/cgi-bin/return.htm&cancelurl=http://166.78.8.98/cgi-bin/cancel.htm", 
       type: "GET", 
       data: '&ajax=1&onlytoken=1', 
       async: true, 
       crossDomain: true, 

       //Load the minibrowser with the redirection url in the success handler 
       success: function (token) { 
        var url = paypal.checkout.urlPrefix +token; 
        //Loading Mini browser with redirect url, true for async AJAX calls 
        paypal.checkout.startFlow(url); 
       }, 
       error: function (responseData, textStatus, errorThrown) { 
        alert("Error in ajax post"+responseData.statusText); 
        //Gracefully Close the minibrowser in case of AJAX errors 
        paypal.checkout.closeFlow(); 
       } 
      }); 
     }, 
     button: ['t1', 't2'] 
    }); 
} 

一旦交易完成用戶返回到return.htm,這是你的登錄交易DB。通過AJAX或只使用PHP(並將return.htm重命名爲return.php)

+0

我可以使用表單將我的客戶轉移到貝寶網站,然後使用ajax但與url到savedata.php將數據保存到savedata.php數據庫? –

+0

當您提交表單時,我認爲您應該首先通過AJAX將數據提交到savedata.php,然後按預期完成表單提交。 – Twisty

+0

@Twisty - 如果由於某種原因調用失敗,數據仍然會被記錄到數據庫 – gidim

0

您可以像這樣完成它,這只是以編程方式提交表單。

<script> 

function sendtopapal() 
{ 
    document.getElementById("paypal").submit(); 
} 
sendtopapal(); 

</script> 
+0

任何解釋爲什麼以及您的答案如何適用於OP? –

0

如上所述,您可以先通過AJAX將數據發佈到您自己的腳本中,然後按照預期完成表單。

<script> 
$("#submit").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "savedata.php", 
     data: $("paypal").serialize() 
    }); 
    return true; 
}); 
</script> 

這應該允許您捕獲正在提交的數據,並將數據一次性發布到PayPal(以及指導用戶)。

相關問題