criobot / laravel5-paypal-ipn
Laravel 5 的 Paypal IPN 类
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-27 20:18:10 UTC
README
此包只是本仓库 IPN 类的实现
https://github.com/Quixotix/PHP-PayPal-IPN
在 composer.json 的 "require-dev" 中添加以下内容
"fahim/laravel5-paypal-ipn": "1.0.*"
PHP-PayPal-IPN
PHP 5 的 PayPal 即时支付通知 (IPN) 类。
在您的 PHP IPN 脚本中使用 PaypalIPNListener
类来处理 POST 数据的编码、回传到 PayPal 以及解析 PayPal 的响应。
Laravel 服务
'Fahim\PaypalIPN\PaypalIPNServiceProvider',
用例
use Fahim\PaypalIPN\PaypalIPNListener; public function paypalIpn() { $ipn = new PaypalIPNListener(); $ipn->use_sandbox = true; $verified = $ipn->processIpn(); $report = $ipn->getTextReport(); Log::info("-----new payment-----"); Log::info($report); if ($verified) { if ($_POST['address_status'] == 'confirmed') { // Check outh POST variable and insert your logic here Log::info("payment verified and inserted to db"); } } else { Log::info("Some thing went wrong in the payment !"); } }
功能
- 通过设置
use_sandbox
属性在实时和沙盒模式之间切换。 - 通过设置
use_ssl
属性支持安全 SSL 和纯 HTTP 交易(推荐使用 SSL)。 - 通过设置
use_curl
属性支持 cURL 和 fsockopen 网络库(推荐使用 cURL)。 - 验证 PayPal 服务器的 HTTP "200" 响应状态码。
- 使用
getTextReport()
方法获取整个 IPN 的详细纯文本报告,以便在电子邮件和日志中供管理员使用。 - 抛出各种异常,以区分代码或服务器配置中的常见错误与无效的 IPN 响应。
入门指南
此代码是为网络开发者设计的。您应该理解 IPN 过程的概念,并理解何时以及为何要使用 IPN。阅读 PayPal 即时支付通知指南 是一个很好的起点。
您还应该拥有一个 PayPal 沙盒账户,其中包括测试买家账户和测试卖家账户。登录沙盒账户后,在 '测试工具' 菜单下有一个 IPN 模拟器,您可以用来测试您的 IPN 监听器。
一旦您设置了沙盒账户,您只需创建一个 PHP 脚本,该脚本将成为您的 IPN 监听器。在该脚本中,使用如下所示的 IpnListener()
类。有关更详尽的示例,请查看源代码中的 example/ipn.php
脚本。
文档
文档尚未生成,但 ipnlistener.php
中有 phpDocumentor 风格的 docstrings(注释),解释了重要的公共属性和方法。
我在我的博客上也写了一个更深入的 IPN 教程: 使用 PHP 的 PayPal IPN
已知问题
问题
processIpn()
方法抛出以下异常
cURL error: [52] GnuTLS recv error (-9): A TLS packet with unexpected length was received.
解决方案
当 cURL 与 GnuTLS 编译时,如果 SSL 版本未明确设置为 cURL 选项,则调用 PayPal 会失败。将 force_ssl_v3
属性设置为强制使用 SSL 3
$listener = new IpnListener(); $listener->force_ssl_v3 = true;
注意:force_ssl_v3 现在默认为 true
问题
PHP Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION
cannot be activated when in safe_mode or an open_basedir is set in ...
解决方案
如果您需要 PHP 安全模式,可以使用 follow_location
属性禁用 CURLOPT_FOLLOWLOCATION。
$listener = new IpnListener(); $listener->follow_location = false;
注意:follow_location 现在默认为 false
示例报告
以下是 getTextReport()
方法返回的报告示例。通过扩展 IpnListener()
类或在您的 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