Skip to content

Commit c921c51

Browse files
committed
upd to 5.1.0
1 parent 3e36138 commit c921c51

File tree

5 files changed

+136
-291
lines changed

5 files changed

+136
-291
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
### Способы оплаты
66

77
* Банковские карты (Card)
8+
* Кредитные карты (CreditCard)
89
* Sepa Debit / IBAN
910
* Sofort
1011
* Giropay
1112

13+
### Поддерживаемая версия STRIPE API
14+
15+
`2023-08-16`
16+
1217
## Инструкция
1318

1419
1. Узнать используемую кодировку (dbconn.php или after_connect_d7.php или after_connect.php)
@@ -22,7 +27,13 @@
2227
1. Название (NAME) обязательно должно быть Stripe или stripe (иначе вебхук не будет работать)
2328
1. В полях ниже указываем свои данные
2429
1. Настраиваем вебхуки
25-
1. Копируем обработчик вебхука stripeWebhook.php, который в корне папки модуля. Копируем в любое публичное место проекта. После указываем путь до обработчика в кабинете stripe.com
30+
* Копируем обработчик вебхука stripeWebhook.php, который в корне папки модуля. Копируем в любое публичное место проекта. После указываем путь до обработчика в кабинете stripe.com
31+
* Переходим в кабинет stripe.com
32+
* указываем url-путь до файла `stripeWebhook.php`
33+
* добавляем вебхуки на события:
34+
* `charge.succeeded`
35+
* `checkout.session.completed`
36+
* версия API `2023-08-16`
2637
1. Поблагодарить автора :)
2738
1. Использовать.
2839

utf8/dev2fun.stripepayment/install/sale_payment/stripe/webhook.php

Lines changed: 60 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @author darkfriend <hi@darkfriend.ru>
44
* @copyright (c) 2020-2023, darkfriend
5-
* @version 1.5.0
5+
* @version 1.5.1
66
*/
77
require_once($_SERVER['DOCUMENT_ROOT'] . "/bitrix/modules/main/include/prolog_before.php");
88

@@ -58,182 +58,99 @@
5858
throw new \Exception('Webhook token is not found!');
5959
}
6060

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-
7061
$event = \Stripe\Webhook::constructEvent(
7162
$payload, $sig_header, $webhookToken
7263
);
7364

7465
$stripe = new \Stripe\StripeClient($secretKey);
7566

7667
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;
10968
case 'charge.succeeded':
11069
/** @var \Stripe\Charge $charge */
11170
$charge = $event->data->object;
11271
if ($charge->status!=='succeeded') {
11372
throw new \Exception('Status is not succeeded');
11473
}
115-
$orderId = $charge->source->metadata->toArray()['orderId'] ?? null;
74+
$customer = $stripe->customers->retrieve($charge->customer);
75+
$orderId = $customer->metadata->toArray()['orderId'] ?? null;
11676
if (!$orderId) {
11777
throw new \Exception('$orderId is not found!');
11878
}
11979
break;
12080

12181
case 'checkout.session.completed':
122-
/** @var \Stripe\Charge $charge */
82+
/** @var \Stripe\Checkout\Session $charge */
12383
$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+
}
12889
}
129-
// $orderId = $customer->metadata->orderId;
13090
break;
13191

13292
default:
13393
throw new \Exception("Received unknown event type {$event->type}");
13494
}
13595

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+
}
211102

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+
}
216107

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+
}
222111

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+
}
224128

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";
229141

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+
}
234152
}
235153
}
236-
// }
237154

238155
\http_response_code(200);
239156

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22
$arModuleVersion = [
3-
"VERSION" => "1.5.0",
4-
"VERSION_DATE" => "2023-08-21 22:10:00",
3+
"VERSION" => "1.5.1",
4+
"VERSION_DATE" => "2023-08-22 22:50:00",
55
];

0 commit comments

Comments
 (0)