From b91c12d0cf680da39de2d503819261adfb445f1c Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Wed, 9 Jul 2014 12:39:12 +0200 Subject: [PATCH] Specify optional notify_url for IPN --- DependencyInjection/Configuration.php | 3 +- .../JMSPaymentPaypalExtension.php | 3 +- Plugin/ExpressCheckoutPlugin.php | 31 +++++++++++++++++-- Resources/config/services.xml | 2 ++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index be34468..8e8e4ca 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -34,9 +34,10 @@ public function getConfigTree() ->scalarNode('signature')->isRequired()->cannotBeEmpty()->end() ->scalarNode('return_url')->defaultNull()->end() ->scalarNode('cancel_url')->defaultNull()->end() + ->scalarNode('notify_url')->defaultNull()->end() ->booleanNode('debug')->defaultValue('%kernel.debug%')->end() ->end() ->end() ->buildTree(); } -} \ No newline at end of file +} diff --git a/DependencyInjection/JMSPaymentPaypalExtension.php b/DependencyInjection/JMSPaymentPaypalExtension.php index 21cbee0..55a2453 100644 --- a/DependencyInjection/JMSPaymentPaypalExtension.php +++ b/DependencyInjection/JMSPaymentPaypalExtension.php @@ -40,6 +40,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('payment.paypal.signature', $config['signature']); $container->setParameter('payment.paypal.express_checkout.return_url', $config['return_url']); $container->setParameter('payment.paypal.express_checkout.cancel_url', $config['cancel_url']); + $container->setParameter('payment.paypal.express_checkout.notify_url', $config['notify_url']); $container->setParameter('payment.paypal.debug', $config['debug']); } -} \ No newline at end of file +} diff --git a/Plugin/ExpressCheckoutPlugin.php b/Plugin/ExpressCheckoutPlugin.php index a4b2528..692914f 100644 --- a/Plugin/ExpressCheckoutPlugin.php +++ b/Plugin/ExpressCheckoutPlugin.php @@ -42,6 +42,11 @@ class ExpressCheckoutPlugin extends AbstractPlugin */ protected $cancelUrl; + /** + * @var string + */ + protected $notifyUrl; + /** * @var \JMS\Payment\PaypalBundle\Client\Client */ @@ -51,12 +56,14 @@ class ExpressCheckoutPlugin extends AbstractPlugin * @param string $returnUrl * @param string $cancelUrl * @param \JMS\Payment\PaypalBundle\Client\Client $client + * @param string $notifyUrl */ - public function __construct($returnUrl, $cancelUrl, Client $client) + public function __construct($returnUrl, $cancelUrl, Client $client, $notifyUrl = null) { $this->client = $client; $this->returnUrl = $returnUrl; $this->cancelUrl = $cancelUrl; + $this->notifyUrl = $notifyUrl; } public function approve(FinancialTransactionInterface $transaction, $retry) @@ -188,12 +195,20 @@ protected function createCheckoutBillingAgreement(FinancialTransactionInterface // complete the transaction $data->set('paypal_payer_id', $details->body->get('PAYERID')); + $optionalParameters = array( + 'PAYMENTREQUEST_0_CURRENCYCODE' => $transaction->getPayment()->getPaymentInstruction()->getCurrency(), + ); + + if (null !== $notifyUrl = $this->getNotifyUrl($data)) { + $optionalParameters['PAYMENTREQUEST_0_NOTIFYURL'] = $notifyUrl; + } + $response = $this->client->requestDoExpressCheckoutPayment( $data->get('express_checkout_token'), $transaction->getRequestedAmount(), $paymentAction, $details->body->get('PAYERID'), - array('PAYMENTREQUEST_0_CURRENCYCODE' => $transaction->getPayment()->getPaymentInstruction()->getCurrency()) + $optionalParameters ); $this->throwUnlessSuccessResponse($response, $transaction); @@ -303,4 +318,14 @@ protected function getCancelUrl(ExtendedDataInterface $data) throw new \RuntimeException('You must configure a cancel url.'); } -} \ No newline at end of file + + protected function getNotifyUrl(ExtendedDataInterface $data) + { + if ($data->has('notify_url')) { + return $data->get('notify_url'); + } + else if (0 !== strlen($this->notifyUrl)) { + return $this->notifyUrl; + } + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 48cf053..79219a5 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -8,6 +8,7 @@ JMS\Payment\PaypalBundle\Plugin\ExpressCheckoutPlugin + JMS\Payment\PaypalBundle\Client\Authentication\TokenAuthenticationStrategy @@ -32,6 +33,7 @@ %payment.paypal.express_checkout.return_url% %payment.paypal.express_checkout.cancel_url% + %payment.paypal.express_checkout.notify_url%