简体   繁体   English

PHP按钮,处理一个php文件,然后重定向到另一个页面

[英]PHP button, process in a php file and then redirect to another page

I have a question regarding php button. 我对php按钮有疑问。 I want to make a dynamic buy now button with paypal. 我想使用Paypal创建一个动态的立即购买按钮。 A button that i can pass my custom variables into. 我可以将自定义变量传递到的按钮。 The only way i know how to that is through a paypal form. 我知道的唯一方法是通过贝宝表单。

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="paid@yourdomain.com">
<input type="hidden" name="item_name" value="my product">
<input type="hidden" name="item_number" value="12345">
<input type="hidden" name="amount" value="9.99">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

But the above form's amount value can easily be tampered with. 但是上述形式的金额值很容易被篡改。 So instead of this i want to have just a button in html, and have everything process through a php file. 所以,除了这个,我只想在html中有一个按钮,并通过php文件进行所有处理。 When a user clicked the button, the process of price calculation will happen in the php file, and then redirect the user to paypal, where they can pay for the item. 当用户单击按钮时,价格计算过程将在php文件中进行,然后将用户重定向到paypal,他们可以在其中为商品付款。 This way the price can't be tampered with. 这样,价格就不会被篡改。 Can someone tell me if it's possible to do it that way? 有人可以告诉我是否可以这样做吗? or i have to dynamically encrypt the button using openssl? 还是我必须使用openssl动态加密按钮? or there's another way? 还是有另一种方式?

Instead of posting directly to PayPal, you can collect all the data server side, and use cURL to post the data to PayPal. 您可以收集所有数据服务器端,并使用cURL将数据发布到PayPal,而不是直接发布到PayPal。 The form would look more like: 该表格看起来更像:

<form action="confirm.php" method="post">
    <input type="hidden" name="business" value="paid@yourdomain.com">
    <input type="hidden" name="item_number" value="12345">
    <input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

Then confirm.php would do a handful of things. 然后,confirm.php将做一些事情。 Lookup the price, calculate the total, etc. 查找价格,计算总数,等等。

<?php
$bizName = isset($_POST['business']):$_POST['business']:"";
$itemNumber = isset($_POST['item_number'])?intval($_POST['item_number']):"";

// Lookup details for the Item, purchase, and collect them into an array maybe
$itemDetails = array(
    "cmd" => "_xclick",
    "amount" => $itemPrice, // Get from DB
    "no_note" => 1,
    "currency_code" => "USD",
    "bn" => "PP-BuyNowBF",
    "business" => $bizName,
    "item_name" => $itemName, // Get from DB
    "item_number" => $itemNumber
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.paypal.com/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($itemDetails));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$svr_out = curl_exec ($ch);
curl_close ($ch);
// Do something with $svr_out, maybe display it, or if it's a redirect, redirect the user in turn.
?>

This is untested and is more of a template to get you started. 这未经测试,更像是一个入门模板。 I am sure your site is more complex and you could then build off of this. 我确信您的网站会更复杂,然后您可以以此为基础。 It is just an example of something you could do. 这只是您可以做的事的一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM