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

Redirect buyer out to the internet banking page if the placeOrder action is succeeded #66

Merged
merged 3 commits into from
Mar 31, 2017
Merged
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
51 changes: 51 additions & 0 deletions Api/Data/PaymentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace Omise\Payment\Api\Data;

interface PaymentInterface
{
// Fields
const TYPE = 'object';
const ORDER_ID = 'order_id';
const AUTHORIZE_URI = 'authorize_uri';

/**
* @api
*
* @param int $value
*
* @return self
*/
public function setOrderId($value);

/**
* @api
*
* @param string $value
*
* @return self
*/
public function setAuthorizeUri($value);

/**
* Always return a "payment" string.
*
* @api
*
* @return string
*/
public function getObject();

/**
* @api
*
* @return int
*/
public function getOrderId();

/**
* @api
*
* @return string|null
*/
public function getAuthorizeUri();
}
12 changes: 12 additions & 0 deletions Api/PaymentInformationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Omise\Payment\Api;

interface PaymentInformationInterface
{
/**
* @param int $order_id
*
* @return Omise\Payment\Api\Data\PaymentInterface
*/
public function offsite($order_id);
}
20 changes: 20 additions & 0 deletions Gateway/Response/PaymentDetailsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Omise\Payment\Gateway\Response;

use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Payment\Gateway\Response\HandlerInterface;

class PaymentDetailsHandler implements HandlerInterface
{
/**
* @inheritdoc
*/
public function handle(array $handlingSubject, array $response)
{
$payment = SubjectReader::readPayment($handlingSubject);
$payment = $payment->getPayment();

$payment->setAdditionalInformation('charge_id', $response['data']['id']);
$payment->setAdditionalInformation('charge_authorize_uri', $response['data']['authorize_uri']);
}
}
64 changes: 64 additions & 0 deletions Model/Data/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
namespace Omise\Payment\Model\Data;

use Omise\Payment\Api\Data\PaymentInterface;
use Magento\Framework\Api\AbstractExtensibleObject;

class Payment extends AbstractExtensibleObject implements PaymentInterface
{
/**
* @api
*
* @param int $value
*
* @return self
*/
public function setOrderId($value)
{
return $this->setData(self::ORDER_ID, $value);
}

/**
* @api
*
* @param string $value
*
* @return self
*/
public function setAuthorizeUri($value)
{
return $this->setData(self::AUTHORIZE_URI, $value);
}

/**
* Always return a "payment" string.
*
* @api
*
* @return string
*/
public function getObject()
{
return 'payment';
}

/**
* @api
*
* @return int
*/
public function getOrderId()
{
return $this->_get(self::ORDER_ID);
}

/**
* @api
*
* @return string|null
*/
public function getAuthorizeUri()
{
return $this->_get(self::AUTHORIZE_URI);
}
}
69 changes: 69 additions & 0 deletions Model/PaymentInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace Omise\Payment\Model;

use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\AuthorizationException;
use Magento\Framework\Exception\PaymentException;
use Magento\Framework\Exception\SessionException;
use Omise\Payment\Api\PaymentInformationInterface;
use Omise\Payment\Api\Data\PaymentInterface;
use Omise\Payment\Api\Data\PaymentInterfaceFactory;

class PaymentInformation implements PaymentInformationInterface
{
/**
* @var \Magento\Checkout\Model\Session
*/
protected $session;

/**
* @var \Omise\Payment\Api\Data\PaymentInterfaceFactory
*/
private $data_factory;


public function __construct(Session $session, PaymentInterfaceFactory $data_factory)
{
$this->session = $session;
$this->data_factory = $data_factory;
}

/**
* @param int $id
*
* @return \Magento\Sales\Model\Order
*/
protected function loadOrder($id)
{
// Note, $order->getId(); will return a string, not int.
$order = $this->session->getLastRealOrder();

if (! $order->getId()) {
throw new SessionException(__('The order session no longer exists, please make an order again or contact our support if you have any questions.'));
}

if ($id != $order->getId()) {
throw new AuthorizationException(__('This request is not authorized to access the resource, please contact our support if you have any questions'));
}

return $order;
}

/**
* @param int $order_id
*
* @return Omise\Payment\Api\Data\PaymentInterface
*/
public function offsite($order_id)
{
if ($payment = $this->loadOrder($order_id)->getPayment()) {
$data = $this->data_factory->create();
$data->setOrderId($order_id);
$data->setAuthorizeUri($payment->getAdditionalInformation('charge_authorize_uri'));

return $data;
}

throw new PaymentException(__('Cannot retrieve a payment detail from the request, please contact our support if you have any questions'));
}
}
5 changes: 4 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Omise\Payment\Api\Data\PaymentInterface" type="Omise\Payment\Model\Data\Payment" />
<preference for="Omise\Payment\Api\PaymentInformationInterface" type="Omise\Payment\Model\PaymentInformation" />

<!-- Internet Banking payment solution -->
<virtualType name="OmiseOffsiteInternetbankingAdapter" type="Magento\Payment\Model\Method\Adapter">
<arguments>
Expand Down Expand Up @@ -45,7 +48,7 @@
<argument name="requestBuilder" xsi:type="object">OmiseOffsiteInternetbankingChargeRequest</argument>
<argument name="transferFactory" xsi:type="object">Omise\Payment\Gateway\Http\TransferFactory</argument>
<argument name="client" xsi:type="object">Omise\Payment\Gateway\Http\Client\OffsiteInternetbanking</argument>
<argument name="handler" xsi:type="object">Magento\Payment\Gateway\Response\HandlerChain</argument>
<argument name="handler" xsi:type="object">Omise\Payment\Gateway\Response\PaymentDetailsHandler</argument>
<argument name="validator" xsi:type="object">Omise\Payment\Gateway\Validator\Offsite\InternetbankingChargeCommandResponseValidator</argument>
</arguments>
</virtualType>
Expand Down
9 changes: 9 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/orders/:order_id/omise-offsite" method="GET">
<service class="Omise\Payment\Api\PaymentInformationInterface" method="offsite"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
</routes>
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
define(
[
'jquery',
'ko',
'mage/storage',
'Magento_Checkout/js/view/payment/default',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/action/redirect-on-success',
'Magento_Checkout/js/model/quote'
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/url-builder',
'Magento_Checkout/js/model/error-processor'
],
function (
$,
ko,
storage,
Component,
fullScreenLoader,
redirectOnSuccessAction,
quote
quote,
urlBuilder,
errorProcessor
) {
'use strict';

Expand Down Expand Up @@ -81,14 +91,41 @@ define(

self.getPlaceOrderDeferredObject()
.fail(
function() {
console.log('failed');
function(response) {
errorProcessor.process(response, self.messageContainer);
fullScreenLoader.stopLoader();
self.isPlaceOrderActionAllowed(true);
}
).done(
function() {
self.afterPlaceOrder();
function(response) {
var self = this;

redirectOnSuccessAction.execute();
var serviceUrl = urlBuilder.createUrl(
'/orders/:order_id/omise-offsite',
{
order_id: response
}
);

storage.get(serviceUrl, false)
.fail(
function (response) {
errorProcessor.process(response, self.messageContainer);
fullScreenLoader.stopLoader();
self.isPlaceOrderActionAllowed(true);
}
)
.done(
function (response) {
if (response) {
$.mage.redirect(response.authorize_uri);
} else {
errorProcessor.process(response, self.messageContainer);
fullScreenLoader.stopLoader();
self.isPlaceOrderActionAllowed(true);
}
}
);
}
);

Expand Down