-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from yoomoney/release/v2.2.2
Release/2.2.2
- Loading branch information
Showing
27 changed files
with
895 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
<?php | ||
/** | ||
* @author YooMoney <[email protected]> | ||
* @copyright © 2020 "YooMoney", NBСO LLC | ||
* @copyright © 2022 "YooMoney", NBСO LLC | ||
* @license https://yoomoney.ru/doc.xml?id=527052 | ||
*/ | ||
use YooKassa\Model\Notification\NotificationSucceeded; | ||
use YooKassa\Model\Notification\NotificationWaitingForCapture; | ||
use YooKassa\Model\NotificationEventType; | ||
use YooKassa\Model\Payment; | ||
use YooKassa\Model\PaymentStatus; | ||
use YooKassa\Model\Notification\NotificationFactory; | ||
use YooKassa\Model\RefundStatus; | ||
|
||
/** | ||
* Class YooMoneyModulePaymentKassaModuleFrontController | ||
|
@@ -17,6 +16,13 @@ | |
*/ | ||
class YooMoneyModuleNotifyCaptureModuleFrontController extends ModuleFrontController | ||
{ | ||
const REFUND_STATUS_ID = 7; | ||
|
||
/** | ||
* Обработка входящих уведомлений от Юkassa | ||
* | ||
* @return bool|void | ||
*/ | ||
public function postProcess() | ||
{ | ||
if ($_SERVER['REQUEST_METHOD'] == 'GET') { | ||
|
@@ -37,8 +43,8 @@ public function postProcess() | |
|
||
return; | ||
} | ||
$json = json_decode($source, true); | ||
if (empty($json)) { | ||
$data = json_decode($source, true); | ||
if (empty($data)) { | ||
if (json_last_error() === JSON_ERROR_NONE) { | ||
$message = 'empty object in body'; | ||
} else { | ||
|
@@ -49,47 +55,118 @@ public function postProcess() | |
|
||
return; | ||
} | ||
|
||
$kassa = $this->module->getKassaModel(); | ||
|
||
try { | ||
if ($json['event'] == NotificationEventType::PAYMENT_WAITING_FOR_CAPTURE) { | ||
$notification = new NotificationWaitingForCapture($json); | ||
$factory = new NotificationFactory(); | ||
$notificationObject = $factory->factory($data); | ||
|
||
$paymentId = $notificationObject->getObject()->getId(); | ||
|
||
if ($notificationObject->getEvent() === NotificationEventType::REFUND_SUCCEEDED) { | ||
$refundNotifyObj = $notificationObject->getObject(); | ||
$refundKassaObj = $kassa->fetchRefund($refundNotifyObj->getId()); | ||
$paymentId = $refundKassaObj->getPaymentId(); | ||
} | ||
|
||
$paymentModel = $kassa->getPayment($paymentId); | ||
|
||
$result = false; | ||
if ( | ||
$notificationObject->getEvent() === NotificationEventType::PAYMENT_SUCCEEDED | ||
&& $paymentModel->getStatus() === PaymentStatus::SUCCEEDED | ||
) { | ||
$kassa->updatePaymentStatus($paymentModel); | ||
$orderId = $kassa->getOrderIdByPayment($paymentModel); | ||
$orderStatusId = $kassa->getSuccessStatusId(); | ||
$history = new OrderHistory(); | ||
$history->id_order = $orderId; | ||
$history->changeIdOrderState($orderStatusId, $orderId); | ||
$history->addWithemail(true); | ||
// обновляем номер транзакции, привязанной к заказу | ||
$kassa->updateOrderPaymentId($orderId, $paymentModel); | ||
$result = true; | ||
} | ||
|
||
if ( | ||
$notificationObject->getEvent() === NotificationEventType::PAYMENT_WAITING_FOR_CAPTURE | ||
&& $paymentModel->getStatus() === PaymentStatus::WAITING_FOR_CAPTURE | ||
) { | ||
$this->module->log('info', 'Notification waiting for capture init.'); | ||
$result = $this->module->captureOrHoldPayment($notification->getObject()->getId()); | ||
} else { | ||
$webhookObject = new NotificationSucceeded($json); | ||
$this->module->log('info', 'Notification succeeded init.'); | ||
$paymentData = $webhookObject->getObject(); | ||
$kassa = $this->module->getKassaModel(); | ||
$paymentModel = $kassa->getPayment($paymentData->getId()); | ||
if ($paymentModel->status === PaymentStatus::SUCCEEDED) { | ||
$kassa->updatePaymentStatus($paymentModel); | ||
$orderId = $kassa->getOrderIdByPayment($paymentModel); | ||
$orderStatusId = $kassa->getSuccessStatusId(); | ||
$history = new OrderHistory(); | ||
$history->id_order = $orderId; | ||
$history->changeIdOrderState($orderStatusId, $orderId); | ||
$history->addWithemail(true); | ||
// обновляем номер транзакции, привязанной к заказу | ||
$this->module->getKassaModel()->updateOrderPaymentId($orderId, $paymentData); | ||
echo json_encode(array('success' => true)); | ||
exit(); | ||
} else { | ||
$result = false; | ||
} | ||
|
||
$kassa->updatePaymentStatus($paymentModel); | ||
$orderId = $kassa->getOrderIdByPayment($paymentModel); | ||
|
||
$history = new OrderHistory(); | ||
$history->id_order = $orderId; | ||
$history->changeIdOrderState($kassa->getOnHoldStatusId(), $orderId); | ||
$history->addWithemail(true); | ||
$kassa->updateOrderPaymentId($orderId, $paymentModel); | ||
$result = true; | ||
} | ||
|
||
if ( | ||
$notificationObject->getEvent() === NotificationEventType::PAYMENT_CANCELED | ||
&& $paymentModel->getStatus() === PaymentStatus::CANCELED | ||
) { | ||
$this->module->log('info', 'Notification payment canceled init.'); | ||
|
||
$kassa->updatePaymentStatus($paymentModel); | ||
$orderId = $kassa->getOrderIdByPayment($paymentModel); | ||
$history = new OrderHistory(); | ||
$history->id_order = $orderId; | ||
$history->changeIdOrderState($kassa->getCancelStatusId(), $orderId); | ||
$history->addWithemail(true); | ||
$kassa->updateOrderPaymentId($orderId, $paymentModel); | ||
|
||
$result = true; | ||
} | ||
|
||
if ( | ||
$notificationObject->getEvent() === NotificationEventType::REFUND_SUCCEEDED | ||
&& $refundKassaObj->getStatus() === RefundStatus::SUCCEEDED | ||
&& $paymentModel->getStatus() === PaymentStatus::SUCCEEDED | ||
) { | ||
$this->module->log('info', 'Notification payment refund init.'); | ||
|
||
$kassa->updatePaymentStatus($paymentModel); | ||
$orderId = $kassa->getOrderIdByPayment($paymentModel); | ||
$history = new OrderHistory(); | ||
$history->id_order = $orderId; | ||
$history->changeIdOrderState(self::REFUND_STATUS_ID, $orderId); | ||
$history->addWithemail(true); | ||
$kassa->updateOrderPaymentId($orderId, $paymentModel); | ||
|
||
$result = true; | ||
} | ||
|
||
if ( | ||
$notificationObject->getEvent() === NotificationEventType::DEAL_CLOSED | ||
|| $notificationObject->getEvent() === NotificationEventType::PAYOUT_CANCELED | ||
|| $notificationObject->getEvent() === NotificationEventType::PAYOUT_SUCCEEDED | ||
) { | ||
$this->module->log( | ||
'info', | ||
'Notification ' . $notificationObject->getEvent() . ' received.' | ||
); | ||
|
||
return true; | ||
} | ||
|
||
if (!$result) { | ||
header('HTTP/1.1 500 Server error 1'); | ||
exit(); | ||
} | ||
|
||
echo json_encode(array('success' => true)); | ||
exit(); | ||
|
||
} catch (\Exception $e) { | ||
$this->module->log('error', 'Invalid notification object - '.$e->getMessage()); | ||
header('HTTP/1.1 500 Server error: '.$e->getMessage()); | ||
|
||
return; | ||
} | ||
|
||
if (!$result) { | ||
header('HTTP/1.1 500 Server error 1'); | ||
exit(); | ||
} | ||
echo json_encode(array('success' => $result)); | ||
exit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.