Paypal BuyNow 按钮快速结帐

发布于 2024-12-05 17:49:53 字数 2216 浏览 0 评论 0原文

我想使用 Express Checkout 和 notification_url 参数将 Paypal 集成到我的网站中,这将通知我的网站,因此我可以采取行动。

我生成了 buynowbuttons。示例立即购买按钮是:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
                <input name="notify_url" value='http://localhost:9000/Home/ProcessPayment' type="hidden"/>
                <input type="hidden" name="cmd" value="_xclick">
                <input type="hidden" name="business" value="[email protected]">
                <input type="hidden" name="lc" value="US">
                <input type="hidden" name="item_name" value="Basic">
                <input type="hidden" name="amount" value="24.99">
                <input type="hidden" name="currency_code" value="USD">
                <input type="hidden" name="button_subtype" value="services">
                <input type="hidden" name="no_note" value="0">
                <input type="hidden" name="tax_rate" value="0.000">
                <input type="hidden" name="shipping" value="0.00">
                <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
                <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif"
                    border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                <img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif"
                    width="1" height="1">
                </form>

我添加了 这样它就可以通知我的控制器中用于调试目的的操作方法。但是,它不起作用。

我被重定向到 Paypal 帐户:

https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_flow&SESSION=somesession&dispatch=somedispatch

但是一旦付款完成,它就永远不会到达主控制器中 ProcessPayment 操作中的断点。我有什么遗漏的吗?

提前致谢:)

更新:- 我有 hidemyass pro 帐户,然后我所要做的就是在 IIS 中设置我的网站,然后我的 notification_url 就可以正常工作了;)。

I want to integrate Paypal in my site using Express Checkout and notify_url parameter which would notify my site and accordinly I can take actions.

I generated buynowbuttons. Sample buy now button is:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
                <input name="notify_url" value='http://localhost:9000/Home/ProcessPayment' type="hidden"/>
                <input type="hidden" name="cmd" value="_xclick">
                <input type="hidden" name="business" value="[email protected]">
                <input type="hidden" name="lc" value="US">
                <input type="hidden" name="item_name" value="Basic">
                <input type="hidden" name="amount" value="24.99">
                <input type="hidden" name="currency_code" value="USD">
                <input type="hidden" name="button_subtype" value="services">
                <input type="hidden" name="no_note" value="0">
                <input type="hidden" name="tax_rate" value="0.000">
                <input type="hidden" name="shipping" value="0.00">
                <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
                <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif"
                    border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                <img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif"
                    width="1" height="1">
                </form>

I added <input name="notify_url" value='http://localhost:9000/Home/ProcessPayment' type="hidden"/> so it can notify my action method in my controller for debugging purpose. However, it is not working.

I get redirected to Paypal account :

https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_flow&SESSION=somesession&dispatch=somedispatch

But once the payment is completed it never hits my breakpoint in ProcessPayment action in Home controller. Is there something that I am missing?

Thanks in advance :)

UPDATE:- I had hidemyass pro account and all I had then was to setup my website in IIS and then my notify_url worked like a charm ;).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乄_柒ぐ汐 2024-12-12 17:49:53

通常情况下,在这种情况下你的断点永远不会被命中,这是正常的,因为:

如果你没有“直接”外部IP地址(并且你至少有一个路由器)或者你已经配置了要执行的所有内容正确的 NAT 端口转发,PayPal IPN 将转到:

1) Nirvana,因为您没有正确配置 IP 地址,

或者

2) 到您实际配置的 IPN 的 IP 地址的测试服务器/生产服务器。

更新:

有 2 个解决方案:

  1. 设置 PayPal IPN 侦听器 到您的外部地址(可能是您的家庭路由器或公司路由器)并配置 NAT 或要求您的管理员执行此操作

  1. 使用 远程调试器 在您的实时/测试系统上,附加到 iis 进程并命中断点。

我个人倾向于解决方案 1,因为我从来没有让远程调试器像我预期的那样工作。

In usual cases your breakpoint will never be hit in this case, and that's normal, because:

If you don't have a "direct" external IP-address ( and you will AT LEAST have a router ) or you have configured everything to perform correct NAT port forwarding, the PayPal IPN will go either to:

1) Nirvana, because you have not configured the IP address correctly

OR

2) To your Testserver/Productionserver which IP-Adress for IPNs you actually configured.

