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; } diff --git a/src/Message/SetupIntents/AbstractRequest.php b/src/Message/SetupIntents/AbstractRequest.php new file mode 100644 index 00000000..246fc1ac --- /dev/null +++ b/src/Message/SetupIntents/AbstractRequest.php @@ -0,0 +1,38 @@ +setParameter('setupIntentReference', $value); + } + + /** + * @return mixed + */ + public function getSetupIntentReference() + { + return $this->getParameter('setupIntentReference'); + } +} diff --git a/src/Message/SetupIntents/CreateSetupIntentRequest.php b/src/Message/SetupIntents/CreateSetupIntentRequest.php new file mode 100644 index 00000000..53eb08b0 --- /dev/null +++ b/src/Message/SetupIntents/CreateSetupIntentRequest.php @@ -0,0 +1,67 @@ + + * + * + * + * @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..47cc3007 --- /dev/null +++ b/src/Message/SetupIntents/Response.php @@ -0,0 +1,145 @@ +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_payment_method']); + } + + 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; + } + + /** + * 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..ab465bd4 --- /dev/null +++ b/src/Message/SetupIntents/RetrieveSetupIntentRequest.php @@ -0,0 +1,55 @@ + + * + * + * + * @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 88e24571..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. @@ -166,4 +165,26 @@ 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); + } + + /** + * @inheritdoc + * + * @return \Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest + */ + public function retrieveSetupIntent(array $parameters = array()) + { + return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\RetrieveSetupIntentRequest', $parameters); + } }