-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from ampeco/master
Setup Intent implementation & off_session payments for Payment Intent
- Loading branch information
Showing
6 changed files
with
350 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Abstract Request. | ||
*/ | ||
|
||
namespace Omnipay\Stripe\Message\SetupIntents; | ||
|
||
/** | ||
* Stripe Payment Intent Abstract Request. | ||
* | ||
* This is the parent class for all Stripe payment intent requests. | ||
* It adds just a getter and setter. | ||
* | ||
* @see \Omnipay\Stripe\PaymentIntentsGateway | ||
* @see \Omnipay\Stripe\Message\AbstractRequest | ||
* @link https://stripe.com/docs/api/payment_intents | ||
*/ | ||
abstract class AbstractRequest extends \Omnipay\Stripe\Message\AbstractRequest | ||
{ | ||
/** | ||
* @param string $value | ||
* | ||
* @return AbstractRequest provides a fluent interface. | ||
*/ | ||
public function setSetupIntentReference($value) | ||
{ | ||
return $this->setParameter('setupIntentReference', $value); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getSetupIntentReference() | ||
{ | ||
return $this->getParameter('setupIntentReference'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Create Payment Method Request. | ||
*/ | ||
|
||
namespace Omnipay\Stripe\Message\SetupIntents; | ||
|
||
/** | ||
* Stripe create setup intent | ||
* | ||
* ### Example | ||
* | ||
* <code> | ||
* | ||
* </code> | ||
* | ||
* @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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Payment Intents Response. | ||
*/ | ||
|
||
namespace Omnipay\Stripe\Message\SetupIntents; | ||
|
||
use Omnipay\Common\Message\ResponseInterface; | ||
use Omnipay\Stripe\Message\Response as BaseResponse; | ||
|
||
/** | ||
* Stripe Payment Intents Response. | ||
* | ||
* This is the response class for all payment intents related responses. | ||
* | ||
* @see \Omnipay\Stripe\PaymentIntentsGateway | ||
*/ | ||
class Response extends BaseResponse implements ResponseInterface | ||
{ | ||
/** | ||
* Get the status of a payment intents response. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getStatus() | ||
{ | ||
if (isset($this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Create Payment Method Request. | ||
*/ | ||
|
||
namespace Omnipay\Stripe\Message\SetupIntents; | ||
|
||
/** | ||
* Stripe create setup intent | ||
* | ||
* ### Example | ||
* | ||
* <code> | ||
* | ||
* </code> | ||
* | ||
* @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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters