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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号