forked from thephpleague/omnipay-stripe
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Checkout Gateway (thephpleague#202)
* add Checkout gateway * Update src/Message/Checkout/PurchaseRequest.php Co-authored-by: Sandra Kuipers <[email protected]> Co-authored-by: yepzy <[email protected]> Co-authored-by: Barry vd. Heuvel <[email protected]> Co-authored-by: Sandra Kuipers <[email protected]>
- Loading branch information
1 parent
fbf57b3
commit 16f822d
Showing
5 changed files
with
334 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Payment Intents Gateway. | ||
*/ | ||
|
||
namespace Omnipay\Stripe; | ||
|
||
/** | ||
* Stripe Payment Intents Gateway. | ||
* | ||
* @see \Omnipay\Stripe\AbstractGateway | ||
* @see \Omnipay\Stripe\Message\AbstractRequest | ||
* @link https://stripe.com/docs/api | ||
* @method \Omnipay\Common\Message\NotificationInterface acceptNotification(array $options = array()) | ||
* @method \Omnipay\Common\Message\RequestInterface refund(array $options = array()) | ||
* @method \Omnipay\Common\Message\RequestInterface void(array $options = array()) | ||
*/ | ||
class CheckoutGateway extends AbstractGateway | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getName() | ||
{ | ||
return 'Stripe Checkout'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @return \Omnipay\Stripe\Message\Checkout\PurchaseRequest | ||
*/ | ||
public function purchase(array $parameters = array()) | ||
{ | ||
return $this->createRequest('\Omnipay\Stripe\Message\Checkout\PurchaseRequest', $parameters); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @return \Omnipay\Stripe\Message\Checkout\PurchaseRequest | ||
*/ | ||
public function fetchTransaction(array $parameters = array()) | ||
{ | ||
return $this->createRequest('\Omnipay\Stripe\Message\Checkout\FetchTransactionRequest', $parameters); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* @return \Omnipay\Stripe\Message\AuthorizeRequest | ||
*/ | ||
public function authorize(array $parameters = array()) | ||
{ | ||
return $this->createRequest('\Omnipay\Stripe\Message\AuthorizeRequest', $parameters); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* @return \Omnipay\Stripe\Message\CaptureRequest | ||
*/ | ||
public function capture(array $parameters = array()) | ||
{ | ||
return $this->createRequest('\Omnipay\Stripe\Message\CaptureRequest', $parameters); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Abstract Request. | ||
*/ | ||
|
||
namespace Omnipay\Stripe\Message\Checkout; | ||
|
||
/** | ||
* 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 | ||
{ | ||
} |
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,47 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Fetch Transaction Request. | ||
*/ | ||
|
||
namespace Omnipay\Stripe\Message\Checkout; | ||
|
||
/** | ||
* Stripe Fetch Transaction Request. | ||
* Example -- note this example assumes that the purchase has been successful | ||
* and that the transaction ID returned from the purchase is held in $sale_id. | ||
* See PurchaseRequest for the first part of this example transaction: | ||
* <code> | ||
* // Fetch the transaction so that details can be found for refund, etc. | ||
* $transaction = $gateway->fetchTransaction(); | ||
* $transaction->setTransactionReference($sale_id); | ||
* $response = $transaction->send(); | ||
* $data = $response->getData(); | ||
* echo "Gateway fetchTransaction response data == " . print_r($data, true) . "\n"; | ||
* </code> | ||
* | ||
* @see PurchaseRequest | ||
* @see Omnipay\Stripe\CheckoutGateway | ||
* @link https://stripe.com/docs/api/checkout/sessions/retrieve | ||
*/ | ||
class FetchTransactionRequest extends AbstractRequest | ||
{ | ||
public function getData() | ||
{ | ||
$this->validate('transactionReference'); | ||
|
||
$data = []; | ||
|
||
return $data; | ||
} | ||
|
||
public function getEndpoint() | ||
{ | ||
return $this->endpoint.'/checkout/sessions/'. $this->getTransactionReference(); | ||
} | ||
|
||
public function getHttpMethod() | ||
{ | ||
return 'GET'; | ||
} | ||
} |
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,166 @@ | ||
<?php | ||
|
||
/** | ||
* Stripe Checkout Session Request. | ||
*/ | ||
|
||
namespace Omnipay\Stripe\Message\Checkout; | ||
|
||
/** | ||
* Stripe Checkout Session Request | ||
* | ||
* @see \Omnipay\Stripe\Gateway | ||
* @link https://stripe.com/docs/api/checkout/sessions | ||
*/ | ||
class PurchaseRequest extends AbstractRequest | ||
{ | ||
/** | ||
* Set the success url | ||
* | ||
* @param string $value | ||
* | ||
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest | ||
*/ | ||
public function setSuccessUrl($value) | ||
{ | ||
return $this->setParameter('success_url', $value); | ||
} | ||
|
||
/** | ||
* Get the success url | ||
* | ||
* @return string | ||
*/ | ||
public function getSuccessUrl() | ||
{ | ||
return $this->getParameter('success_url'); | ||
} | ||
/** | ||
* Set the cancel url | ||
* | ||
* @param string $value | ||
* | ||
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest | ||
*/ | ||
public function setCancelUrl($value) | ||
{ | ||
return $this->setParameter('cancel_url', $value); | ||
} | ||
|
||
/** | ||
* Get the success url | ||
* | ||
* @return string | ||
*/ | ||
public function getCancelUrl() | ||
{ | ||
return $this->getParameter('cancel_url'); | ||
} | ||
|
||
/** | ||
* Set the payment method types accepted url | ||
* | ||
* @param array $value | ||
* | ||
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest | ||
*/ | ||
public function setPaymentMethodTypes($value) | ||
{ | ||
return $this->setParameter('payment_method_types', $value); | ||
} | ||
|
||
/** | ||
* Get the success url | ||
* | ||
* @return string | ||
*/ | ||
public function getPaymentMethodTypes() | ||
{ | ||
return $this->getParameter('payment_method_types'); | ||
} | ||
|
||
/** | ||
* Set the payment method types accepted url | ||
* | ||
* @param string $value | ||
* | ||
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest | ||
*/ | ||
public function setMode($value) | ||
{ | ||
return $this->setParameter('mode', $value); | ||
} | ||
|
||
/** | ||
* Get the success url | ||
* | ||
* @return string | ||
*/ | ||
public function getMode() | ||
{ | ||
return $this->getParameter('mode'); | ||
} | ||
|
||
/** | ||
* Set the payment method types accepted url | ||
* | ||
* @param array $value | ||
* | ||
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest | ||
*/ | ||
public function setLineItems($value) | ||
{ | ||
return $this->setParameter('line_items', $value); | ||
} | ||
|
||
/** | ||
* Get the success url | ||
* | ||
* @return array | ||
*/ | ||
public function getLineItems() | ||
{ | ||
return $this->getParameter('line_items'); | ||
} | ||
|
||
/** | ||
* Set the payment method types accepted url | ||
* | ||
* @param string $value | ||
* | ||
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest | ||
*/ | ||
public function setClientReferenceId($value) | ||
{ | ||
return $this->setParameter('client_reference_id', $value); | ||
} | ||
|
||
/** | ||
* Get the success url | ||
* | ||
* @return string | ||
*/ | ||
public function getClientReferenceId() | ||
{ | ||
return $this->getParameter('client_reference_id'); | ||
} | ||
|
||
|
||
public function getData() | ||
{ | ||
$data = array( | ||
'success_url' => $this->getSuccessUrl(), | ||
'cancel_url' => $this->getCancelUrl(), | ||
'payment_method_types' => $this->getPaymentMethodTypes(), | ||
'mode' => $this->getMode(), | ||
'line_items' => $this->getLineItems() | ||
); | ||
|
||
return $data; | ||
} | ||
|
||
public function getEndpoint() | ||
{ | ||
return $this->endpoint.'/checkout/sessions'; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Omnipay\Stripe; | ||
|
||
use Omnipay\Tests\GatewayTestCase; | ||
|
||
/** | ||
* @property \Omnipay\Stripe\CheckoutGateway gateway | ||
*/ | ||
class CheckoutGatewayTest extends GatewayTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->gateway = new CheckoutGateway($this->getHttpClient(), $this->getHttpRequest()); | ||
} | ||
|
||
public function testPurchase() | ||
{ | ||
$request = $this->gateway->purchase(['mode' => 'payment']); | ||
|
||
$this->assertInstanceOf('Omnipay\Stripe\Message\Checkout\PurchaseRequest', $request); | ||
$this->assertSame('payment', $request->getMode()); | ||
} | ||
|
||
public function testFetchTransaction() | ||
{ | ||
$request = $this->gateway->fetchTransaction(['transactionReference' => 'transaction-reference']); | ||
|
||
$this->assertInstanceOf('Omnipay\Stripe\Message\Checkout\FetchTransactionRequest', $request); | ||
$this->assertSame('transaction-reference', $request->getTransactionReference()); | ||
} | ||
} |