From 23f998e3fdf7f860d3096d9fb30aea19498d6ca6 Mon Sep 17 00:00:00 2001 From: "Alexander (SASh) Alexiev" Date: Fri, 24 Apr 2020 17:11:03 +0300 Subject: [PATCH 1/4] add: create setup intent request --- src/Message/SetupIntents/AbstractRequest.php | 39 ++++++ .../SetupIntents/CreateSetupIntentRequest.php | 66 +++++++++ src/Message/SetupIntents/Response.php | 130 ++++++++++++++++++ src/PaymentIntentsGateway.php | 12 ++ 4 files changed, 247 insertions(+) create mode 100644 src/Message/SetupIntents/AbstractRequest.php create mode 100644 src/Message/SetupIntents/CreateSetupIntentRequest.php create mode 100644 src/Message/SetupIntents/Response.php diff --git a/src/Message/SetupIntents/AbstractRequest.php b/src/Message/SetupIntents/AbstractRequest.php new file mode 100644 index 00000000..4ea498e7 --- /dev/null +++ b/src/Message/SetupIntents/AbstractRequest.php @@ -0,0 +1,39 @@ +setParameter('paymentIntentReference', $value); + } + + /** + * @return mixed + */ + public function getSetupIntentReference() + { + return $this->getParameter('paymentIntentReference'); + } + +} diff --git a/src/Message/SetupIntents/CreateSetupIntentRequest.php b/src/Message/SetupIntents/CreateSetupIntentRequest.php new file mode 100644 index 00000000..951325b6 --- /dev/null +++ b/src/Message/SetupIntents/CreateSetupIntentRequest.php @@ -0,0 +1,66 @@ + + * + * + * + * @see \Omnipay\Stripe\Message\PaymentIntents\AttachPaymentMethodRequest + * @see \Omnipay\Stripe\Message\PaymentIntents\DetachPaymentMethodRequest + * @see \Omnipay\Stripe\Message\PaymentIntents\UpdatePaymentMethodRequest + * @link https://stripe.com/docs/api/setup_intents/create + */ +class CreateSetupIntentRequest extends AbstractRequest +{ + /** + * @inheritdoc + */ + public function getData() + { + $data = []; + + if ($this->getCustomerReference()) { + $data['customer'] = $this->getCustomerReference(); + } + if ($this->getDescription()){ + $data['description'] = $this->getDescription(); + } + + if ($this->getMetadata()){ + $this['metadata'] = $this->getMetadata(); + } + if ($this->getPaymentMethod()){ + $this['payment_method'] = $this->getPaymentMethod(); + } + + $data['usage'] = 'off_session'; + $data['payment_method_types'][] = 'card'; + + return $data; + } + + /** + * @inheritdoc + */ + public function getEndpoint() + { + return $this->endpoint.'/setup_intents'; + } + + /** + * @inheritdoc + */ + protected function createResponse($data, $headers = []) + { + return $this->response = new Response($this, $data, $headers); + } +} diff --git a/src/Message/SetupIntents/Response.php b/src/Message/SetupIntents/Response.php new file mode 100644 index 00000000..d9d30f67 --- /dev/null +++ b/src/Message/SetupIntents/Response.php @@ -0,0 +1,130 @@ +data['object']) && 'setup_intent' === $this->data['object']) { + return $this->data['status']; + } + + return null; + } + + /** + * Return true if the payment intent requires confirmation. + * + * @return bool + */ + public function requiresConfirmation() + { + return $this->getStatus() === 'requires_confirmation'; + } + + /** + * @inheritdoc + */ + public function getClientSecret() + { + if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) { + if (!empty($this->data['client_secret'])) { + return $this->data['client_secret']; + } + } + } + + /** + * @inheritdoc + */ + public function getCustomerReference() + { + + if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) { + if (!empty($this->data['customer'])) { + return $this->data['customer']; + } + } + + return parent::getCustomerReference(); + } + + /** + * @inheritdoc + */ + public function isSuccessful() + { + if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) { + return in_array($this->getStatus(), ['succeeded', 'requires_capture']); + } + + return parent::isSuccessful(); + } + + /** + * @inheritdoc + */ + public function isCancelled() + { + if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) { + return $this->getStatus() === 'canceled'; + } + + return parent::isCancelled(); + } + + /** + * @inheritdoc + */ + public function isRedirect() + { + if ($this->getStatus() === 'requires_action' || $this->getStatus() === 'requires_source_action') { + // Currently this gateway supports only manual confirmation, so any other + // next action types pretty much mean a failed transaction for us. + return (!empty($this->data['next_action']) && $this->data['next_action']['type'] === 'redirect_to_url'); + } + + return parent::isRedirect(); + } + + /** + * @inheritdoc + */ + public function getRedirectUrl() + { + return $this->isRedirect() ? $this->data['next_action']['redirect_to_url']['url'] : parent::getRedirectUrl(); + } + + /** + * Get the payment intent reference. + * + * @return string|null + */ + public function getSetupIntentReference() + { + if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) { + return $this->data['id']; + } + + return null; + } +} diff --git a/src/PaymentIntentsGateway.php b/src/PaymentIntentsGateway.php index 88e24571..a5ed27e2 100644 --- a/src/PaymentIntentsGateway.php +++ b/src/PaymentIntentsGateway.php @@ -166,4 +166,16 @@ public function deleteCard(array $parameters = array()) { return $this->createRequest('\Omnipay\Stripe\Message\PaymentIntents\DetachPaymentMethodRequest', $parameters); } + + // Setup Intent + + /** + * @inheritdoc + * + * @return \Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest + */ + public function createSetupIntent(array $parameters = array()) + { + return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest', $parameters); + } } From 68b490362af2d794cb1b3c2a52f5e90a8ce8458b Mon Sep 17 00:00:00 2001 From: "Alexander (SASh) Alexiev" Date: Sat, 25 Apr 2020 09:12:17 +0300 Subject: [PATCH 2/4] add: restrieve setup intent and fix the setup intents response --- src/Message/SetupIntents/AbstractRequest.php | 4 +- src/Message/SetupIntents/Response.php | 16 +++++- .../RetrieveSetupIntentRequest.php | 54 +++++++++++++++++++ src/PaymentIntentsGateway.php | 9 ++++ 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/Message/SetupIntents/RetrieveSetupIntentRequest.php diff --git a/src/Message/SetupIntents/AbstractRequest.php b/src/Message/SetupIntents/AbstractRequest.php index 4ea498e7..c56b6dbe 100644 --- a/src/Message/SetupIntents/AbstractRequest.php +++ b/src/Message/SetupIntents/AbstractRequest.php @@ -25,7 +25,7 @@ abstract class AbstractRequest extends \Omnipay\Stripe\Message\AbstractRequest */ public function setSetupIntentReference($value) { - return $this->setParameter('paymentIntentReference', $value); + return $this->setParameter('setupIntentReference', $value); } /** @@ -33,7 +33,7 @@ public function setSetupIntentReference($value) */ public function getSetupIntentReference() { - return $this->getParameter('paymentIntentReference'); + return $this->getParameter('setupIntentReference'); } } diff --git a/src/Message/SetupIntents/Response.php b/src/Message/SetupIntents/Response.php index d9d30f67..bb643b34 100644 --- a/src/Message/SetupIntents/Response.php +++ b/src/Message/SetupIntents/Response.php @@ -74,7 +74,7 @@ public function getCustomerReference() public function isSuccessful() { if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) { - return in_array($this->getStatus(), ['succeeded', 'requires_capture']); + return in_array($this->getStatus(), ['succeeded', 'requires_payment_method']); } return parent::isSuccessful(); @@ -127,4 +127,18 @@ public function getSetupIntentReference() return null; } + + /** + * Get the payment intent reference. + * + * @return string|null + */ + public function getPaymentMethod() + { + if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) { + return $this->data['payment_method']; + } + + return null; + } } diff --git a/src/Message/SetupIntents/RetrieveSetupIntentRequest.php b/src/Message/SetupIntents/RetrieveSetupIntentRequest.php new file mode 100644 index 00000000..8f0df658 --- /dev/null +++ b/src/Message/SetupIntents/RetrieveSetupIntentRequest.php @@ -0,0 +1,54 @@ + + * + * + * + * @see \Omnipay\Stripe\Message\PaymentIntents\AttachPaymentMethodRequest + * @see \Omnipay\Stripe\Message\PaymentIntents\DetachPaymentMethodRequest + * @see \Omnipay\Stripe\Message\PaymentIntents\UpdatePaymentMethodRequest + * @link https://stripe.com/docs/api/setup_intents/create + */ +class RetrieveSetupIntentRequest extends AbstractRequest +{ + /** + * @inheritdoc + */ + public function getData() + { + $this->validate('setupIntentReference'); + + return []; + } + + /** + * @inheritdoc + */ + public function getEndpoint() + { + return $this->endpoint.'/setup_intents/'.$this->getSetupIntentReference(); + } + + public function getHttpMethod() + { + return 'GET'; + } + + /** + * @inheritdoc + */ + protected function createResponse($data, $headers = []) + { + return $this->response = new Response($this, $data, $headers); + } +} diff --git a/src/PaymentIntentsGateway.php b/src/PaymentIntentsGateway.php index a5ed27e2..c646d2be 100644 --- a/src/PaymentIntentsGateway.php +++ b/src/PaymentIntentsGateway.php @@ -178,4 +178,13 @@ public function createSetupIntent(array $parameters = array()) { return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest', $parameters); } + /** + * @inheritdoc + * + * @return \Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest + */ + public function retrieveSetupIntent(array $parameters = array()) + { + return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\RetrieveSetupIntentRequest', $parameters); + } } From 2e1355e661369a1e162c5369a354777ec7070d2c Mon Sep 17 00:00:00 2001 From: "Alexander (SASh) Alexiev" Date: Sat, 25 Apr 2020 19:24:12 +0300 Subject: [PATCH 3/4] add: off session option for payment intents --- .../PaymentIntents/AuthorizeRequest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Message/PaymentIntents/AuthorizeRequest.php b/src/Message/PaymentIntents/AuthorizeRequest.php index fe939d1e..5ac708f0 100644 --- a/src/Message/PaymentIntents/AuthorizeRequest.php +++ b/src/Message/PaymentIntents/AuthorizeRequest.php @@ -126,6 +126,26 @@ public function getConfirm() return $this->getParameter('confirm'); } + /** + * Set the confirm parameter. + * + * @param $value + */ + public function setOffSession($value) + { + $this->setParameter('offSession', $value); + } + + /** + * Get the confirm parameter. + * + * @return mixed + */ + public function getOffSession() + { + return $this->getParameter('offSession'); + } + /** * @return mixed */ @@ -352,6 +372,8 @@ public function getData() $this->validate('returnUrl'); $data['return_url'] = $this->getReturnUrl(); } + $data['off_session'] = $this->getOffSession() ? 'true' : 'false'; + return $data; } From 2c88b82b44ca3ae4ae54b067416ab2e79914be04 Mon Sep 17 00:00:00 2001 From: "Alexander (SASh) Alexiev" Date: Sat, 25 Apr 2020 19:34:00 +0300 Subject: [PATCH 4/4] red: formatting --- src/Message/SetupIntents/AbstractRequest.php | 1 - src/Message/SetupIntents/CreateSetupIntentRequest.php | 9 +++++---- src/Message/SetupIntents/Response.php | 3 ++- src/Message/SetupIntents/RetrieveSetupIntentRequest.php | 3 ++- src/PaymentIntentsGateway.php | 4 ++-- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Message/SetupIntents/AbstractRequest.php b/src/Message/SetupIntents/AbstractRequest.php index c56b6dbe..246fc1ac 100644 --- a/src/Message/SetupIntents/AbstractRequest.php +++ b/src/Message/SetupIntents/AbstractRequest.php @@ -35,5 +35,4 @@ public function getSetupIntentReference() { return $this->getParameter('setupIntentReference'); } - } diff --git a/src/Message/SetupIntents/CreateSetupIntentRequest.php b/src/Message/SetupIntents/CreateSetupIntentRequest.php index 951325b6..53eb08b0 100644 --- a/src/Message/SetupIntents/CreateSetupIntentRequest.php +++ b/src/Message/SetupIntents/CreateSetupIntentRequest.php @@ -3,6 +3,7 @@ /** * Stripe Create Payment Method Request. */ + namespace Omnipay\Stripe\Message\SetupIntents; /** @@ -31,14 +32,14 @@ public function getData() if ($this->getCustomerReference()) { $data['customer'] = $this->getCustomerReference(); } - if ($this->getDescription()){ + if ($this->getDescription()) { $data['description'] = $this->getDescription(); } - if ($this->getMetadata()){ + if ($this->getMetadata()) { $this['metadata'] = $this->getMetadata(); } - if ($this->getPaymentMethod()){ + if ($this->getPaymentMethod()) { $this['payment_method'] = $this->getPaymentMethod(); } @@ -53,7 +54,7 @@ public function getData() */ public function getEndpoint() { - return $this->endpoint.'/setup_intents'; + return $this->endpoint . '/setup_intents'; } /** diff --git a/src/Message/SetupIntents/Response.php b/src/Message/SetupIntents/Response.php index bb643b34..47cc3007 100644 --- a/src/Message/SetupIntents/Response.php +++ b/src/Message/SetupIntents/Response.php @@ -3,10 +3,11 @@ /** * Stripe Payment Intents Response. */ + namespace Omnipay\Stripe\Message\SetupIntents; use Omnipay\Common\Message\ResponseInterface; -use Omnipay\Stripe\Message\Response as BaseResponse;; +use Omnipay\Stripe\Message\Response as BaseResponse; /** * Stripe Payment Intents Response. diff --git a/src/Message/SetupIntents/RetrieveSetupIntentRequest.php b/src/Message/SetupIntents/RetrieveSetupIntentRequest.php index 8f0df658..ab465bd4 100644 --- a/src/Message/SetupIntents/RetrieveSetupIntentRequest.php +++ b/src/Message/SetupIntents/RetrieveSetupIntentRequest.php @@ -3,6 +3,7 @@ /** * Stripe Create Payment Method Request. */ + namespace Omnipay\Stripe\Message\SetupIntents; /** @@ -36,7 +37,7 @@ public function getData() */ public function getEndpoint() { - return $this->endpoint.'/setup_intents/'.$this->getSetupIntentReference(); + return $this->endpoint . '/setup_intents/' . $this->getSetupIntentReference(); } public function getHttpMethod() diff --git a/src/PaymentIntentsGateway.php b/src/PaymentIntentsGateway.php index c646d2be..e6ecea2f 100644 --- a/src/PaymentIntentsGateway.php +++ b/src/PaymentIntentsGateway.php @@ -3,9 +3,8 @@ /** * Stripe Payment Intents Gateway. */ -namespace Omnipay\Stripe; -use Omnipay\Stripe\Message\PaymentIntents\Response; +namespace Omnipay\Stripe; /** * Stripe Payment Intents Gateway. @@ -178,6 +177,7 @@ public function createSetupIntent(array $parameters = array()) { return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest', $parameters); } + /** * @inheritdoc *