From 1d04cfc0734f2ac5b2b1241c099025c9988ae49f Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Tue, 25 Jul 2017 16:09:43 +0100 Subject: [PATCH 1/9] Adding PayPal Rest extension --- code/extensions/PayPaylRestExtension.php | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 code/extensions/PayPaylRestExtension.php diff --git a/code/extensions/PayPaylRestExtension.php b/code/extensions/PayPaylRestExtension.php new file mode 100644 index 00000000..3a3471af --- /dev/null +++ b/code/extensions/PayPaylRestExtension.php @@ -0,0 +1,50 @@ +getPayment(); + + // We only want to respond to the notification if we are using SagePay + if ($payment->Gateway !== 'PayPal_Rest') { + return; + } + + $omniPayResponse = $response->getOmnipayResponse(); + if ($omnipayResponse !== null + && $omnipayResponse->isSuccessful() + && !$response->isError()) { + + if ($omniPayResponse instanceof RestAuthorizeResponse) { + $payment->TransactionReference = $omniPayResponse->getTransactionId(); + $payment->write(); + } + } + } +} From 7859d95d3dc97d43c6c50c6b74a177032a5b8c20 Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Tue, 25 Jul 2017 16:11:23 +0100 Subject: [PATCH 2/9] Slight docuimentation tweek --- code/extensions/PayPaylRestExtension.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/code/extensions/PayPaylRestExtension.php b/code/extensions/PayPaylRestExtension.php index 3a3471af..3e028883 100644 --- a/code/extensions/PayPaylRestExtension.php +++ b/code/extensions/PayPaylRestExtension.php @@ -3,6 +3,15 @@ use Omnipay\PayPal\Message\RestAuthorizeResponse; use SilverStripe\Omnipay\Service\ServiceResponse; +/** + * PayPal Rest can woerk without taking a credit card, this extension deals with + * that and the response that comes back from PayPal + * + * # payment.yml + * SilverStripe\Omnipay\Service\PaymentService: + * extensions: + * - PayPalRestExtension + */ class PayPalRestExtension extends Extension { /** @@ -31,7 +40,7 @@ public function updateServiceResponse($response) /** @var Payment $payment */ $payment = $response->getPayment(); - // We only want to respond to the notification if we are using SagePay + // We only want to process the response if we are using Paypal_Rest if ($payment->Gateway !== 'PayPal_Rest') { return; } From 55717d9901c2120cb08f229fae964df2f129bdef Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Tue, 25 Jul 2017 16:21:59 +0100 Subject: [PATCH 3/9] Added more docs --- code/extensions/PayPaylRestExtension.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/code/extensions/PayPaylRestExtension.php b/code/extensions/PayPaylRestExtension.php index 3e028883..872fa976 100644 --- a/code/extensions/PayPaylRestExtension.php +++ b/code/extensions/PayPaylRestExtension.php @@ -4,7 +4,7 @@ use SilverStripe\Omnipay\Service\ServiceResponse; /** - * PayPal Rest can woerk without taking a credit card, this extension deals with + * PayPal Rest can work without taking a credit card, this extension deals with * that and the response that comes back from PayPal * * # payment.yml @@ -28,6 +28,12 @@ public function onBeforePurchase(array &$gatewayData) public function onBeforeCompletePurchase(array &$gatewayData) { + /** + * As described in Omnipay\PayPal\Message\RestPurchaseRequest PayPal + * responds from the payment withthe transaction reference and a + * PayerID as GET vars. We gather tham and throw them back to PayPal + * for confirmation + */ $gatewayData['transactionReference'] = $_GET['paymentId']; $gatewayData['payerId'] = $_GET['PayerID']; } @@ -40,7 +46,7 @@ public function updateServiceResponse($response) /** @var Payment $payment */ $payment = $response->getPayment(); - // We only want to process the response if we are using Paypal_Rest + // We only want to process the response if we are using PayPal_Rest if ($payment->Gateway !== 'PayPal_Rest') { return; } From f04d23344d5f56f9103ccab865c0af17adf7ead0 Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Tue, 25 Jul 2017 16:32:31 +0100 Subject: [PATCH 4/9] Added properties to the Payment model for IDE goodness --- code/model/Payment.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/code/model/Payment.php b/code/model/Payment.php index d3919bd1..a05da383 100644 --- a/code/model/Payment.php +++ b/code/model/Payment.php @@ -10,6 +10,13 @@ * paid or not, and the gateway used to make payment. * * @package payment + * @property string Gateway + * @property Money Money + * @property string Status + * @property string Identifier + * @property string TransactionReference + * @property string SuccessUrl + * @property string FailureUrl */ final class Payment extends DataObject implements PermissionProvider { From 48661092dace086a1ce26ec7a1a02d8b77cb82d5 Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Wed, 26 Jul 2017 09:24:06 +0100 Subject: [PATCH 5/9] Fixed typo --- .../{PayPaylRestExtension.php => PayPalRestExtension.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename code/extensions/{PayPaylRestExtension.php => PayPalRestExtension.php} (100%) diff --git a/code/extensions/PayPaylRestExtension.php b/code/extensions/PayPalRestExtension.php similarity index 100% rename from code/extensions/PayPaylRestExtension.php rename to code/extensions/PayPalRestExtension.php From 71a560d246f54f0b647bedc541bb5e944750f6fc Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Wed, 26 Jul 2017 09:40:51 +0100 Subject: [PATCH 6/9] Removing code that is no longer useful --- code/extensions/PayPalRestExtension.php | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/code/extensions/PayPalRestExtension.php b/code/extensions/PayPalRestExtension.php index 872fa976..b56915d5 100644 --- a/code/extensions/PayPalRestExtension.php +++ b/code/extensions/PayPalRestExtension.php @@ -37,29 +37,4 @@ public function onBeforeCompletePurchase(array &$gatewayData) $gatewayData['transactionReference'] = $_GET['paymentId']; $gatewayData['payerId'] = $_GET['PayerID']; } - - /** - * @param ServiceResponse $response - */ - public function updateServiceResponse($response) - { - /** @var Payment $payment */ - $payment = $response->getPayment(); - - // We only want to process the response if we are using PayPal_Rest - if ($payment->Gateway !== 'PayPal_Rest') { - return; - } - - $omniPayResponse = $response->getOmnipayResponse(); - if ($omnipayResponse !== null - && $omnipayResponse->isSuccessful() - && !$response->isError()) { - - if ($omniPayResponse instanceof RestAuthorizeResponse) { - $payment->TransactionReference = $omniPayResponse->getTransactionId(); - $payment->write(); - } - } - } } From 6250dd6653ddc3cf571ecdee22c2be2c506fc8c4 Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Wed, 26 Jul 2017 10:24:17 +0100 Subject: [PATCH 7/9] Added Docblock and corrected typo --- code/extensions/PayPalRestExtension.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code/extensions/PayPalRestExtension.php b/code/extensions/PayPalRestExtension.php index b56915d5..9861d352 100644 --- a/code/extensions/PayPalRestExtension.php +++ b/code/extensions/PayPalRestExtension.php @@ -26,11 +26,14 @@ public function onBeforePurchase(array &$gatewayData) unset($gatewayData['card']); } + /** + * @param array $gatewayData + */ public function onBeforeCompletePurchase(array &$gatewayData) { /** * As described in Omnipay\PayPal\Message\RestPurchaseRequest PayPal - * responds from the payment withthe transaction reference and a + * responds from the payment with the transaction reference and a * PayerID as GET vars. We gather tham and throw them back to PayPal * for confirmation */ From bf4f687e1fc0aac18ef23d59a5c70b550968ee1d Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Wed, 26 Jul 2017 10:31:50 +0100 Subject: [PATCH 8/9] Removed unused includes --- code/extensions/PayPalRestExtension.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/code/extensions/PayPalRestExtension.php b/code/extensions/PayPalRestExtension.php index 9861d352..86a9bb0a 100644 --- a/code/extensions/PayPalRestExtension.php +++ b/code/extensions/PayPalRestExtension.php @@ -1,8 +1,5 @@ Date: Wed, 26 Jul 2017 10:57:22 +0100 Subject: [PATCH 9/9] Added check for Rest Gateway --- code/extensions/PayPalRestExtension.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/code/extensions/PayPalRestExtension.php b/code/extensions/PayPalRestExtension.php index 86a9bb0a..fa1a7631 100644 --- a/code/extensions/PayPalRestExtension.php +++ b/code/extensions/PayPalRestExtension.php @@ -20,7 +20,9 @@ public function onBeforePurchase(array &$gatewayData) * If you don't want to supply a Credit card then unsetting this will * take you through to PayPal without one */ - unset($gatewayData['card']); + if ($this->isRestGateway()) { + unset($gatewayData['card']); + } } /** @@ -31,10 +33,20 @@ public function onBeforeCompletePurchase(array &$gatewayData) /** * As described in Omnipay\PayPal\Message\RestPurchaseRequest PayPal * responds from the payment with the transaction reference and a - * PayerID as GET vars. We gather tham and throw them back to PayPal + * PayerID as GET vars. We gather them and throw them back to PayPal * for confirmation */ - $gatewayData['transactionReference'] = $_GET['paymentId']; - $gatewayData['payerId'] = $_GET['PayerID']; + if ($this->isRestGateway()) { + $gatewayData['transactionReference'] = $_GET['paymentId']; + $gatewayData['payerId'] = $_GET['PayerID']; + } + } + + /** + * @return bool + */ + public function isRestGateway() + { + return ($this->owner->getPayment()->Gateway === 'PayPal_Rest'); } }