UPDATE:

There are 2 Solutions:

  1. Set the PayPal IPN listener to your external address ( maybe your homerouter or your companyrouter ) and configure the NAT or ask your admin to do it

OR

  1. Use the Remote debugger on your live/test system, to attach to the iis-process and hit the breakpoint.

I personally would tend to solution 1., because i never got the remote debugger to work like i expected..

天涯离梦残月幽梦 2024-12-12 17:49:53

这是因为他们创建自己的 HTTPRequest 到该 URL,并且它不是您会话的一部分。

他们会尝试从他们的服务器联系您的 localhost:9000,但这个 URL 对他们来说并不存在。

您需要添加一些日志记录来检查他们与此 URL 通信时发生的情况,例如记录他们已到达该页面以及代码中发生的情况。然后将此页面“实时”放置在某个地方,以便 PayPal 进行对话。

It is because they create their own HTTPRequest to that URL, and it is not part of your session.

They will try to contact your localhost:9000 from their server, and this URL doesnt exist to them.

You will need to put in some logging to check what happens when they talk to this URL, e.g. log that they have reached the page, and what happened in the code. Then put this page "Live" somewhere for PayPal to talk to.

掩饰不了的爱 2024-12-12 17:49:53

请参阅下面的一些评论,我相信这些评论将帮助您解决此问题:

1 - notification_url 必须是可以从 PayPal 访问的 url。它应该类似于 http://www.example.com/path/to/your/脚本。这是 PayPal 将发布交易结果的 URL。

2 - 当交易从 PayPal 返回时,您将需要一种方法来识别该交易。我这样做的方法是包含一个字段,

<input type="hidden" name="invoice" value=$uniquevaluecreatedbyyourapplication>

Paypal 会在响应中发送回该字段。

就我而言,我将事务添加到 MySQL 表中,并将唯一记录 ID 作为发票编号传递。当 PayPal 发回响应时,我根据数据库检查发票号码以识别交易。

我希望这有帮助。如果没有,请重新发帖。

更新以包含有关 IPN 消息的更多详细信息

基本上,当交易完成时,PayPal 将发布到您在 notification_url 字段中传递的脚本。

Paypal 将在该帖子中附加一个令牌 (tx)。您的脚本将使用如下内容进行调用:

http://www.example.com/path/to/your/listener?tx=1234567890099r48&... (PayPal adds also other transaction fields here).

但是,您不应依赖 PayPal 添加到第一条消息的交易字段,因为您并不真正知道它来自 PayPal。

因此,为了确认该消息来自 PayPal,您需要向 PayPal 回发一条消息,其中包括 PayPal 发送给您的 tx_token;只有您知道的 auth_token (您从 PayPal 卖家帐户中获取此令牌);以及一个 cmd 变量,告诉 PayPal 您正在验证交易。

一旦 PayPal 从您的侦听器脚本收到此帖子,它将回复一条帖子,确认交易已完成以及所有详细信息。您可以信赖第二篇文章,因为它是您使用唯一的 auth_token 向 Paypal 发出请求的结果。

PayPal 将向您发送一系列包含交易信息的字段,其中包括确认付款是否成功或处于待处理状态的字段。根据客户支付方式的不同,付款可能会在一段时间内保持待处理状态,直至结清。付款结清后,PayPal 将自动向您发送另一条消息。因此,您应该配置侦听器来处理第二条消息。 PayPal 建议您在付款结清之前不要发货。

为了帮助您入门,请参阅下面我用于处理 PayPal 响应的侦听器脚本的代码。您需要根据自己的需要进行调整,但它对我有用。

$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "enter your own authorization token";
$req .= "&tx=$tx_token&at=$auth_token";

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

//$fp = fsockopen ('http://www.sandbox.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// replace www.sandbox.paypal.com with www.paypal.com when you go live
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

