toolani / paypal-ipn-verifier
验证PayPal IPN
Requires
- php: >=5.3.0
- psr/log: ~1.0
This package is not auto-updated.
Last update: 2024-09-28 17:07:21 UTC
README
PHP 5的PayPal即时付款通知(IPN)验证器类。
在您的IPN脚本中使用IpnVerifier
类来处理POST数据的编码、向PayPal发送回执以及解析PayPal的响应。
此类基于(并且与Quixotix/PHP-PayPal-IPN几乎API兼容)Quixotix/PHP-PayPal-IPN。下面是差异和代码移植的技巧。
特性
- 支持实时和沙盒环境。
- 验证来自PayPal服务器的HTTP "200"响应状态码。
- 使用
getTextReport()
方法获取整个IPN的详细纯文本报告,以便在电子邮件和日志中用于管理员。 - 抛出各种异常以区分代码或服务器配置中的常见错误与无效的IPN响应。
与Quixotix/PHP-PayPal-IPN的差异
- 始终使用SSL验证IPN。
- 始终使用cURL(不支持fsockopen)。
- PSR-2合规的编码风格。
入门指南
此代码面向Web开发者。您应该理解IPN过程的概念性工作原理,并了解何时以及为何要使用IPN。阅读PayPal即时付款通知指南是一个好的开始。
您还应该有一个PayPal沙盒账户,其中包括一个测试买家账户和一个测试卖家账户。登录您的沙盒账户后,在“测试工具”菜单下有一个IPN模拟器,您可以使用它来测试您的IPN监听器。
一旦您设置了沙盒账户,您只需创建一个PHP脚本,该脚本将成为您的IPN监听器。在该脚本中,使用如下所示的IpnVerifier()
类。
<?php use Toolani\Payment\Paypal\IpnVerifier; // If you're doing this for real, autoloading would be better include("/path/to/Toolani/Payment/Paypal/IpnVerifier.php"); $verifier = new IpnVerifier($useSandbox = true); try { $verified = $verifier->verify($_POST); } catch (Exception $e) { // fatal error trying to process IPN. exit(1); } if ($verified) { // IPN response was "VERIFIED" } else { // IPN response was "INVALID" } // Verification status is available as a string, too. See table below $status = $verifier->getVerificationStatusString();
日志记录
IpnVerifier
构造函数可以接受一个PSR-3兼容的日志对象作为其第二个参数。
如果提供,它将用于记录有关每个IPN的一些基本信息以及验证过程中出现的任何错误详情。
验证状态字符串
以下是由getVerificationStatusString
可能返回的可能状态。在正常操作中,只期望'VERIFIED'和'INVALID'。其他状态指示错误。
Quixotix/PHP-PayPal-IPN兼容性
将使用PHP-PayPal-IPN类的脚本移植到使用IpnVerifier
应该是直接的,并且提供了一个processIpn()
方法来简化此过程。
上面的用法示例与PHP-PayPal-IPN文档中给出的示例直接等效。
需要注意的事项
- 沙盒配置现在是通过构造函数参数完成的,而不是公开属性,尽管
useSandbox
属性仍然是公开的(目前如此)。 - 没有配置cURL的选项,SSL将始终使用。
verify
方法需要您显式提供POST数据的数组。PHP-PayPal-IPN的processIpn
没有这样做,如果未提供数据,会自动使用$_POST
。因此提供了一个processIpn
方法,以模仿旧的行为。注意:verify
是验证IPN的首选方法,processIpn
应被视为已弃用,并且可能在未来的版本中删除。
示例报告
以下是一个由 getTextReport()
方法返回的报告示例。通过扩展 IpnVerifier
类或在您的 ipn 脚本中直接访问数据来创建您自己的报告。
--------------------------------------------------------------------------------
[09/09/2011 8:35 AM] - https://www.sandbox.paypal.com/cgi-bin/webscr (curl)
--------------------------------------------------------------------------------
HTTP/1.1 200 OK
Date: Fri, 09 Sep 2011 13:35:39 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Set-Cookie: c9MWDuvPtT9GIMyPc3jwol1VSlO=Ch-NORlHUjlmbEm__KG9LupR4mfMfQTkx1QQ6hHDyc0RImWr88NY_ILeICENiwtVX3iw4jEnT1-1gccYjQafWrQCkDmiykNT8TeDUg7R7L0D9bQm47PTG8MafmrpyrUAxQfst0%7c_jG1ZL6CffJgwrC-stQeqni04tKaYSIZqyqhFU7tKnV520wiYOw0hwk5Ehrh3hLDvBxkpm%7cYTFdl0w0YpEqxu0D1jDTVTlEGXlmLs4wob2Glu9htpZkFV9O2aCyfQ4CvA2kLJmlI6YiXm%7c1315575340; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: cookie_check=yes; expires=Mon, 06-Sep-2021 13:35:40 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navlns=0.0; expires=Thu, 04-Sep-2031 13:35:40 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: Apache=10.72.109.11.1315575339707456; path=/; expires=Sun, 01-Sep-41 13:35:39 GMT
X-Cnection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
VERIFIED
--------------------------------------------------------------------------------
test_ipn 1
payment_type instant
payment_date 06:34:51 Sep 09, 2011 PDT
payment_status Completed
address_status confirmed
payer_status verified
first_name John
last_name Smith
payer_email buyer@paypalsandbox.com
payer_id TESTBUYERID01
address_name John Smith
address_country United States
address_country_code US
address_zip 95131
address_state CA
address_city San Jose
address_street 123, any street
business seller@paypalsandbox.com
receiver_email seller@paypalsandbox.com
receiver_id TESTSELLERID1
residence_country US
item_name something
item_number AK-1234
quantity 1
shipping 3.04
tax 2.02
mc_currency USD
mc_fee 0.44
mc_gross 12.34
mc_gross_1 9.34
txn_type web_accept
txn_id 51991334
notify_version 2.1
custom xyz123
charset windows-1252
verify_sign Ah5rOpfPGo5g6FNg95DMPybP51J5AUEdXS1hqyRAP6WYYwaixKNDgQRR