|
2 | 2 | /** |
3 | 3 | * @author darkfriend <hi@darkfriend.ru> |
4 | 4 | * @copyright (c) 2020-2023, darkfriend |
5 | | - * @version 1.5.0 |
| 5 | + * @version 1.5.1 |
6 | 6 | */ |
7 | 7 | require_once($_SERVER['DOCUMENT_ROOT'] . "/bitrix/modules/main/include/prolog_before.php"); |
8 | 8 |
|
|
58 | 58 | throw new \Exception('Webhook token is not found!'); |
59 | 59 | } |
60 | 60 |
|
61 | | - // Set your secret key: remember to change this to your live secret key in production |
62 | | - // See your keys here: https://dashboard.stripe.com/account/apikeys |
63 | | -// \Stripe\Stripe::setApiKey($secretKey); |
64 | | - |
65 | | - |
66 | | -// $event = \Stripe\Event::constructFrom( |
67 | | -// json_decode($payload, true), |
68 | | -// ); |
69 | | - |
70 | 61 | $event = \Stripe\Webhook::constructEvent( |
71 | 62 | $payload, $sig_header, $webhookToken |
72 | 63 | ); |
73 | 64 |
|
74 | 65 | $stripe = new \Stripe\StripeClient($secretKey); |
75 | 66 |
|
76 | 67 | switch ($event->type) { |
77 | | -// case 'payment_intent.created': |
78 | | -// $paymentIntent = $event->data->object; |
79 | | -// break; |
80 | | -// case 'payment_intent.pending': break; |
81 | | -// case 'payment_intent.processing': break; |
82 | | -// case 'payment_intent.succeeded': break; |
83 | | -// case 'payment_intent.canceled': break; |
84 | | -// case 'payment_intent.payment_failed': break; |
85 | | -// case 'payment_intent.requires_action': break; |
86 | | - |
87 | | - case 'source.chargeable': |
88 | | - /** @var \Stripe\Charge $charge */ |
89 | | - $charge = $event->data->object; |
90 | | - $finalCharge = $stripe->charges->create([ |
91 | | - 'amount' => $charge->amount, |
92 | | - 'currency' => $charge->currency, |
93 | | - 'source' => $charge->id, |
94 | | - ]); |
95 | | -// \http_response_code(200); |
96 | | -// if(\is_string($finalCharge)) { |
97 | | -// \http_response_code(580); |
98 | | -// die($finalCharge); |
99 | | -// } |
100 | | -// if($charge->type!=='sofort') { |
101 | | -// die('OK'); |
102 | | -// } else { |
103 | | - $orderId = $charge->metadata->toArray()['orderId'] ?? null; |
104 | | - if (!$orderId) { |
105 | | - throw new \Exception('$orderId is not found!'); |
106 | | - } |
107 | | -// } |
108 | | - break; |
109 | 68 | case 'charge.succeeded': |
110 | 69 | /** @var \Stripe\Charge $charge */ |
111 | 70 | $charge = $event->data->object; |
112 | 71 | if ($charge->status!=='succeeded') { |
113 | 72 | throw new \Exception('Status is not succeeded'); |
114 | 73 | } |
115 | | - $orderId = $charge->source->metadata->toArray()['orderId'] ?? null; |
| 74 | + $customer = $stripe->customers->retrieve($charge->customer); |
| 75 | + $orderId = $customer->metadata->toArray()['orderId'] ?? null; |
116 | 76 | if (!$orderId) { |
117 | 77 | throw new \Exception('$orderId is not found!'); |
118 | 78 | } |
119 | 79 | break; |
120 | 80 |
|
121 | 81 | case 'checkout.session.completed': |
122 | | - /** @var \Stripe\Charge $charge */ |
| 82 | + /** @var \Stripe\Checkout\Session $charge */ |
123 | 83 | $charge = $event->data->object; |
124 | | - $customer = $stripe->customers->retrieve($charge->customer->id); |
125 | | - $orderId = $customer->metadata->toArray()['orderId'] ?? null; |
126 | | - if (!$orderId) { |
127 | | - throw new \Exception('OrderId is not found!'); |
| 84 | + if ($charge->status === 'complete' && $charge->payment_status === 'paid') { |
| 85 | + $orderId = $charge->metadata->toArray()['orderId'] ?? null; |
| 86 | + if (!$orderId) { |
| 87 | + throw new \Exception('OrderId is not found!'); |
| 88 | + } |
128 | 89 | } |
129 | | -// $orderId = $customer->metadata->orderId; |
130 | 90 | break; |
131 | 91 |
|
132 | 92 | default: |
133 | 93 | throw new \Exception("Received unknown event type {$event->type}"); |
134 | 94 | } |
135 | 95 |
|
136 | | -// $event = \Stripe\Webhook::constructEvent( |
137 | | -// $payload, $sig_header, $webhookToken |
138 | | -// ); |
139 | | -// $charge = $event['data']['object']; |
140 | | - |
141 | | -// if(\in_array($event['type'], [ |
142 | | -// 'checkout.session.completed', |
143 | | -// 'source.chargeable', |
144 | | -//// 'charge.succeeded', |
145 | | -// ])) { |
146 | | -// |
147 | | -// if($event['type'] === 'source.chargeable') { |
148 | | -// $finalCharge = \Stripe\Charge::create([ |
149 | | -// 'amount' => $charge['amount'], |
150 | | -// 'currency' => $charge['currency'], |
151 | | -// 'source' => $charge['id'], |
152 | | -// ]); |
153 | | -// \http_response_code(200); |
154 | | -// // var_dump($finalCharge); |
155 | | -// if(\is_string($finalCharge)) { |
156 | | -// \http_response_code(580); |
157 | | -// die($finalCharge); |
158 | | -// } |
159 | | -// if($charge['type']!=='sofort') { |
160 | | -// die('OK'); |
161 | | -// } else { |
162 | | -// $orderId = $charge['metadata']['orderId']; |
163 | | -// if(!$orderId) { |
164 | | -// die('$orderId is not found!'); |
165 | | -// } |
166 | | -// } |
167 | | -// } |
168 | | -// |
169 | | -// if($event['type']==='charge.succeeded') { |
170 | | -// // http_response_code(500); |
171 | | -// if($charge['status']!=='succeeded') { |
172 | | -// die('Status is not succeeded'); |
173 | | -// } |
174 | | -// $orderId = $charge['source']['metadata']['orderId']; |
175 | | -// } |
176 | | -// |
177 | | -// if($event['type']==='checkout.session.completed') { |
178 | | -// $customer = \Stripe\Customer::retrieve($charge['customer']); |
179 | | -// if(empty($customer->metadata->orderId)) { |
180 | | -// throw new \Exception('OrderId is not found!'); |
181 | | -// } |
182 | | -// $orderId = $customer->metadata->orderId; |
183 | | -// } |
184 | | - |
185 | | - // http_response_code(403); |
186 | | - // die(); |
187 | | - |
188 | | - // $orderID = $customer->metadata->orderId; |
189 | | - |
190 | | - /** @var \Bitrix\Sale\Order $order */ |
191 | | - $order = Order::load($orderId); |
192 | | - if(empty($order)) { |
193 | | - throw new \Exception('Order is not found!'); |
194 | | - } |
195 | | - $arOrder = $order->getFieldValues(); |
196 | | - $orderID = $arOrder['ID']; |
197 | | - $userId = $order->getUserId(); |
198 | | - |
199 | | - $arFields = [ |
200 | | - "PAYED"=>"Y", |
201 | | - "DATE_PAYED" => Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG))), |
202 | | - "USER_ID" => $userId, |
203 | | - "EMP_PAYED_ID" => $userId, |
204 | | - "PS_SUM" => ($charge['amount']/100), |
205 | | - "PS_CURRENCY" => $charge['currency'], |
206 | | - "PS_STATUS " => "Y", |
207 | | - ]; |
208 | | - if(!empty($paySystem['PSA_PARAMS']['PAYED_ORDER_STATUS']['VALUE'])) { |
209 | | - $arFields['STATUS_ID'] = $paySystem['PSA_PARAMS']['PAYED_ORDER_STATUS']['VALUE']; |
210 | | - } |
| 96 | + if ($orderId) { |
| 97 | + /** @var \Bitrix\Sale\Order $order */ |
| 98 | + $order = Order::load($orderId); |
| 99 | + if (empty($order)) { |
| 100 | + throw new \Exception('Order is not found!'); |
| 101 | + } |
211 | 102 |
|
212 | | - $events = GetModuleEvents("dev2fun.stripepayment", "OnBeforeUpdateOrder", true); |
213 | | - foreach ($events as $arEvent) { |
214 | | - ExecuteModuleEventEx($arEvent, array(&$arFields, $charge, $orderID)); |
215 | | - } |
| 103 | + if ($order->isPaid()) { |
| 104 | + http_response_code(200); |
| 105 | + die('OK'); |
| 106 | + } |
216 | 107 |
|
217 | | -// $saleOrder = new CSaleOrder; |
218 | | - $resOrder = CSaleOrder::Update($orderID, $arFields); |
219 | | - if(!$resOrder) { |
220 | | - throw new Exception($APPLICATION->GetException()); |
221 | | - } |
| 108 | + if ($order->isCanceled()) { |
| 109 | + throw new \Exception('Order has status is canceled'); |
| 110 | + } |
222 | 111 |
|
223 | | - $output = "Pay Success"; |
| 112 | + $arOrder = $order->getFieldValues(); |
| 113 | + $orderID = $arOrder['ID']; |
| 114 | + $userId = $order->getUserId(); |
| 115 | + |
| 116 | + $arFields = [ |
| 117 | + "PAYED"=>"Y", |
| 118 | + "DATE_PAYED" => Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG))), |
| 119 | + "USER_ID" => $userId, |
| 120 | + "EMP_PAYED_ID" => $userId, |
| 121 | + "PS_SUM" => ($charge['amount']/100), |
| 122 | + "PS_CURRENCY" => $charge['currency'], |
| 123 | + "PS_STATUS " => "Y", |
| 124 | + ]; |
| 125 | + if(!empty($paySystem['PSA_PARAMS']['PAYED_ORDER_STATUS']['VALUE'])) { |
| 126 | + $arFields['STATUS_ID'] = $paySystem['PSA_PARAMS']['PAYED_ORDER_STATUS']['VALUE']; |
| 127 | + } |
224 | 128 |
|
225 | | - $events = GetModuleEvents("dev2fun.stripepayment", "OnBeforeSuccessOutput", true); |
226 | | - foreach ($events as $arEvent) { |
227 | | - ExecuteModuleEventEx($arEvent, array(&$output,$arFields,$orderID)); |
228 | | - } |
| 129 | + $events = GetModuleEvents("dev2fun.stripepayment", "OnBeforeUpdateOrder", true); |
| 130 | + foreach ($events as $arEvent) { |
| 131 | + ExecuteModuleEventEx($arEvent, array(&$arFields, $charge, $orderID)); |
| 132 | + } |
| 133 | + |
| 134 | + // $saleOrder = new CSaleOrder; |
| 135 | + $resOrder = CSaleOrder::Update($orderID, $arFields); |
| 136 | + if(!$resOrder) { |
| 137 | + throw new Exception($APPLICATION->GetException()); |
| 138 | + } |
| 139 | + |
| 140 | + $output = "Pay Success"; |
229 | 141 |
|
230 | | - if(!empty($SALE_CORRESPONDENCE['REDIRECT_SUCCESS']['VALUE'])){ |
231 | | - $url = Dev2funModuleStripeClass::GetRedirectUrl($SALE_CORRESPONDENCE['REDIRECT_SUCCESS']['VALUE'],$orderId,'success'); |
232 | | - if($url) { |
233 | | - LocalRedirect($url); |
| 142 | + $events = GetModuleEvents("dev2fun.stripepayment", "OnBeforeSuccessOutput", true); |
| 143 | + foreach ($events as $arEvent) { |
| 144 | + ExecuteModuleEventEx($arEvent, array(&$output,$arFields,$orderID)); |
| 145 | + } |
| 146 | + |
| 147 | + if(!empty($SALE_CORRESPONDENCE['REDIRECT_SUCCESS']['VALUE'])){ |
| 148 | + $url = Dev2funModuleStripeClass::GetRedirectUrl($SALE_CORRESPONDENCE['REDIRECT_SUCCESS']['VALUE'],$orderId,'success'); |
| 149 | + if($url) { |
| 150 | + LocalRedirect($url); |
| 151 | + } |
234 | 152 | } |
235 | 153 | } |
236 | | -// } |
237 | 154 |
|
238 | 155 | \http_response_code(200); |
239 | 156 |
|
|
0 commit comments