Skip to content

Commit 59daa9c

Browse files
Merge pull request #76 from checkout/dev
Plugin improvement
2 parents bc9344b + 6976516 commit 59daa9c

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

Controller/Payment/PaymentToken.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Checkout.com Magento 2 Payment module (https://www.checkout.com)
4+
*
5+
* Copyright (c) 2017 Checkout.com (https://www.checkout.com)
6+
* Author: David Fiaty | [email protected]
7+
*
8+
* License GNU/GPL V3 https://www.gnu.org/licenses/gpl-3.0.en.html
9+
*/
10+
11+
namespace CheckoutCom\Magento2\Controller\Payment;
12+
13+
use Magento\Framework\Controller\Result\JsonFactory;
14+
use Magento\Framework\App\Action\Context;
15+
use CheckoutCom\Magento2\Model\Service\PaymentTokenService;
16+
use CheckoutCom\Magento2\Gateway\Config\Config as GatewayConfig;
17+
18+
class PaymentToken extends AbstractAction {
19+
20+
/**
21+
* @var PaymentTokenService
22+
*/
23+
protected $paymentTokenService;
24+
25+
/**
26+
* @var JsonFactory
27+
*/
28+
protected $resultJsonFactory;
29+
30+
/**
31+
* PaymentToken constructor.
32+
* @param PaymentTokenService $paymentTokenService
33+
* @param JsonFactory $resultJsonFactory
34+
*/
35+
public function __construct(
36+
Context $context,
37+
PaymentTokenService $paymentTokenService,
38+
JsonFactory $resultJsonFactory,
39+
GatewayConfig $gatewayConfig
40+
)
41+
{
42+
parent::__construct($context, $gatewayConfig);
43+
$this->paymentTokenService = $paymentTokenService;
44+
$this->resultJsonFactory = $resultJsonFactory;
45+
}
46+
47+
/**
48+
* Handles the controller method.
49+
*/
50+
public function execute() {
51+
return $this->resultJsonFactory->create()->setData([
52+
'payment_token' => $this->paymentTokenService->getToken()
53+
]);
54+
}
55+
}

Model/Ui/ConfigProvider.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Magento\Store\Model\StoreManagerInterface;
1616
use CheckoutCom\Magento2\Gateway\Config\Config;
1717
use CheckoutCom\Magento2\Model\Adapter\ChargeAmountAdapter;
18-
use CheckoutCom\Magento2\Model\Service\PaymentTokenService;
1918

2019
class ConfigProvider implements ConfigProviderInterface {
2120

@@ -35,11 +34,6 @@ class ConfigProvider implements ConfigProviderInterface {
3534
*/
3635
protected $checkoutSession;
3736

38-
/**
39-
* @var PaymentTokenService
40-
*/
41-
protected $paymentTokenService;
42-
4337
/**
4438
* @var StoreManagerInterface
4539
*/
@@ -48,13 +42,11 @@ class ConfigProvider implements ConfigProviderInterface {
4842
/**
4943
* ConfigProvider constructor.
5044
* @param Config $config
51-
* @param PaymentTokenService $paymentTokenService
5245
* @param Session $checkoutSession
5346
* @param StoreManagerInterface $storeManager
5447
*/
55-
public function __construct(Config $config, PaymentTokenService $paymentTokenService, Session $checkoutSession, StoreManagerInterface $storeManager) {
48+
public function __construct(Config $config, Session $checkoutSession, StoreManagerInterface $storeManager) {
5649
$this->config = $config;
57-
$this->paymentTokenService = $paymentTokenService;
5850
$this->checkoutSession = $checkoutSession;
5951
$this->storeManager = $storeManager;
6052
}
@@ -92,7 +84,6 @@ public function getConfig() {
9284
'design_settings' => $this->config->getDesignSettings(),
9385
'accepted_currencies' => $this->config->getAcceptedCurrencies(),
9486
'payment_mode' => $this->config->getPaymentMode(),
95-
'payment_token' => $isActive ? $this->getPaymentToken() : '',
9687
'quote_value' => $this->getQuoteValue(),
9788
'quote_currency' => $this->getQuoteCurrency(),
9889
'embedded_theme' => $this->config->getEmbeddedTheme(),
@@ -107,15 +98,6 @@ public function getConfig() {
10798
];
10899
}
109100

110-
/**
111-
* Get a payment token.
112-
*
113-
* @return string
114-
*/
115-
public function getPaymentToken() {
116-
return $this->paymentTokenService->getToken();
117-
}
118-
119101
/**
120102
* Get a quote value.
121103
*

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"magento/module-vault": "*"
1212
},
1313
"type": "magento2-module",
14-
"version": "1.0.32",
14+
"version": "1.0.33",
1515
"autoload": {
1616
"files": [
1717
"registration.php"

view/frontend/web/js/view/payment/method-renderer/hosted.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ define(
5858
* @returns {string}
5959
*/
6060
getPaymentToken: function() {
61-
return CheckoutCom.getPaymentConfig()['payment_token'];
61+
// Send the request
62+
var ajaxRequest = $.ajax({
63+
url: url.build('checkout_com/payment/paymentToken'),
64+
type: "post"
65+
});
66+
67+
// Process the payment token response
68+
ajaxRequest.done(function (response, textStatus, jqXHR) {
69+
$('#paymentToken').val(response.payment_token);
70+
});
6271
},
6372

6473
/**

view/frontend/web/template/payment/hosted.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<input name="logoUrl" data-bind="attr: {value: getDesignSettings()['hosted']['logo_url']}">
4949
<!-- /ko -->
5050
<!-- ko if: (getPaymentMode() === "localpayments" || getPaymentMode() === "mixed") -->
51-
<input name="paymentToken" data-bind="attr: {value: getPaymentToken()}">
51+
<input id ="paymentToken" name="paymentToken" data-bind="attr: {value: getPaymentToken()}">
5252
<!-- /ko -->
5353
</form>
5454

0 commit comments

Comments
 (0)