Skip to content

Commit 16f822d

Browse files
yepzyyepzybarryvdhSKuipers
authored
Implement Checkout Gateway (#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]>
1 parent fbf57b3 commit 16f822d

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

src/CheckoutGateway.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Stripe Payment Intents Gateway.
5+
*/
6+
7+
namespace Omnipay\Stripe;
8+
9+
/**
10+
* Stripe Payment Intents Gateway.
11+
*
12+
* @see \Omnipay\Stripe\AbstractGateway
13+
* @see \Omnipay\Stripe\Message\AbstractRequest
14+
* @link https://stripe.com/docs/api
15+
* @method \Omnipay\Common\Message\NotificationInterface acceptNotification(array $options = array())
16+
* @method \Omnipay\Common\Message\RequestInterface refund(array $options = array())
17+
* @method \Omnipay\Common\Message\RequestInterface void(array $options = array())
18+
*/
19+
class CheckoutGateway extends AbstractGateway
20+
{
21+
/**
22+
* @inheritdoc
23+
*/
24+
public function getName()
25+
{
26+
return 'Stripe Checkout';
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
* @return \Omnipay\Stripe\Message\Checkout\PurchaseRequest
32+
*/
33+
public function purchase(array $parameters = array())
34+
{
35+
return $this->createRequest('\Omnipay\Stripe\Message\Checkout\PurchaseRequest', $parameters);
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
* @return \Omnipay\Stripe\Message\Checkout\PurchaseRequest
41+
*/
42+
public function fetchTransaction(array $parameters = array())
43+
{
44+
return $this->createRequest('\Omnipay\Stripe\Message\Checkout\FetchTransactionRequest', $parameters);
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*
50+
* @return \Omnipay\Stripe\Message\AuthorizeRequest
51+
*/
52+
public function authorize(array $parameters = array())
53+
{
54+
return $this->createRequest('\Omnipay\Stripe\Message\AuthorizeRequest', $parameters);
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*
60+
* @return \Omnipay\Stripe\Message\CaptureRequest
61+
*/
62+
public function capture(array $parameters = array())
63+
{
64+
return $this->createRequest('\Omnipay\Stripe\Message\CaptureRequest', $parameters);
65+
}
66+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Stripe Abstract Request.
5+
*/
6+
7+
namespace Omnipay\Stripe\Message\Checkout;
8+
9+
/**
10+
* Stripe Payment Intent Abstract Request.
11+
*
12+
* This is the parent class for all Stripe payment intent requests.
13+
* It adds just a getter and setter.
14+
*
15+
* @see \Omnipay\Stripe\PaymentIntentsGateway
16+
* @see \Omnipay\Stripe\Message\AbstractRequest
17+
* @link https://stripe.com/docs/api/payment_intents
18+
*/
19+
abstract class AbstractRequest extends \Omnipay\Stripe\Message\AbstractRequest
20+
{
21+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Stripe Fetch Transaction Request.
5+
*/
6+
7+
namespace Omnipay\Stripe\Message\Checkout;
8+
9+
/**
10+
* Stripe Fetch Transaction Request.
11+
* Example -- note this example assumes that the purchase has been successful
12+
* and that the transaction ID returned from the purchase is held in $sale_id.
13+
* See PurchaseRequest for the first part of this example transaction:
14+
* <code>
15+
* // Fetch the transaction so that details can be found for refund, etc.
16+
* $transaction = $gateway->fetchTransaction();
17+
* $transaction->setTransactionReference($sale_id);
18+
* $response = $transaction->send();
19+
* $data = $response->getData();
20+
* echo "Gateway fetchTransaction response data == " . print_r($data, true) . "\n";
21+
* </code>
22+
*
23+
* @see PurchaseRequest
24+
* @see Omnipay\Stripe\CheckoutGateway
25+
* @link https://stripe.com/docs/api/checkout/sessions/retrieve
26+
*/
27+
class FetchTransactionRequest extends AbstractRequest
28+
{
29+
public function getData()
30+
{
31+
$this->validate('transactionReference');
32+
33+
$data = [];
34+
35+
return $data;
36+
}
37+
38+
public function getEndpoint()
39+
{
40+
return $this->endpoint.'/checkout/sessions/'. $this->getTransactionReference();
41+
}
42+
43+
public function getHttpMethod()
44+
{
45+
return 'GET';
46+
}
47+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
/**
4+
* Stripe Checkout Session Request.
5+
*/
6+
7+
namespace Omnipay\Stripe\Message\Checkout;
8+
9+
/**
10+
* Stripe Checkout Session Request
11+
*
12+
* @see \Omnipay\Stripe\Gateway
13+
* @link https://stripe.com/docs/api/checkout/sessions
14+
*/
15+
class PurchaseRequest extends AbstractRequest
16+
{
17+
/**
18+
* Set the success url
19+
*
20+
* @param string $value
21+
*
22+
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest
23+
*/
24+
public function setSuccessUrl($value)
25+
{
26+
return $this->setParameter('success_url', $value);
27+
}
28+
29+
/**
30+
* Get the success url
31+
*
32+
* @return string
33+
*/
34+
public function getSuccessUrl()
35+
{
36+
return $this->getParameter('success_url');
37+
}
38+
/**
39+
* Set the cancel url
40+
*
41+
* @param string $value
42+
*
43+
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest
44+
*/
45+
public function setCancelUrl($value)
46+
{
47+
return $this->setParameter('cancel_url', $value);
48+
}
49+
50+
/**
51+
* Get the success url
52+
*
53+
* @return string
54+
*/
55+
public function getCancelUrl()
56+
{
57+
return $this->getParameter('cancel_url');
58+
}
59+
60+
/**
61+
* Set the payment method types accepted url
62+
*
63+
* @param array $value
64+
*
65+
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest
66+
*/
67+
public function setPaymentMethodTypes($value)
68+
{
69+
return $this->setParameter('payment_method_types', $value);
70+
}
71+
72+
/**
73+
* Get the success url
74+
*
75+
* @return string
76+
*/
77+
public function getPaymentMethodTypes()
78+
{
79+
return $this->getParameter('payment_method_types');
80+
}
81+
82+
/**
83+
* Set the payment method types accepted url
84+
*
85+
* @param string $value
86+
*
87+
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest
88+
*/
89+
public function setMode($value)
90+
{
91+
return $this->setParameter('mode', $value);
92+
}
93+
94+
/**
95+
* Get the success url
96+
*
97+
* @return string
98+
*/
99+
public function getMode()
100+
{
101+
return $this->getParameter('mode');
102+
}
103+
104+
/**
105+
* Set the payment method types accepted url
106+
*
107+
* @param array $value
108+
*
109+
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest
110+
*/
111+
public function setLineItems($value)
112+
{
113+
return $this->setParameter('line_items', $value);
114+
}
115+
116+
/**
117+
* Get the success url
118+
*
119+
* @return array
120+
*/
121+
public function getLineItems()
122+
{
123+
return $this->getParameter('line_items');
124+
}
125+
126+
/**
127+
* Set the payment method types accepted url
128+
*
129+
* @param string $value
130+
*
131+
* @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest
132+
*/
133+
public function setClientReferenceId($value)
134+
{
135+
return $this->setParameter('client_reference_id', $value);
136+
}
137+
138+
/**
139+
* Get the success url
140+
*
141+
* @return string
142+
*/
143+
public function getClientReferenceId()
144+
{
145+
return $this->getParameter('client_reference_id');
146+
}
147+
148+
149+
public function getData()
150+
{
151+
$data = array(
152+
'success_url' => $this->getSuccessUrl(),
153+
'cancel_url' => $this->getCancelUrl(),
154+
'payment_method_types' => $this->getPaymentMethodTypes(),
155+
'mode' => $this->getMode(),
156+
'line_items' => $this->getLineItems()
157+
);
158+
159+
return $data;
160+
}
161+
162+
public function getEndpoint()
163+
{
164+
return $this->endpoint.'/checkout/sessions';
165+
}
166+
}

tests/CheckoutGatewayTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Omnipay\Stripe;
4+
5+
use Omnipay\Tests\GatewayTestCase;
6+
7+
/**
8+
* @property \Omnipay\Stripe\CheckoutGateway gateway
9+
*/
10+
class CheckoutGatewayTest extends GatewayTestCase
11+
{
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->gateway = new CheckoutGateway($this->getHttpClient(), $this->getHttpRequest());
17+
}
18+
19+
public function testPurchase()
20+
{
21+
$request = $this->gateway->purchase(['mode' => 'payment']);
22+
23+
$this->assertInstanceOf('Omnipay\Stripe\Message\Checkout\PurchaseRequest', $request);
24+
$this->assertSame('payment', $request->getMode());
25+
}
26+
27+
public function testFetchTransaction()
28+
{
29+
$request = $this->gateway->fetchTransaction(['transactionReference' => 'transaction-reference']);
30+
31+
$this->assertInstanceOf('Omnipay\Stripe\Message\Checkout\FetchTransactionRequest', $request);
32+
$this->assertSame('transaction-reference', $request->getTransactionReference());
33+
}
34+
}

0 commit comments

Comments
 (0)