From 5962e246072bd372920d7ccc6203601b466299f9 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Wed, 29 Mar 2017 14:52:47 +0700 Subject: [PATCH 1/3] Assign charge.id & charge.authorize_uri back to an order additional info. --- Gateway/Response/PaymentDetailsHandler.php | 20 ++++++++++++++++++++ etc/di.xml | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Gateway/Response/PaymentDetailsHandler.php diff --git a/Gateway/Response/PaymentDetailsHandler.php b/Gateway/Response/PaymentDetailsHandler.php new file mode 100644 index 000000000..9e7183921 --- /dev/null +++ b/Gateway/Response/PaymentDetailsHandler.php @@ -0,0 +1,20 @@ +getPayment(); + + $payment->setAdditionalInformation('charge_id', $response['data']['id']); + $payment->setAdditionalInformation('charge_authorize_uri', $response['data']['authorize_uri']); + } +} diff --git a/etc/di.xml b/etc/di.xml index 3ead5b59a..17d2efe62 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -45,7 +45,7 @@ OmiseOffsiteInternetbankingChargeRequest Omise\Payment\Gateway\Http\TransferFactory Omise\Payment\Gateway\Http\Client\OffsiteInternetbanking - Magento\Payment\Gateway\Response\HandlerChain + Omise\Payment\Gateway\Response\PaymentDetailsHandler Omise\Payment\Gateway\Validator\Offsite\InternetbankingChargeCommandResponseValidator From e768fdbbeab15894c4b311204b01449404ac8329 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Fri, 31 Mar 2017 17:52:40 +0700 Subject: [PATCH 2/3] Implement RESTFul API to retrieve a payment detail after place an order. To retrieve an authorize_uri back from charge result. --- Api/Data/PaymentInterface.php | 51 +++++++++++++++++++++ Api/PaymentInformationInterface.php | 12 +++++ Model/Data/Payment.php | 64 ++++++++++++++++++++++++++ Model/PaymentInformation.php | 69 +++++++++++++++++++++++++++++ etc/di.xml | 3 ++ etc/webapi.xml | 9 ++++ 6 files changed, 208 insertions(+) create mode 100644 Api/Data/PaymentInterface.php create mode 100644 Api/PaymentInformationInterface.php create mode 100644 Model/Data/Payment.php create mode 100644 Model/PaymentInformation.php create mode 100644 etc/webapi.xml diff --git a/Api/Data/PaymentInterface.php b/Api/Data/PaymentInterface.php new file mode 100644 index 000000000..6bcca8133 --- /dev/null +++ b/Api/Data/PaymentInterface.php @@ -0,0 +1,51 @@ +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); + } +} diff --git a/Model/PaymentInformation.php b/Model/PaymentInformation.php new file mode 100644 index 000000000..bcc906f09 --- /dev/null +++ b/Model/PaymentInformation.php @@ -0,0 +1,69 @@ +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')); + } +} diff --git a/etc/di.xml b/etc/di.xml index 17d2efe62..fb98ea48f 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -1,5 +1,8 @@ + + + diff --git a/etc/webapi.xml b/etc/webapi.xml new file mode 100644 index 000000000..96ee6919b --- /dev/null +++ b/etc/webapi.xml @@ -0,0 +1,9 @@ + + + + + + + + + From 3bdb74ca3cbbbddc0057dd32f7276c7ec43410c0 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Fri, 31 Mar 2017 18:09:08 +0700 Subject: [PATCH 3/3] Handle place order action, either redirect buyer out if success or raise an error message if it fail. --- .../omise-offsite-internetbanking-method.js | 51 ++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/view/frontend/web/js/view/payment/method-renderer/omise-offsite-internetbanking-method.js b/view/frontend/web/js/view/payment/method-renderer/omise-offsite-internetbanking-method.js index cd82797d8..316bd77d4 100644 --- a/view/frontend/web/js/view/payment/method-renderer/omise-offsite-internetbanking-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/omise-offsite-internetbanking-method.js @@ -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'; @@ -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); + } + } + ); } );