Skip to content

Commit 23f998e

Browse files
author
Alexander (SASh) Alexiev
committed
add: create setup intent request
1 parent 1b27d76 commit 23f998e

File tree

4 files changed

+247
-0
lines changed

4 files changed

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

src/PaymentIntentsGateway.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,16 @@ public function deleteCard(array $parameters = array())
166166
{
167167
return $this->createRequest('\Omnipay\Stripe\Message\PaymentIntents\DetachPaymentMethodRequest', $parameters);
168168
}
169+
170+
// Setup Intent
171+
172+
/**
173+
* @inheritdoc
174+
*
175+
* @return \Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest
176+
*/
177+
public function createSetupIntent(array $parameters = array())
178+
{
179+
return $this->createRequest('\Omnipay\Stripe\Message\SetupIntents\CreateSetupIntentRequest', $parameters);
180+
}
169181
}

0 commit comments

Comments
 (0)