if (!$fp)
{
    // HTTP ERROR
}
else
{
    fputs ($fp, $header . $req);
    // read the body data
    $res = '';
    $headerdone = false;
    while (!feof($fp))
    {
        $line = fgets ($fp, 1024);
        if (strcmp($line, "\r\n") == 0)
        {
            // read the header
            $headerdone = true;
        }
        else if ($headerdone)
        {
            // header has been read. now read the contents
            $res .= $line;
        }
    }

    // parse the data
    $lines = explode("\n", $res);
    $keyarray = array();
    if (strcmp ($lines[0], "SUCCESS") == 0)
    {
        for ($i=1; $i<count($lines);$i++)
        {
            list($key,$val) = explode("=", $lines[$i]);
            $keyarray[urldecode($key)] = urldecode($val);
        }
        $firstname = $keyarray['first_name'];
        $lastname = $keyarray['last_name'];
        $itemname = $keyarray['item_name'];
        $amount = $keyarray['payment_gross'];
        $invoiceid = $keyarray['invoice'];
        $profileid = $keyarray['subscr_id'];
        // check the payment_status is Completed
        // check that txn_id has not been previously processed
        // check that receiver_email is your Primary PayPal email
        // check that payment_amount/payment_currency are correct
        // process payment
        // show receipt to client
    }
}

我希望这有帮助。

see below a few comments that I believe will help you resolve this problem:

1 - the notify_url needs to be an url that can be accessed from PayPal. It should be something like http://www.example.com/path/to/your/script. This is the url PayPal will post to with the results of the transaction.

2 - You will need a way to identify the transaction when it comes back from PayPal. The way I do it is by including a field

<input type="hidden" name="invoice" value=$uniquevaluecreatedbyyourapplication>

Paypal will send this field back in the response.

In my case I add the transaction to a MySQL table and I pass the unique record id as the invoice number. When PayPal sends back the response I check the invoice number against my database to identify the transaction.

I hope this helps. If not, please post again.

Update to include more details about the IPN message

Basically when the transaction completes, PayPal will post to the script you passed in the notify_url field.

Paypal will append a token (tx) to that post. Your script will be called with something like this:

http://www.example.com/path/to/your/listener?tx=1234567890099r48&... (PayPal adds also other transaction fields here).

However you should not rely on the transaction fields PayPal adds to the first message because you don't really know it came from PayPal.

So in order to confirm that the message came from PayPal, you need to post back a message to PayPal including the tx_token PayPal sent you; an auth_token only you know (You get this from inside your PayPal seller account); and a cmd variable that tells PayPal you are verifying a transaction.

Once PayPal receives this post from your listener script, it will respond back with a post confirming that the transaction was completed and all the details. You can rely on this second post because it came as a result of your request to Paypal using your unique auth_token.

PayPal will send you a bunch of fields with transaction information including one that confirms whether the payment was successful or is in pending status. Depending on the method the client used to pay, the payment may remain in pending status for a while until it clears. PayPal will send you another message automatically when the payment clears. So you should configure your listener to handle this second message also. PayPal advises that you should not ship the product until the payment has cleared.

And to get you started, please see below the code I use for a listener script that process PayPal's response. You will need to adjust it for your needs, but it is working for me.

$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "enter your own authorization token";
$req .= "&tx=$tx_token&at=$auth_token";

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

//$fp = fsockopen ('http://www.sandbox.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// replace www.sandbox.paypal.com with www.paypal.com when you go live
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

if (!$fp)
{
    // HTTP ERROR
}
else
{
    fputs ($fp, $header . $req);
    // read the body data
    $res = '';
    $headerdone = false;
    while (!feof($fp))
    {
        $line = fgets ($fp, 1024);
        if (strcmp($line, "\r\n") == 0)
        {
            // read the header
            $headerdone = true;
        }
        else if ($headerdone)
        {
            // header has been read. now read the contents
            $res .= $line;
        }
    }

    // parse the data
    $lines = explode("\n", $res);
    $keyarray = array();
    if (strcmp ($lines[0], "SUCCESS") == 0)
    {
        for ($i=1; $i<count($lines);$i++)
        {
            list($key,$val) = explode("=", $lines[$i]);
            $keyarray[urldecode($key)] = urldecode($val);
        }
        $firstname = $keyarray['first_name'];
        $lastname = $keyarray['last_name'];
        $itemname = $keyarray['item_name'];
        $amount = $keyarray['payment_gross'];
        $invoiceid = $keyarray['invoice'];
        $profileid = $keyarray['subscr_id'];
        // check the payment_status is Completed
        // check that txn_id has not been previously processed
        // check that receiver_email is your Primary PayPal email
        // check that payment_amount/payment_currency are correct
        // process payment
        // show receipt to client
    }
}

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文