Skip to content

Commit

Permalink
Merge pull request #171 from Invertus/wechat-and-wait
Browse files Browse the repository at this point in the history
[SV-4] Api up 1.40;Introduce Wechatpay, accounttoaccount, add wait tpl
  • Loading branch information
zuk3975 authored Jul 1, 2024
2 parents 51bc5de + c1d83af commit adb4c5d
Show file tree
Hide file tree
Showing 19 changed files with 449 additions and 171 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
},
"author": "PrestaShop",
"license": "AFL-3.0"
}
}
65 changes: 41 additions & 24 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ public function postProcess()
}

if ($cart->orderExists()) {
if (method_exists('Order', 'getIdByCartId')) {
$orderId = Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 use the alternative method
$orderId = Order::getOrderByCartId($cartId);
}

$orderId = $this->getOrderId($cartId);
$order = new Order($orderId);

$saferPayAuthorizedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED);
Expand All @@ -92,24 +86,19 @@ public function postProcess()
}
}

/** @var SaferPayOrderRepository $saferPayOrderRepository */
$saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class);

try {
$assertResponseBody = $this->assertTransaction($cartId);

/** @var SaferPayOrderRepository $saferPayOrderRepository */
$saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class);
$saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId);

/** @var UpdateSaferPayOrderAction $updateSaferPayOrderAction */
$updateSaferPayOrderAction = $this->module->getService(UpdateSaferPayOrderAction::class);
$updateSaferPayOrderAction->run(new SaferPayOrder($saferPayOrderId), self::SAFERPAY_ORDER_AUTHORIZE_ACTION);

// If order does not exist but assertion is valid that means order authorized or captured.
if (method_exists('Order', 'getIdByCartId')) {
$orderId = Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 use the alternative method
$orderId = Order::getOrderByCartId($cartId);
}
$orderId = $this->getOrderId($cartId);
if (!$orderId) {
/** @var CheckoutProcessor $checkoutProcessor **/
$checkoutProcessor = $this->module->getService(CheckoutProcessor::class);
Expand All @@ -123,13 +112,7 @@ public function postProcess()
$checkoutData->setOrderStatus($assertResponseBody->getTransaction()->getStatus());

$checkoutProcessor->run($checkoutData);

if (method_exists('Order', 'getIdByCartId')) {
$orderId = Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 or lower, use the alternative method
$orderId = Order::getOrderByCartId($cartId);
}
$orderId = $this->getOrderId($cartId);
}

//TODO look into pipeline design pattern to use when object is modified in multiple places to avoid this issue.
Expand All @@ -153,15 +136,34 @@ public function postProcess()

//NOTE to get latest information possible and not override new information.
$order = new Order($orderId);
$paymentMethod = $assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod();

if ((int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
if (SaferPayConfig::supportsOrderCapture($paymentMethod) && (int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$assertResponseBody->getTransaction()->getStatus() !== TransactionStatus::CAPTURED
) {
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
$orderStatusService->capture($order);
}
} catch (Exception $e) {
// this might be executed after pending transaction is declined (e.g. with accountToAccount payment)
if (!isset($order)) {
$orderId = $this->getOrderId($cartId);
$order = new Order($orderId);
}

$saferPayOrderId = $saferPayOrderRepository->getIdByOrderId($order->id);
$saferPayOrder = new SaferPayOrder($saferPayOrderId);

if ($order->id && $saferPayOrder->id) {
// assuming order transaction was declined
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZATION_FAILED_);
$order->update();
$saferPayOrder->authorized = false;
$saferPayOrder->pending = false;
$saferPayOrder->update();
}

PrestaShopLogger::addLog(
sprintf(
'%s has caught an error: %s',
Expand All @@ -187,6 +189,21 @@ private function assertTransaction($cartId) {
return $transactionAssert->assert($cartId);
}

/**
* @param int $cartId
*
* @return bool|int
*/
private function getOrderId($cartId)
{
if (method_exists('Order', 'getIdByCartId')) {
return Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 use the alternative method
return Order::getOrderByCartId($cartId);
}
}

protected function displayMaintenancePage()
{
return true;
Expand Down
Loading

0 comments on commit adb4c5d

Please sign in to comment.