使用PayPal即时付款通知时,使用PHP获取自定义变量的值

时间:2016-11-08 15:04:14

标签: php paypal

我正在尝试在我的服务器上使用PHP实现PayPal IPN。我在GitHub上找到了Paypal的代码。我的理解是,我将PaypalIPN.php和我的example_usage.php版本放在服务器上,并将我的example_usage.php版本的网址指定为PayPal。

example_usage.php看起来像这样:

<?php require('PaypalIPN.php');

use PaypalIPN;
$ipn = new PayPalIPN();
// Use the sandbox endpoint during testing.
$ipn->useSandbox();
$verified = $ipn->verifyIPN();
if ($verified) {
    /*
     * Process IPN
     * A list of variables is available here:
     * https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNandPDTVariables/
     */

***** This is where I should get the value of custom and store it in DB or whatever *****
** getPOSTdata() used here **

}
// Reply with an empty 200 response to indicate to paypal the IPN was received correctly.
header("HTTP/1.1 200 OK");

我将通过以下方式向paypal发送自定义变量:

<a id="a-submit" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&custom=customdata&hosted_button_id=someID">Pay with PayPal</a>

Paypal应该在自定义变量下返回它。我该如何访问它?小问题。这真的吗?它看起来非常简单,我担心我错过了什么。

编辑:我使用以下函数编辑了PaypalIPN.php

private $postData = "";

  public function getPOSTdata(){
        return $postData;
    }

**IN verifyIPN()**
 $raw_post_data = file_get_contents('php://input');
$postData=$raw_post_data;

我以下列方式使用它:

$lastname = $ipn->getPOSTdata();

我面临的问题是$lastname是一个空字符串。这不可能是真的,因为我知道PayPal必须给我一些东西。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要添加一个方法来访问 PaypalPN.php

中的 $ myPost 数组

PaypalIPN.php第6 - 8行

private $use_sandbox = false;

private $use_local_certs = true;

更改为以下内容:

private $use_sandbox = false;
public  $pubMyPost;
private $use_local_certs = true;

然后再往下......

PaypalIPN.php第75-79行:

            $myPost[$keyval[0]] = urldecode($keyval[1]);
        }
    }

    // Build the body of the verification post request, adding the _notify-validate command.

更改为:

            $myPost[$keyval[0]] = urldecode($keyval[1]);
        }
    }
    this.pubMyPost = $myPost; // set the public var
    // Build the body of the verification post request, adding the _notify-validate command.

然后在example_usage.php里面

$ipn->useSandbox();
$verified = $ipn->verifyIPN();
if ($verified) {
    // Now you can access all the public var containing your key=value pairs from the post.
    $myPost = $ipn->pubMyPost;
}