Skip to content

Commit

Permalink
Merge pull request #7 from onpayio/task/api_payment_creation
Browse files Browse the repository at this point in the history
Implemented creation of payments through API instead of posting to payment window
  • Loading branch information
ebbesmoeller authored Aug 23, 2023
2 parents d48c0cd + f81d5bb commit 901f230
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 61 deletions.
85 changes: 41 additions & 44 deletions Block/RedirectUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
use Magento\Sales\Model\OrderFactory;
use OnPay\API\Exception\InvalidFormatException;
use OnPay\Magento2\Helper\Config;
use OnPay\OnPayAPI;
use OnPay\API\PaymentWindow;
use OnPay\API\PaymentService;
use OnPay\API\Payment\SimplePayment;
use OnPay\API\PaymentWindow\PaymentInfo;
use OnPay\Magento2\Helper\Currency;
use OnPay\Magento2\Model\ManageOnPay;
use OnPay\Magento2\Model\Payment\OnPaySelectMethod;
use OnPay\Magento2\Model\Payment\OnPayMobilePayCheckoutMethod;
use OnPay\Magento2\Model\OnPayTokenStorage;
use Sokil\IsoCodes\Database\Countries;

class RedirectUrl extends Template
Expand Down Expand Up @@ -81,9 +85,14 @@ class RedirectUrl extends Template
protected $manageOnPay;

/**
* @var PaymentWindow|null
* @var SimplePayment|null
*/
protected $paymentWindow;
protected $payment;

/**
* @var OnPayAPI
*/
protected $onPayApi;

/**
* @param Context $context
Expand Down Expand Up @@ -111,7 +120,15 @@ public function __construct(
$this->regionFactory = $regionFactory;
$this->manageOnPay = $manageOnPay;
$this->currencyHelper = new Currency();
$this->paymentWindow = null;
$this->payment = null;

$tokenStorage = new OnPayTokenStorage($helper);
$this->onPayApi = new OnPayAPI(
$tokenStorage, [
'client_id' => $helper->getWebsiteUrl(),
'redirect_uri' => $helper->getAuthorizeUrl()
]
);
}

/**
Expand Down Expand Up @@ -167,16 +184,32 @@ private function getRegion($regionId)
return $this->regionFactory->create()->load($regionId);
}

/**
* @return string|null
*/
public function getPaymentUrl()
{
$payment = $this->getPayment();
if (null === $payment) {
return null;
}
return $payment->getPaymentWindowLink();
}

/**
* @return PaymentWindow|null
* @throws InvalidFormatException
*/
protected function getPaymentWindow()
protected function getPayment()
{
if (null === $this->paymentWindow) {
$this->paymentWindow = $this->createPaymentWindow();
if (null === $this->payment) {
$paymentService = new PaymentService($this->onPayApi);
$paymentWindow = $this->createPaymentWindow();
if (null !== $paymentWindow && $paymentWindow->isValid()) {
$this->payment = $paymentService->createNewPayment($paymentWindow);
}
}
return $this->paymentWindow;
return $this->payment;
}

/**
Expand Down Expand Up @@ -216,7 +249,7 @@ protected function createPaymentWindow()
$paymentWindow->set3DSecure($this->helper->getSecure());
$paymentWindow->setLanguage($this->helper->getPaymentWindowLanguage());
$paymentWindow->setDesign($this->helper->getDesign());
$paymentWindow->setExpiration($this->helper->getExpiration());
$paymentWindow->setExpiration(intval($this->helper->getExpiration()));

$method = $payment->getMethod();
if ($method !== OnPaySelectMethod::METHOD_CODE) {
Expand Down Expand Up @@ -319,40 +352,4 @@ public function isAlreadyPaid()

return false;
}

/**
* @return bool
*/
public function isValid()
{
$paymentWindow = $this->getPaymentWindow();
if (null === $paymentWindow) {
return false;
}
return $paymentWindow->isValid();
}

/**
* @return string|null
*/
public function getActionUrl()
{
$paymentWindow = $this->getPaymentWindow();
if (null === $paymentWindow) {
return null;
}
return $paymentWindow->getActionUrl();
}

/**
* @return array|null
*/
public function getFormFields()
{
$paymentWindow = $this->getPaymentWindow();
if (null === $paymentWindow) {
return null;
}
return $paymentWindow->getFormFields();
}
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Implemented payment creation through the API, instead of posting to payment window.
19 changes: 2 additions & 17 deletions view/frontend/templates/redirectUrl.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,8 @@
* @var \OnPay\Magento2\Block\RedirectUrl $block
*/
?>

<?php if ($block->isValid()): ?>
<form id="onpay_window_form" method="post" action="<?= $block->getActionUrl(); ?>" accept-charset="UTF-8">
<?php foreach ($block->getFormFields() as $key => $value): ?>
<input type="hidden" name="<?= $block->escapeHtml($key); ?>" value="<?= $block->escapeHtml($value); ?>">
<?php endforeach; ?>

<input type="submit" style="display:none;" value="Make Payment">
</form>
<script type="text/javascript">
require([
'jquery'
], function($) {
'use strict';
$('#onpay_window_form').submit();
});
</script>
<?php if (null !== $block->getPaymentUrl()): ?>
<?php header("Location: " . $block->getPaymentUrl()); exit(); ?>
<?php elseif (null === $block->getOrderId()): ?>
<h3><?= $block->escapeHtml(__('Could not get OrderID')); ?></h3>
<?php elseif ($block->isAlreadyPaid()): ?>
Expand Down

0 comments on commit 901f230

Please sign in to comment.