douwen1549 2015-10-23 10:02 采纳率: 100%
浏览 15
已采纳

使用一个按钮发布到数据库和paypal

I'm actually stuck, sending a form post request to multiple action receiver.


What am I trying to do?

I'm trying to send an post request to my database and to paypal at the same time using one button.

After the user submitted the form, I want my PHP script to post those informations to the database before redirecting to paypal. To work with those sent informations.


My actual code:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="item_name" id="item_name" value="Helpful.ninja coins"/>
    <input type="hidden" name="business" value="pr0bpayment@gmail.com">
    <input type="hidden" name="return" value="http://helpful.ninja/?username=payment_complete=1"> 
    <input type="hidden" name="image_url" value="http://helpful.ninja/assets/images/logo_dark.png"> 
    <input type="hidden" name="no_shipping" value="1">
    <input type="hidden" name="amount" class="amountToPay" value="">  
    <input type="hidden" name="no_note" value="1"> 
    <input type="hidden" name="currency_code" value="USD">  
    <input type="hidden" name="bn" value="PP-BuyNowBF">
    <button type="submit" name="submit" alt="PayPal - The safer, easier way to pay online!" class="btn bg-danger-400">Purchase now <i class="icon-paypal position-right"></i></button>
</form>

<?php
    require_once('../handling/database.php');
    require_once('../handling/user.class.php');
    require_once('../handling/purchase.class.php');
    require_once('../handling/config.php');
    $user = new user($_SESSION['Username']);
    $config = new config();
    if(!empty($_POST)){
        $purchase = new purchase($user->username, $user->coins, $_POST['coinValue'], $config->coin_price);
        $purchase->insert_purchase($user->username, $user->email, $_SERVER['REMOTE_ADDR'], $purchase->ordernumber, $purchase->purchase_amount, $purchase->price);
    }
?>

<script type="text/javascript">
    $(".coinValue").bind("change paste keyup", function() {
        $(".amountToPay").val($(this).val() * <?php echo $config->coin_price; ?>);
    });
</script>

So I'm trying to post the following code:

if(!empty($_POST)){
    $purchase = new purchase($user->username, $user->coins, $_POST['coinValue'], $config->coin_price);
    $purchase->insert_purchase($user->username, $user->email, $_SERVER['REMOTE_ADDR'], $purchase->ordernumber, $purchase->purchase_amount, $purchase->price);
}

to the databse before the user gets redirected to paypal.


Does anyone have an idea how to manage this?

  • 写回答

1条回答 默认 最新

  • dongxiao1591 2015-10-23 10:18
    关注

    The best option is probably to use one of the other PayPal apis that is designed for this type of thing, but sticking with the simple paypal button api, you can use the fact that the paypal api endpoint will accept the parameters in a get request as well as a post.

    So you can post the form to your php handler then perform a header redirect to paypal with the data:

    <?php
        //php must be at top of file, before ANY output, in order to use header redirect
        require_once('../handling/database.php');
        require_once('../handling/user.class.php');
        require_once('../handling/purchase.class.php');
        require_once('../handling/config.php');
        $user = new user($_SESSION['Username']);
        $config = new config();
        if(!empty($_POST)){
            $purchase = new purchase($user->username, $user->coins, $_POST['coinValue'], $config->coin_price);
            $purchase->insert_purchase($user->username, $user->email, $_SERVER['REMOTE_ADDR'], $purchase->ordernumber, $purchase->purchase_amount, $purchase->price);
            //redirect to paypal
            header('Location: https://www.paypal.com/cgi-bin/webscr?' . http_build_query($_POST));
            die();
        }
    ?>
    
    <!-- post form to itsself, not paypal-->
    <form action="" method="post">
        <input type="hidden" name="cmd" value="_xclick">
        <input type="hidden" name="item_name" id="item_name" value="Helpful.ninja coins"/>
        <input type="hidden" name="business" value="pr0bpayment@gmail.com">
        <input type="hidden" name="return" value="http://helpful.ninja/?username=payment_complete=1"> 
        <input type="hidden" name="image_url" value="http://helpful.ninja/assets/images/logo_dark.png"> 
        <input type="hidden" name="no_shipping" value="1">
        <input type="hidden" name="amount" class="amountToPay" value="">  
        <input type="hidden" name="no_note" value="1"> 
        <input type="hidden" name="currency_code" value="USD">  
        <input type="hidden" name="bn" value="PP-BuyNowBF">
        <button type="submit" name="submit" alt="PayPal - The safer, easier way to pay online!" class="btn bg-danger-400">Purchase now <i class="icon-paypal position-right"></i></button>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?