Skip to content

Commit a91faeb

Browse files
authored
Merge pull request #177 from ampeco/master
Setup Intent implementation & off_session payments for Payment Intent
2 parents 56c5735 + 2c88b82 commit a91faeb

File tree

6 files changed

+350
-2
lines changed

6 files changed

+350
-2
lines changed

src/Message/PaymentIntents/AuthorizeRequest.php

+22
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ public function getConfirm()
127127
return $this->getParameter('confirm');
128128
}
129129

130+
/**
131+
* Set the confirm parameter.
132+
*
133+
* @param $value
134+
*/
135+
public function setOffSession($value)
136+
{
137+
$this->setParameter('offSession', $value);
138+
}
139+
140+
/**
141+
* Get the confirm parameter.
142+
*
143+
* @return mixed
144+
*/
145+
public function getOffSession()
146+
{
147+
return $this->getParameter('offSession');
148+
}
149+
130150
/**
131151
* @return mixed
132152
*/
@@ -401,6 +421,8 @@ public function getData()
401421
$this->validate('returnUrl');
402422
$data['return_url'] = $this->getReturnUrl();
403423
}
424+
$data['off_session'] = $this->getOffSession() ? 'true' : 'false';
425+
404426

405427
return $data;
406428
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* Stripe Abstract Request.
5+
*/
6+
7+
namespace Omnipay\Stripe\Message\SetupIntents;
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+
/**
22+
* @param string $value
23+
*
24+
* @return AbstractRequest provides a fluent interface.
25+
*/
26+
public function setSetupIntentReference($value)
27+
{
28+
return $this->setParameter('setupIntentReference', $value);
29+
}
30+
31+
/**
32+
* @return mixed
33+
*/
34+
public function getSetupIntentReference()
35+
{
36+
return $this->getParameter('setupIntentReference');
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* Stripe Create Payment Method Request.
5+
*/
6+
7+
namespace Omnipay\Stripe\Message\SetupIntents;
8+
9+
/**
10+
* Stripe create setup intent
11+
*
12+
* ### Example
13+
*
14+
* <code>
15+
*
16+
* </code>
17+
*
18+
* @see \Omnipay\Stripe\Message\PaymentIntents\AttachPaymentMethodRequest
19+
* @see \Omnipay\Stripe\Message\PaymentIntents\DetachPaymentMethodRequest
20+
* @see \Omnipay\Stripe\Message\PaymentIntents\UpdatePaymentMethodRequest
21+
* @link https://stripe.com/docs/api/setup_intents/create
22+
*/
23+
class CreateSetupIntentRequest extends AbstractRequest
24+
{
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function getData()
29+
{
30+
$data = [];
31+
32+
if ($this->getCustomerReference()) {
33+
$data['customer'] = $this->getCustomerReference();
34+
}
35+
if ($this->getDescription()) {
36+
$data['description'] = $this->getDescription();
37+
}
38+
39+
if ($this->getMetadata()) {
40+
$this['metadata'] = $this->getMetadata();
41+
}
42+
if ($this->getPaymentMethod()) {
43+
$this['payment_method'] = $this->getPaymentMethod();
44+
}
45+
46+
$data['usage'] = 'off_session';
47+
$data['payment_method_types'][] = 'card';
48+
49+
return $data;
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function getEndpoint()
56+
{
57+
return $this->endpoint . '/setup_intents';
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
protected function createResponse($data, $headers = [])
64+
{
65+
return $this->response = new Response($this, $data, $headers);
66+
}
67+
}

src/Message/SetupIntents/Response.php

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/**
4+
* Stripe Payment Intents Response.
5+
*/
6+
7+
namespace Omnipay\Stripe\Message\SetupIntents;
8+
9+
use Omnipay\Common\Message\ResponseInterface;
10+
use Omnipay\Stripe\Message\Response as BaseResponse;
11+
12+
/**
13+
* Stripe Payment Intents Response.
14+
*
15+
* This is the response class for all payment intents related responses.
16+
*
17+
* @see \Omnipay\Stripe\PaymentIntentsGateway
18+
*/
19+
class Response extends BaseResponse implements ResponseInterface
20+
{
21+
/**
22+
* Get the status of a payment intents response.
23+
*
24+
* @return string|null
25+
*/
26+
public function getStatus()
27+
{
28+
if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) {
29+
return $this->data['status'];
30+
}
31+
32+
return null;
33+
}
34+
35+
/**
36+
* Return true if the payment intent requires confirmation.
37+
*
38+
* @return bool
39+
*/
40+
public function requiresConfirmation()
41+
{
42+
return $this->getStatus() === 'requires_confirmation';
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function getClientSecret()
49+
{
50+
if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) {
51+
if (!empty($this->data['client_secret'])) {
52+
return $this->data['client_secret'];
53+
}
54+
}
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function getCustomerReference()
61+
{
62+
63+
if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) {
64+
if (!empty($this->data['customer'])) {
65+
return $this->data['customer'];
66+
}
67+
}
68+
69+
return parent::getCustomerReference();
70+
}
71+
72+
/**
73+
* @inheritdoc
74+
*/
75+
public function isSuccessful()
76+
{
77+
if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) {
78+
return in_array($this->getStatus(), ['succeeded', 'requires_payment_method']);
79+
}
80+
81+
return parent::isSuccessful();
82+
}
83+
84+
/**
85+
* @inheritdoc
86+
*/
87+
public function isCancelled()
88+
{
89+
if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) {
90+
return $this->getStatus() === 'canceled';
91+
}
92+
93+
return parent::isCancelled();
94+
}
95+
96+
/**
97+
* @inheritdoc
98+
*/
99+
public function isRedirect()
100+
{
101+
if ($this->getStatus() === 'requires_action' || $this->getStatus() === 'requires_source_action') {
102+
// Currently this gateway supports only manual confirmation, so any other
103+
// next action types pretty much mean a failed transaction for us.
104+
return (!empty($this->data['next_action']) && $this->data['next_action']['type'] === 'redirect_to_url');
105+
}
106+
107+
return parent::isRedirect();
108+
}
109+
110+
/**
111+
* @inheritdoc
112+
*/
113+
public function getRedirectUrl()
114+
{
115+
return $this->isRedirect() ? $this->data['next_action']['redirect_to_url']['url'] : parent::getRedirectUrl();
116+
}
117+
118+
/**
119+
* Get the payment intent reference.
120+
*
121+
* @return string|null
122+
*/
123+
public function getSetupIntentReference()
124+
{
125+
if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) {
126+
return $this->data['id'];
127+
}
128+
129+
return null;
130+
}
131+
132+
/**
133+
* Get the payment intent reference.
134+
*
135+
* @return string|null
136+
*/
137+
public function getPaymentMethod()
138+
{
139+
if (isset($this->data['object']) && 'setup_intent' === $this->data['object']) {
140+
return $this->data['payment_method'];
141+
}
142+
143+
return null;
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Stripe Create Payment Method Request.
5+
*/
6+
7+
namespace Omnipay\Stripe\Message\SetupIntents;
8+
9+
/**
10+
* Stripe create setup intent
11+
*
12+
* ### Example
13+
*
14+
* <code>
15+
*
16+
* </code>
17+
*
18+
* @see \Omnipay\Stripe\Message\PaymentIntents\AttachPaymentMethodRequest
19+
* @see \Omnipay\Stripe\Message\PaymentIntents\DetachPaymentMethodRequest
20+
* @see \Omnipay\Stripe\Message\PaymentIntents\UpdatePaymentMethodRequest
21+
* @link https://stripe.com/docs/api/setup_intents/create
22+
*/
23+
class RetrieveSetupIntentRequest extends AbstractRequest
24+
{
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function getData()
29+
{
30+
$this->validate('setupIntentReference');
31+
32+
return [];
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function getEndpoint()
39+
{
40+
return $this->endpoint . '/setup_intents/' . $this->getSetupIntentReference();
41+
}
42+
43+
public function getHttpMethod()
44+
{
45+
return 'GET';
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
protected function createResponse($data, $headers = [])
52+
{
53+
return $this->response = new Response($this, $data, $headers);
54+
}
55+
}

src/PaymentIntentsGateway.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
/**
44
* Stripe Payment Intents Gateway.
55
*/
6-
namespace Omnipay\Stripe;
76

8-
use Omnipay\Stripe\Message\PaymentIntents\Response;
7+
namespace Omnipay\Stripe;
98

109
/**
1110
* Stripe Payment Intents Gateway.
@@ -166,4 +165,26 @@ public function deleteCard(array $parameters = array())
166165
{
167166
return $this->createRequest('\Omnipay\Stripe\Message\PaymentIntents\DetachPaymentMethodRequest', $parameters);
168167
}
168+
169+
// Setup Intent
170+
171+
/**
172+
* @inheritdoc
173+
*
174+
* @return \Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest
175+
*/
176+
public function createSetupIntent(array $parameters = array())
177+
{
178+
return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest', $parameters);
179+
}
180+
181+
/**
182+
* @inheritdoc
183+
*
184+
* @return \Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest
185+
*/
186+
public function retrieveSetupIntent(array $parameters = array())
187+
{
188+
return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\RetrieveSetupIntentRequest', $parameters);
189+
}
169190
}

0 commit comments

Comments
 (0)