如何使用PDT获取PayPal付款信息

时间:2018-11-06 09:42:32

标签: php reactjs paypal eclipse-pdt

我已为我的react应用(在服务器A上)设置了一个paypal按钮,并希望获得 通过PDT(由php作为服务器B处理)的交易信息。

我正在沙盒模式下进行测试,在react应用(成功)上付款后,它不会重定向到“返回网址(服务器B)”

※在我的React应用中,它仅在付款事件关闭后没有弹出窗口(但付款事件成功)后弹出一个要付款的窗口

我不知道缺少什么...而且我已经读过this post并确保将其启用

这是我的代码:

PayPal按钮进行反应

<PaypalButton
  client={CLIENT} //token
  env={ENV} //sandbox
  commit={true}
  currency={'TWD'}
  total={90}
  onSuccess={onSuccess}
  onError={onError}
  onCancel={onCancel}
/>

PDT的PHP代码

public function PDTPrase()
{
    $pp_hostname = "www.sandbox.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-synch';

    $tx_token = $_GET['tx'];
    $auth_token = "XXXXXX";
    $req .= "&tx=$tx_token&at=$auth_token";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    //set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
    //if your server does not bundled with default verisign certificates.
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
    $res = curl_exec($ch);
    curl_close($ch);
    if (!$res) {
        //HTTP ERROR
    } else {
        // parse the data
        $lines = explode("\n", trim($res));
        $keyarray = array();
        if (strcmp($lines[0], "SUCCESS") == 0) {
            for ($i = 1; $i < count($lines); $i++) {
                $temp = explode("=", $lines[$i], 2);
                $keyarray[urldecode($temp[0])] = urldecode($temp[1]);
            }
            // 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
            $firstname = $keyarray['first_name'];
            $lastname = $keyarray['last_name'];
            $itemname = $keyarray['item_name'];
            $amount = $keyarray['payment_gross'];

            echo ("<p><h3>Thank you for your purchase!</h3></p>");

            echo ("<b>Payment Details</b><br>\n");
            echo ("<li>Name: $firstname $lastname</li>\n");
            echo ("<li>Item: $itemname</li>\n");
            echo ("<li>Amount: $amount</li>\n");
            echo ("");
        } else if (strcmp($lines[0], "FAIL") == 0) {
            // log for manual investigation
        }
    }
}

0 个答案:

没有答案