|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Stripe\Message; |
|
4
|
|
|
|
|
5
|
|
|
use Omnipay\Common\ItemBag; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class PurchaseResponse |
|
9
|
|
|
* @package ByTIC\Payments\Stripe\Message |
|
10
|
|
|
* |
|
11
|
|
|
* @method PurchaseResponse send() |
|
12
|
|
|
*/ |
|
13
|
|
|
class PurchaseRequest extends \Omnipay\Stripe\Message\PaymentIntents\PurchaseRequest |
|
14
|
|
|
{ |
|
15
|
1 |
|
use Traits\HasKeysTrait; |
|
16
|
|
|
|
|
17
|
1 |
|
private function nullIfEmpty(string $value = null): ?string |
|
18
|
|
|
{ |
|
19
|
|
|
return empty($value) ? null : $value; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get the raw data array for this message. The format of this varies from gateway to |
|
24
|
|
|
* gateway, but will usually be either an associative array, or a SimpleXMLElement. |
|
25
|
|
|
* |
|
26
|
1 |
|
* @return mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getData() |
|
29
|
1 |
|
{ |
|
30
|
|
|
// Just validate the parameters. |
|
31
|
1 |
|
$this->validate('apiKey', 'transactionId', 'returnUrl'); |
|
32
|
1 |
|
|
|
33
|
|
|
if (empty($this->getCancelUrl())) { |
|
34
|
|
|
$this->setParameter('cancelUrl', $this->getReturnUrl()); |
|
35
|
|
|
} |
|
36
|
1 |
|
|
|
37
|
|
|
$data = [ |
|
38
|
|
|
'client_reference_id' => $this->getTransactionId(), |
|
39
|
1 |
|
'payment_method_types' => ['card'], |
|
40
|
|
|
'payment_intent_data' => [ |
|
41
|
1 |
|
'description' => $this->getDescription(), |
|
42
|
1 |
|
], |
|
43
|
|
|
'success_url' => $this->getReturnUrl(), |
|
44
|
|
|
'cancel_url' => $this->getCancelUrl(), |
|
45
|
1 |
|
]; |
|
46
|
1 |
|
|
|
47
|
|
|
if ($this->getApplicationFee()) { |
|
48
|
|
|
$data['payment_intent_data']['application_fee_amount'] = $this->getApplicationFeeInteger(); |
|
49
|
1 |
|
} |
|
50
|
1 |
|
|
|
51
|
1 |
|
foreach (['success_url', 'cancel_url'] as $type) { |
|
52
|
|
|
$data[$type] .= (parse_url($data[$type], PHP_URL_QUERY) ? '&' : '?') . 'stpsid={CHECKOUT_SESSION_ID}'; |
|
53
|
|
|
} |
|
54
|
1 |
|
|
|
55
|
1 |
|
$items = $this->getItems(); |
|
56
|
1 |
|
if (!($items instanceof ItemBag)) { |
|
57
|
1 |
|
$items = new ItemBag( |
|
58
|
1 |
|
[ |
|
59
|
1 |
|
[ |
|
60
|
|
|
'prodid' => $this->getTransactionId(), |
|
61
|
|
|
'name' => $this->getDescription(), |
|
62
|
|
|
'description' => $this->getDescription(), |
|
63
|
|
|
'quantity' => 1, |
|
64
|
|
|
'currency' => $this->getCurrency(), |
|
65
|
1 |
|
'price' => $this->getAmount() |
|
66
|
1 |
|
] |
|
67
|
|
|
] |
|
68
|
1 |
|
); |
|
69
|
1 |
|
} |
|
70
|
1 |
|
|
|
71
|
1 |
|
$data['line_items'] = array_map( |
|
72
|
1 |
|
function (\Omnipay\Common\Item $item) { |
|
73
|
|
|
return [ |
|
74
|
1 |
|
'name' => $item->getName(), |
|
75
|
1 |
|
'description' => $this->nullIfEmpty($item->getDescription()), |
|
76
|
1 |
|
'amount' => (int)(100 * $item->getPrice()), |
|
77
|
1 |
|
'currency' => $this->getCurrency(), |
|
78
|
1 |
|
'quantity' => $item->getQuantity(), |
|
79
|
1 |
|
]; |
|
80
|
1 |
|
}, |
|
81
|
|
|
array_values( |
|
82
|
|
|
array_filter( |
|
83
|
|
|
$items->all(), |
|
84
|
|
|
function (\Omnipay\Common\Item $item) { |
|
85
|
1 |
|
return $item->getPrice() > 0; |
|
86
|
|
|
} |
|
87
|
|
|
) |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
1 |
|
return $data; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
1 |
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function sendData($data): PurchaseResponse |
|
98
|
|
|
{ |
|
99
|
|
|
// We use Stripe's SDK to initialise a (Stripe) session. The session gets passed through the process and is |
|
100
|
|
|
// used to identify this transaction. |
|
101
|
|
|
\Stripe\Stripe::setApiKey($this->getApiKey()); |
|
102
|
1 |
|
|
|
103
|
|
|
$options = []; |
|
104
|
1 |
|
$sentData = [ |
|
105
|
1 |
|
'publicKey' => $this->getPublicKey() |
|
106
|
1 |
|
]; |
|
107
|
|
|
if ($this->getOnBehalfOf()) { |
|
108
|
|
|
$options['stripe_account'] = $this->getOnBehalfOf(); |
|
109
|
|
|
$sentData['stripe_account'] = $this->getOnBehalfOf(); |
|
110
|
|
|
} |
|
111
|
|
|
// Initiate the session. |
|
112
|
|
|
$session = \Stripe\Checkout\Session::create($data, $options); |
|
113
|
|
|
$sentData['session'] = $session; |
|
114
|
|
|
return $this->response = new PurchaseResponse($this, $sentData); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|