PHP和Paypal。单击“提交”时,将数据保存到数据库并重定向到paypal

时间:2015-10-19 06:07:55

标签: php paypal

我有下面的表格,我想知道是否有办法,当用户点击提交按钮时,将数据保存到数据库中,如$ shipping_price,$ invoice_id,并将用户重定向到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="paypal@email.com" />
    <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>

3 个答案:

答案 0 :(得分: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,这是您将事务记录到数据库的位置。通过AJAX或仅使用PHP(并将return.htm重命名为return.php)

答案 1 :(得分:0)

你可以这样做,这只是以编程方式提交表单。

<script>

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

</script>

答案 2 :(得分:0)

根据建议,您可以先通过AJAX将数据发布到您自己的脚本中,然后按预期完成表单。

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

这应该允许您捕获正在提交的数据,并将数据一次性发布到PayPal(以及指导用户)。