简体   繁体   English

Paypal BuyNow按钮快速结帐

[英]Paypal BuyNow button express checkout

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. 我想使用Express Checkout和notify_url参数将Paypal集成到我的网站中,这会通知我的网站,因此我可以采取措施。

I generated buynowbuttons. 我生成了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="something@something.com">
                <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. 我添加了<input name="notify_url" value='http://localhost:9000/Home/ProcessPayment' type="hidden"/>以便它可以通知控制器中的操作方法以进行调试。 However, it is not working. 但是,它不起作用。

I get redirected to Paypal account : 我被重定向到Paypal帐户:

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. 但是一旦付款完成,它就永远不会在Home控制器的ProcessPayment操作中达到我的断点。 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 ;). 更新:-我有hidemyass专业帐户,然后我所要做的就是在IIS中设置我的网站,然后我的notify_url就像一个魅力;)。

It is because they create their own HTTPRequest to that URL, and it is not part of your session. 这是因为他们为该URL创建了自己的HTTPRequest,因此它不是会话的一部分。

They will try to contact your localhost:9000 from their server, and this URL doesnt exist to them. 他们将尝试从其服务器与您的localhost:9000联系,并且此URL对他们而言不存在。

You will need to put in some logging to check what happens when they talk to this URL, eg log that they have reached the page, and what happened in the code. 您将需要进行一些日志记录,以检查他们与该URL对话时发生的情况,例如记录他们已到达页面的日志以及代码中发生的情况。 Then put this page "Live" somewhere for PayPal to talk to. 然后将此页面“实时”放置在PayPal与之对话的地方。

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: 如果您没有“直接”外部IP地址(并且您将至少拥有一台路由器),或者您已将所有内容配置为执行正确的NAT端口转发,则PayPal IPN可以:

1) Nirvana, because you have not configured the IP address correctly 1)必杀技,因为您没有正确配置IP地址

OR 要么

2) To your Testserver/Productionserver which IP-Adress for IPNs you actually configured. 2)向您的Testserver / Productionserver实际配置了哪个IP地址的IP地址。

UPDATE: 更新:

There are 2 Solutions: 有2个解决方案:

  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 PayPal IPN侦听器设置为您的外部地址(可能是您的家庭路由器或公司路由器),并配置NAT或要求您的管理员执行此操作

OR 要么

  1. Use the Remote debugger on your live/test system, to attach to the iis-process and hit the breakpoint. 使用实时/测试系统上的远程调试器 ,将其附加到iis进程并命中断点。

I personally would tend to solution 1., because i never got the remote debugger to work like i expected.. 我个人倾向于解决方案1.,因为我从来没有像预期的那样使远程调试器正常工作。

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. 1-notify_url必须是可以从PayPal访问的URL。 It should be something like http://www.example.com/path/to/your/script . 它应该类似于http://www.example.com/path/to/your/script This is the url PayPal will post to with the results of the transaction. 这是PayPal随交易结果发布到的网址。

2 - You will need a way to identify the transaction when it comes back from PayPal. 2-当您从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. 就我而言,我将交易添加到MySQL表中,并传递唯一的记录ID作为发票编号。 When PayPal sends back the response I check the invoice number against my database to identify the transaction. 当PayPal发回响应时,我对照数据库检查发票编号以识别交易。

I hope this helps. 我希望这有帮助。 If not, please post again. 如果没有,请重新发布。

Update to include more details about the IPN message 更新以包括有关IPN消息的更多详细信息

Basically when the transaction completes, PayPal will post to the script you passed in the notify_url field. 基本上,交易完成后,PayPal将发布到您在notify_url字段中传递的脚本中。

Paypal will append a token (tx) to that post. Paypal将在该帖子后附加一个标记(tx)。 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. 但是,您不应该依赖PayPal添加到第一条消息中的交易字段,因为您实际上并不知道它来自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; 因此,为了确认消息来自PayPal,您需要将消息回发到PayPal,包括发送给您的tx_token PayPal。 an auth_token only you know (You get this from inside your PayPal seller account); 仅您知道的auth_token(您可以从您的PayPal卖家帐户中获取); and a cmd variable that tells PayPal you are verifying a transaction. 还有一个cmd变量,该变量告诉PayPal您正在验证交易。

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. 当PayPal从您的侦听器脚本中收到此帖子后,它将以一则帖子回覆,确认交易已完成以及所有详细信息。 You can rely on this second post because it came as a result of your request to Paypal using your unique auth_token. 您可以依赖第二篇文章,因为它是您使用唯一的auth_token向Paypal请求的结果。

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. PayPal将向您发送一堆包含交易信息的字段,其中包括一个用于确认付款是否成功或处于待处理状态的字段。 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. 贝宝(PayPal)建议您在付款结清后再发货。

And to get you started, please see below the code I use for a listener script that process PayPal's response. 为了让您入门,请在下面查看我用于处理PayPal响应的侦听器脚本的代码。 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. 我希望这有帮助。

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

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