vkopytich13/paypal-ipn-listener
Composer 安装命令:
composer require vkopytich13/paypal-ipn-listener
包简介
A PayPal IPN (Instant Payment Notification) listener for PHP
README 文档
README
A PayPal IPN (Instant Payment Notification) listener for PHP
Index
Prerequisites
- PHP >=7.1.0
- A good understanding of how the PayPal Instant Payment Notification system works (see here)
Installation
composer require mike182uk/paypal-ipn-listener
Architecture
This package is made up of several components that work together:
Listener- Listens for and processes the IPN messagesVerifier- Verifies the IPN message with PayPalService- Communicates with PayPalMessage- Wrapper around the IPN messageMessageFactory- Creates a new message instance from a data sourceEventDispatcher- Dispatches events
The listener creates a Message using a MessageFactory. The Message is passed to the Verifier which uses a Service to communicate with PayPal. The Listener uses the EventDispatcher to dispatch events relating to the outcome of the IPN message verification.
The MessageFactory and Service components are swappable components.
This package provides 2 message factories:
Mdb\PayPal\Ipn\MessageFactory\InputStreamMessageFactory- Creates a message from thephp://inputstreamMdb\PayPal\Ipn\MessageFactory\ArrayMessageFactory- Creates a message from an array passed to thesetDatamethod
This package provides 1 service:
Mdb\PayPal\Ipn\Service\GuzzleService- Uses Guzzle to communicate with PayPal
Usage
You can either build up the listener object manually or you can use a listener builder. This package provides 2 listener builders:
Mdb\PayPal\Ipn\ListenerBuilder\Guzzle\InputStreamListenerBuilder- Builds a listener using the guzzle service and the input stream message factoryMdb\PayPal\Ipn\ListenerBuilder\Guzzle\ArrayListenerBuilder- Builds a listener using the guzzle service and the array message factory
Using a listener builder is the preferred way of building up a listener object.
Using a listener builder
use Mdb\PayPal\Ipn\ListenerBuilder\Guzzle\InputStreamListenerBuilder as ListenerBuilder; $listener = (new ListenerBuilder)->build();
Building up the listener manually
use GuzzleHttp\Client; use Mdb\PayPal\Ipn\InputStream; use Mdb\PayPal\Ipn\Listener; use Mdb\PayPal\Ipn\MessageFactory\InputStreamMessageFactory; use Mdb\PayPal\Ipn\Service\GuzzleService; use Mdb\PayPal\Ipn\Verifier; use Symfony\Component\EventDispatcher\EventDispatcher; $service = new GuzzleService( new Client(), 'https://www.sandbox.paypal.com/cgi-bin/webscr' ); $verifier = new Verifier($service); $messageFactory = new InputStreamMessageFactory(new InputStream()); $listener = new Listener( $messageFactory, $verifier, new EventDispatcher() );
A lot of plumbing is needed to create the listener manually. The job of the listener builder is to abstract away this logic.
Subscribing to events
Once you have created the listener object you can subscribe to the events that it will dispatch:
use Mdb\PayPal\Ipn\Event\MessageVerifiedEvent; use Mdb\PayPal\Ipn\Event\MessageInvalidEvent; use Mdb\PayPal\Ipn\Event\MessageVerificationFailureEvent; $listener->onVerified(function (MessageVerifiedEvent $event) { $ipnMessage = $event->getMessage(); // IPN message was verified, everything is ok! Do your processing logic here... }); $listener->onInvalid(function (MessageInvalidEvent $event) { $ipnMessage = $event->getMessage(); // IPN message was was invalid, something is not right! Do your logging here... }); $listener->onVerificationFailure(function (MessageVerificationFailureEvent $event) { $error = $event->getError(); // Something bad happend when trying to communicate with PayPal! Do your logging here... });
You can use any callable when subscribing to an event:
class IpnProcessor { public function onVerified(MessageVerifiedEvent $event) { $message = $event->getMessage(); // ... } } $listener->onVerified(array(new Processor, 'onVerified'));
class IpnProcessor { public static function onVerified(MessageVerifiedEvent $event) { $message = $event->getMessage(); // ... } } $listener->onVerified(array('IpnProcessor', 'onVerified'));
Listening for IPN messages
The last thing you need to do to kick of the process is listen for an IPN message:
$listener->listen();
Full Example
use Mdb\PayPal\Ipn\Event\MessageVerifiedEvent; use Mdb\PayPal\Ipn\Event\MessageInvalidEvent; use Mdb\PayPal\Ipn\Event\MessageVerificationFailureEvent; use Mdb\PayPal\Ipn\ListenerBuilder\Guzzle\InputStreamListenerBuilder as ListenerBuilder; $listener = (new ListenerBuilder)->build(); $listener->onVerified(function (MessageVerifiedEvent $event) { $ipnMessage = $event->getMessage(); // IPN message was verified, everything is ok! Do your processing logic here... }); $listener->onInvalid(function (MessageInvalidEvent $event) { $ipnMessage = $event->getMessage(); // IPN message was was invalid, something is not right! Do your logging here... }); $listener->onVerificationFailure(function (MessageVerificationFailureEvent $event) { $error = $event->getError(); // Something bad happend when trying to communicate with PayPal! Do your logging here... }); $listener->listen();
Sandbox mode
When using one of the provided listener builders you can set your listener to use PayPal's sandbox for testing purposes:
$listenerBuilder = new ListenerBuilder(); $listenerBuilder->useSandbox(); // use PayPal sandbox $listener = $listenerBuilder->build();
You can find some full usage examples in the examples directory.
Extending
To create your own service you must implement Mdb\PayPal\Ipn\Service.
To create your own message factory you must implement Mdb\PayPal\Ipn\MessageFactory.
To create your own listener builder it is best to extend Mdb\PayPal\Ipn\ListenerBuilder as this provides most of the boilerplate code needed to create a listener builder.
You will notice that when using any of the provided guzzle listener builders that there is a useSandbox method exposed. You can add this functionality to your listener builder by using the Mdb\PayPal\Ipn\ListenerBuilder\ModeDependentServiceEndpoint trait (see Mdb\PayPal\Ipn\ListenerBuilder\GuzzleListenerBuilder for usage example).
Notes
Testing
PayPal provides an IPN simulator here.
vkopytich13/paypal-ipn-listener 适用场景与选型建议
vkopytich13/paypal-ipn-listener 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「paypal」 「ipn」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vkopytich13/paypal-ipn-listener 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vkopytich13/paypal-ipn-listener 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vkopytich13/paypal-ipn-listener 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PayPal SDK for ExpressCheckout and AdaptivePayments. Supports recurring payments, simple payments, parallel payments and chained payments.
Saferpay Payum plugin providing Ecommerce and Business features
Modernized, framework-agnostic PayPal IPN verification package for legacy IPN workflows.
Rich payment solutions for zend framework2. Paypal, payex, authorize.net, be2bill, omnipay, recurring paymens, instant notifications and many more
Librería para la gestión sencilla de pagos mediante TPV Redsys y Paypal
A PayPal IPN (Instant Payment Notification) listener for PHP
统计信息
- 总下载量: 3.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-10