Skip to content

Commit

Permalink
Implement Checkout Gateway (thephpleague#202)
Browse files Browse the repository at this point in the history
* 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
4 people authored Aug 8, 2021
1 parent fbf57b3 commit 16f822d
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/CheckoutGateway.php
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);
}
}
21 changes: 21 additions & 0 deletions src/Message/Checkout/AbstractRequest.php
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
{
}
47 changes: 47 additions & 0 deletions src/Message/Checkout/FetchTransactionRequest.php
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';
}
}
166 changes: 166 additions & 0 deletions src/Message/Checkout/PurchaseRequest.php
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';
}
}
34 changes: 34 additions & 0 deletions tests/CheckoutGatewayTest.php
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());
}
}

0 comments on commit 16f822d

Please sign in to comment.