Skip to content

Commit eaf646d

Browse files
authored
Merge commit from fork
Release 3.18.1
2 parents d20cb3d + fe9d290 commit eaf646d

4 files changed

Lines changed: 40 additions & 61 deletions

File tree

202/build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<property name="src-dir" value="${basedir}" />
3333
<property name="TARGETNAME" value="paypal" />
3434
<property name="TARGETBRANCH" value="${env.GIT_BRANCH}" />
35-
<property name="TARGETVERSION" value="3.18.0" />
35+
<property name="TARGETVERSION" value="3.18.1" />
3636
<property name="PHPVERSION" value="5.6" />
3737

3838
<target name="build" depends="build-common,package-zip,psvalidator" />

controllers/front/validateBnpl.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,37 @@ public function initContent()
6161
return;
6262
}
6363

64-
$transaction = $this->getTransactionFromRequest();
64+
$input = Tools::getValue('paymentData', '');
65+
66+
if (is_string($input)) {
67+
$input = json_decode($input, true);
68+
}
69+
6570
$cart = $this->context->cart;
6671

72+
if (empty($input['orderID']) || !$this->validateOrderID($input['orderID'])) {
73+
return $this->redirectToErrorPage(new Exception('Payment data is not valid.'));
74+
}
75+
if (false === Validate::isLoadedObject($cart)) {
76+
return $this->redirectToErrorPage(new Exception('Cart is not valid.'));
77+
}
78+
79+
$response = $this->client->execute(new OrdersCaptureRequest($input['orderID']));
80+
81+
if ($response->getCode() > 299 || $response->getCode() < 200) {
82+
return $this->redirectToErrorPage(new Exception('Capture is failed.'));
83+
}
84+
85+
if ($response instanceof HttpJsonResponse) {
86+
$transaction = $this->preapreTransaction($response->toArray());
87+
} else {
88+
$transaction = $this->preapreTransaction([]);
89+
}
90+
6791
try {
6892
$this->module->validateOrder(
6993
$cart->id,
70-
$this->getIdOrderState(),
94+
$this->getIdOrderState($transaction->getPaymentStatus()),
7195
$transaction->getTotalPaid(),
7296
$this->module->displayName,
7397
$this->module->l('Payment accepted.'),
@@ -91,16 +115,14 @@ public function initContent()
91115
Tools::redirect('index.php?' . http_build_query($queryParams));
92116
}
93117

94-
protected function getTransactionFromRequest()
118+
protected function preapreTransaction($paymentData)
95119
{
96120
$transaction = new Transaction();
97121

98-
if (empty(Tools::getValue('paymentData'))) {
122+
if (empty($paymentData)) {
99123
return $transaction;
100124
}
101125

102-
$paymentData = json_decode(Tools::getValue('paymentData'), true);
103-
104126
if (false === empty($paymentData['purchase_units'][0]['payments']['captures'][0]['id'])) {
105127
$transaction->setIdTransaction($paymentData['purchase_units'][0]['payments']['captures'][0]['id']);
106128
}
@@ -128,9 +150,13 @@ protected function getTransactionFromRequest()
128150
return $transaction;
129151
}
130152

131-
protected function getIdOrderState()
153+
protected function getIdOrderState($paymentStatus = PayPal::CAPTURE_STATUS_COMPLETED)
132154
{
133-
return (int) Configuration::get('PS_OS_PAYMENT');
155+
if ($paymentStatus === PayPal::CAPTURE_STATUS_COMPLETED) {
156+
return (int) Configuration::get('PS_OS_PAYMENT');
157+
}
158+
159+
return (int) Configuration::get('PS_OS_PAYPAL');
134160
}
135161

136162
protected function redirectToErrorPage($e)
@@ -172,39 +198,13 @@ public function displayAjaxCreateOrder()
172198
die(json_encode($return));
173199
}
174200

175-
public function displayAjaxCaptureOrder()
201+
protected function validateOrderID($orderID)
176202
{
177-
$order = json_decode(Tools::getValue('order'), true);
178-
$return = [
179-
'success' => false,
180-
];
181-
182-
if ($this->validateOrderID($order)) {
183-
$response = $this->client->execute(new OrdersCaptureRequest($order['orderID']));
184-
185-
if ($response->getCode() < 300 && $response->getCode() > 199) {
186-
$return['success'] = true;
187-
188-
if ($response instanceof HttpJsonResponse) {
189-
$return = array_merge($return, $response->toArray());
190-
}
191-
}
192-
}
193-
194-
die(json_encode($return));
195-
}
196-
197-
protected function validateOrderID($order)
198-
{
199-
if (empty($order['orderID'])) {
200-
return false;
201-
}
202-
203-
if (Validate::isCleanHtml($order['orderID']) === false) {
203+
if (Validate::isCleanHtml($orderID) === false) {
204204
return false;
205205
}
206206

207-
if (mb_strlen($order['orderID']) > 36) {
207+
if (mb_strlen($orderID) > 36) {
208208
return false;
209209
}
210210

paypal.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282

8383
class PayPal extends PaymentModule
8484
{
85+
const CAPTURE_STATUS_COMPLETED = 'COMPLETED';
8586
protected $_html = '';
8687
public $_errors = [];
8788
public $context;

views/js/bnpl.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ BNPL.prototype.render = function (container, order, onIsNotEligible) {
4949
}.bind(this),
5050

5151
onApprove: function(data, actions) {
52-
return this.captureOrder(data)
53-
.then(function(detail) {
54-
if (detail.success) {
55-
this.validateOrder(detail);
56-
}
57-
}.bind(this))
52+
return this.validateOrder(data)
5853
}.bind(this)
5954
});
6055

@@ -89,23 +84,6 @@ BNPL.prototype.getIdOrder = function() {
8984
});
9085
}
9186

92-
BNPL.prototype.captureOrder = function(data) {
93-
if (this.validationController === null) {
94-
return;
95-
}
96-
97-
var url = new URL(this.validationController);
98-
var form = new FormData();
99-
url.searchParams.append('ajax', '1');
100-
url.searchParams.append('action', 'CaptureOrder');
101-
form.append('order', JSON.stringify(data));
102-
103-
return fetch(url, {method: 'POST', body: form})
104-
.then(function(response) {
105-
return response.json();
106-
});
107-
}
108-
10987
BNPL.prototype.validateOrder = function(detail) {
11088
if (this.validationController === null) {
11189
return;

0 commit comments

Comments
 (0)