Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sl 252/iframe not working #195

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down Expand Up @@ -77,7 +78,7 @@ protected function processGetStatus()

$this->ajaxDie(json_encode([
'saferpayOrder' => json_encode($saferPayOrder),
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->pending,
'isFinished' => true,
'href' => $this->context->link->getModuleLink(
$this->module->name,
$this->getSuccessControllerName($isBusinessLicence, $fieldToken),
Expand Down Expand Up @@ -145,9 +146,17 @@ private function submitHostedFields()

/** @var CheckoutController $checkoutController */
$checkoutController = $this->module->getService(CheckoutController::class);
$redirectUrl = $checkoutController->execute($checkoutData);
// $redirectUrl = $checkoutController->execute($checkoutData);

if (empty($redirectUrl)) {
$saferPayOrder = new SaferPayOrder();
$saferPayOrder->is_transaction = 1;
$saferPayOrder->update();

/** @var SaferPayTransactionAssertion $assertService */
$assertService = $this->module->getService(SaferPayTransactionAssertion::class);
$assertService->assert($this->context->cart->id, false);

if (empty($redirectUrl) || Tools::getValue('action') === 'submitHostedFields') {
$redirectUrl = $this->getRedirectionToControllerUrl('successHosted');
}

Expand Down
33 changes: 33 additions & 0 deletions controllers/front/hostedIframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\Front\CheckoutController;
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
use Invertus\SaferPay\Enum\ControllerName;
use PrestaShop\PrestaShop\Core\Checkout\TermsAndConditions;

if (!defined('_PS_VERSION_')) {
Expand All @@ -38,15 +41,45 @@ public function initContent()

$paymentMethod = Tools::getValue('saved_card_method');
$selectedCard = Tools::getValue("selectedCreditCard_{$paymentMethod}");

if (!SaferPayConfig::isVersion17()) {
$selectedCard = Tools::getValue("saved_card_{$paymentMethod}");
}

try {
/** @var CheckoutController $checkoutController */
$checkoutController = $this->module->getService(CheckoutController::class);

// refactor it to create checkout data from validator request
$checkoutData = CheckoutData::create(
(int) $this->context->cart->id,
$paymentMethod,
(int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE),
$selectedCard
);

$redirectUrl = $checkoutController->execute($checkoutData);
} catch (Exception $e) {
$redirectUrl = $this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
[
'cartId' => $this->context->cart->id,
'orderId' => Order::getOrderByCartId($this->context->cart->id),
'secureKey' => $this->context->cart->secure_key,
'moduleId' => $this->module->id,
],
true
);
$this->redirectWithNotifications($redirectUrl);
}

$this->context->smarty->assign([
'credit_card_front_url' => "{$this->module->getPathUri()}views/img/example-card/credit-card-front.png",
'credit_card_back_url' => "{$this->module->getPathUri()}views/img/example-card/credit-card-back.png",
'tos_cms' => SaferPayConfig::isVersionAbove177() ? $this->getDefaultTermsAndConditions() : null,
'saferpay_selected_card' => $selectedCard,
'redirect' => $redirectUrl,
]);

if (SaferPayConfig::isVersion17()) {
Expand Down
4 changes: 4 additions & 0 deletions controllers/front/successHosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function postProcess()
$secureKey = Tools::getValue('secureKey');
$moduleId = Tools::getValue('moduleId');

$order = new Order($orderId);
$statusService = $this->module->getService(SaferPayOrderStatusService::class);
$statusService->setAuthorized($order);

$cart = new Cart($cartId);
if ($cart->secure_key !== $secureKey) {
$this->errors[] = $this->module->l('Failed to validate cart.', self::FILE_NAME);
Expand Down
7 changes: 3 additions & 4 deletions src/DTO/Request/Initialize/InitializeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public function getAsArray()
'PayerNote' => $this->payment->getPayerNote(),
'Description' => $this->payment->getDescription(),
],
'PaymentMeans' => $this->getPaymentMeansField() ?: null,
'Payer' => [
'IpAddress' => $this->payer->getIpAddress(),
'LanguageCode' => $this->payer->getLanguageCode(),
Expand Down Expand Up @@ -250,9 +249,9 @@ public function getAsArray()
];
}

if ($this->alias || $this->fieldToken) {
unset($return['PaymentMethods']);
}
// if ($this->alias || $this->fieldToken) {
// unset($return['PaymentMethods']);
// }

//Wallet related, payment method must be empty, instead used "Wallets" argument in request.
if (in_array(\Tools::strtoupper($this->paymentMethod), SaferPayConfig::WALLET_PAYMENT_METHODS)) {
Expand Down
15 changes: 15 additions & 0 deletions src/Service/SaferPayOrderStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ public function setComplete(Order $order)
$order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_);
}

public function setAuthorized(Order $order)
{
$saferPayOrder = $this->orderRepository->getByOrderId($order->id);
$saferPayOrder->authorized = 1;

$saferPayOrder->update();

//NOTE: Older PS versions does not handle same state change, so we need to check if state is already set
if ($order->getCurrentState() === _SAFERPAY_PAYMENT_AUTHORIZED_) {
return;
}

$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_);
}

/** TODO extract capture api code to different service like Assert for readability */
public function capture(Order $order, $refundedAmount = 0, $isRefund = false)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Service/TransactionFlow/SaferPayTransactionAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace Invertus\SaferPay\Service\TransactionFlow;

use Invertus\SaferPay\Api\Request\AssertService;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\DTO\Response\Assert\AssertBody;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\Request\AssertRequestObjectCreator;
Expand Down Expand Up @@ -71,6 +72,14 @@ public function assert($cartId, $update = true)
$saferPayOrder = new SaferPayOrder($this->orderRepository->getIdByCartId($cartId));
\PrestaShopLogger::addLog('saferpayOrderId:' . $saferPayOrder->id);

$businessLicence = \Configuration::get(SaferPayConfig::BUSINESS_LICENSE . SaferPayConfig::getConfigSuffix());

if($businessLicence) {
$saferPayOrder = new SaferPayOrder($saferPayOrder->id);
$saferPayOrder->is_transaction = 1;
$saferPayOrder->update();
}

$assertRequest = $this->assertRequestCreator->create($saferPayOrder->token);
$assertResponse = $this->assertionService->assert($assertRequest, $saferPayOrder->id);

Expand Down
2 changes: 1 addition & 1 deletion views/templates/front/credit_cards.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</tr>
</thead>
{foreach $rows as $row}
{$row nofilter|escape:'htmlall':'UTF-8'}
{$row|escape:'htmlall':'UTF-8' nofilter}
{/foreach}
</table>
</div>
Expand Down
Loading