Skip to content

Commit aa0b51a

Browse files
authored
Merge pull request #1065 from payplug/PRE-2955-hf-with-save-card
Pre 2955 hf with save card
2 parents 63c40c9 + f989723 commit aa0b51a

9 files changed

+206
-56
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('jquery', 'react', 'wc-blocks-registry', 'wc-settings', 'wp-data', 'wp-html-entities', 'wp-i18n'), 'version' => '6ac2d83bddc2eb19ddab');
1+
<?php return array('dependencies' => array('jquery', 'react', 'wc-blocks-registry', 'wc-settings', 'wp-data', 'wp-html-entities', 'wp-i18n'), 'version' => 'eb05e0507eccb0282a25');

assets/js/blocks/wc-payplug-payplug-blocks.js

Lines changed: 35 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/blocks/wc-payplug-payplug-blocks.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/payplug-hostedfields.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ var HostedFields = {
129129

130130
if (isValid) {
131131
document.getElementById("hftoken").value = result.hfToken;
132+
document.getElementById("card-last4").value = result.cardCode ? result.cardCode.slice(-4) : "";
133+
document.getElementById("card-expiry").value = document.getElementById("card-expiry").value = result.cardValidityDate ? result.cardValidityDate.replace("-", "/") : "";
132134

133135
// Ensure no duplicate listeners
134136
jQuery('form.woocommerce-checkout, form#order_review').off('submit', HostedFields.tokenizeHandler);

resources/js/frontend/wc-payplug-hosted-fields-blocks.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import {getSetting} from '@woocommerce/settings';
2-
import React, {useEffect} from 'react';
3-
4-
const settings = getSetting('payplug_data', {});
1+
import { getSetting } from '@woocommerce/settings';
2+
import React, { useEffect, useState } from 'react';
3+
const settings = getSetting( 'payplug_data', {} );
54
const $ = jQuery;
65

76
var style = {
8-
'input': {
9-
'font-size': '1em',
10-
'background-color': 'transparent',
7+
"input": {
8+
"font-size": "1em",
9+
"background-color": "transparent",
1110
},
12-
'::placeholder': {
13-
'font-size': '1em',
14-
'color': '#777',
15-
'font-style': 'italic'
11+
"::placeholder": {
12+
"font-size": "1em",
13+
"color": "#777",
14+
"font-style": "italic"
1615
},
17-
':invalid': {
18-
'color': '#FF0000',
19-
'font-size': '1em'
16+
":invalid": {
17+
"color": "#FF0000",
18+
"font-size": "1em"
2019
}
2120
};
2221

@@ -125,10 +124,13 @@ var HostedFields = {
125124
}
126125
}
127126
};
127+
let saved_card = false;
128128

129129
const IntegratedPayment = typeof dalenys !== 'undefined' ? ({props: props,}) => {
130-
const {eventRegistration, emitResponse} = props;
131-
const {onCheckoutValidation, onPaymentProcessing} = eventRegistration;
130+
131+
const { eventRegistration, emitResponse } = props;
132+
saved_card = props.shouldSavePayment;
133+
const { onCheckoutValidation, onPaymentProcessing } = eventRegistration;
132134

133135
//on init
134136
useEffect(() => {
@@ -141,12 +143,18 @@ const IntegratedPayment = typeof dalenys !== 'undefined' ? ({props: props,}) =>
141143
useEffect(() => {
142144
const handlePaymentProcessing = () => {
143145
const hftoken = document.getElementById('hf-token').value;
146+
const savedCard = document.getElementById('saved_card').value;
147+
const cardLast4 = document.getElementById('card-last4').value;
148+
const cardExpiry = document.getElementById('card-expiry').value;
144149

145150
return {
146151
type: emitResponse.responseTypes.SUCCESS,
147152
meta: {
148153
paymentMethodData: {
149154
hftoken,
155+
savedCard,
156+
cardLast4,
157+
cardExpiry
150158
},
151159
},
152160
};
@@ -185,6 +193,9 @@ const IntegratedPayment = typeof dalenys !== 'undefined' ? ({props: props,}) =>
185193
HostedFields.hfields.createToken(function (result) {
186194
if (HostedFields.submitValidation($('[name=hosted-fields-cardHolder]'), $('.IntegratedPayment_error.-cardHolder .invalidField')) && result.execCode == '0000') {
187195
document.getElementById('hf-token').value = result.hfToken;
196+
// Save last 4 and expiry to hidden fields
197+
document.getElementById('card-last4').value = result.cardCode ? result.cardCode.slice(-4) : "";
198+
document.getElementById('card-expiry').value = result.cardValidityDate ? result.cardValidityDate.replace("-", "/") : "";
188199
resolve(result);
189200
} else {
190201
reject(new Error('Tokenization failed'));
@@ -272,6 +283,10 @@ const IntegratedPayment = typeof dalenys !== 'undefined' ? ({props: props,}) =>
272283
target='_blank'>{settings?.payplug_integrated_payment_privacy_policy}</a>
273284
</div>
274285
<input type='hidden' name='hf-token' id='hf-token'/>
286+
<input type='hidden' name='saved_card' id='saved_card' value={saved_card ? '1' : '0'} />
287+
<input type='hidden' name='card-last4' id='card-last4'/>
288+
<input type='hidden' name='card-expiry' id='card-expiry'/>
289+
275290
</>
276291
)
277292
} : null;

src/Gateway/PayplugCreditCard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public function scheduled_subscription_payment($amount, $order)
318318
$metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
319319
PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
320320

321-
$this->response->process_payment($payment, true);
321+
$this->response->process_payment($payment,true);
322322

323323
if (($payment->__get('is_paid'))) {
324324
$redirect = $order->get_checkout_order_received_url();

src/Gateway/PayplugGateway.php

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ public function validate_payment($id = null, $save_request = true, $ipn = false)
332332
if (strpos($transaction_id, 'pay_') !== 0) {
333333
$payment = $this->api->payment_retrieve( $this->hosted_fields->populateGetTransaction($transaction_id), true );
334334
$payment->is_live = $this->get_current_mode() === 'live';
335+
$payment->alias = get_post_meta($order_id, '_transaction_alias', true);
335336

336337
//FIXME:: HF the amount unit is EURO not Cents
337338
$payment->amount = $payment->amount * 100;
@@ -668,7 +669,7 @@ public function filter_tokens($tokens, $user_id, $gateway_id)
668669
}
669670

670671
if ($current_year === (int) $token->get_expiry_year() && $current_month > (int) $token->get_expiry_month()) {
671-
unset($tokens[$k]);
672+
unset($tokens[$k]);
672673
continue;
673674
}
674675
}
@@ -878,8 +879,9 @@ public function process_standard_payment($order, $amount, $customer_id)
878879
];
879880

880881
if ($this->payment_method === 'integrated' && (defined('USE_HOSTED_FIELDS') && USE_HOSTED_FIELDS) && isset($_POST['hftoken'])) {
882+
$saved_card = isset($_POST['savedcard']) && $_POST['savedcard'] === '1';
881883
$hf_token = filter_var($_POST['hftoken'], FILTER_SANITIZE_STRING);
882-
$payment_data = $this->hosted_fields->populateCreatePayment($payment_data, $order, $order_id, $hf_token, $amount);
884+
$payment_data = $this->hosted_fields->populateCreatePayment($payment_data, $order, $order_id, $hf_token, $amount, $saved_card);
883885
$payment_data['metadata']['woocommerce_block'] = "HOSTED_FIELDS";
884886
}
885887

@@ -909,14 +911,32 @@ public function process_standard_payment($order, $amount, $customer_id)
909911
*/
910912
$payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
911913
$payment = $this->api->payment_create($payment_data);
914+
$alias = isset($payment['ALIAS']) ? $payment['ALIAS'] : null;
912915

913916
//TODO:: payment is not created we need to generate an error
914917
//HF VS NORMAL
915918
$payment_id = isset($payment['TRANSACTIONID']) ? $payment['TRANSACTIONID'] : $payment->id;
916-
917919
if (!empty($payment_id) && strpos($payment_id, 'pay_') !== 0) {
918-
$payment = $this->api->payment_retrieve( $this->hosted_fields->populateGetTransaction($payment_id), true );
920+
$payment = $this->api->payment_retrieve( $this->hosted_fields->populateGetTransaction($payment_id),true );
919921
$payment->is_live = $this->get_current_mode() === 'live';
922+
if (isset($alias)){
923+
$user_id = $payment->metadata['customer_id'];
924+
// Get and sanitize POST values
925+
$card_last4 = isset($_POST['cardlast4']) ? filter_var($_POST['cardlast4'], FILTER_SANITIZE_STRING) :'';
926+
$expiry_date = isset($_POST['cardexpiry']) ? filter_var($_POST['cardexpiry'], FILTER_SANITIZE_STRING):'';
927+
if (preg_match('/^(\d{2})\/(\d{2})$/', $expiry_date, $matches)) {
928+
$set_expiry_year = '20' . $matches[2];
929+
$set_expiry_month = $matches[1];
930+
} else {
931+
$set_expiry_year = '';
932+
$set_expiry_month = '';
933+
}
934+
update_user_meta($order->get_customer_id(), 'payplug_customer_id', $user_id);
935+
update_user_meta($order->get_customer_id(), 'payplug_card_last4', $card_last4);
936+
update_user_meta($order->get_customer_id(), 'payplug_card_expiry_year', $set_expiry_year);
937+
update_user_meta($order->get_customer_id(), 'payplug_card_expiry_month', $set_expiry_month);
938+
939+
}
920940
}
921941

922942
//TODO if payment has 5xxxx error
@@ -926,6 +946,12 @@ public function process_standard_payment($order, $amount, $customer_id)
926946
? update_post_meta($order_id, '_transaction_id', $payment_id)
927947
: $order->set_transaction_id($payment_id);
928948

949+
if ($alias) {
950+
update_post_meta($order_id, '_transaction_alias', $alias);
951+
}
952+
953+
$order_alias = get_post_meta($order_id, '_transaction_alias', true);
954+
929955
$order->set_payment_method( $this->id );
930956
$order->set_payment_method_title($this->method_title);
931957

@@ -976,7 +1002,6 @@ public function process_standard_payment($order, $amount, $customer_id)
9761002
*/
9771003
public function process_payment_with_token($order, $amount, $customer_id, $token_id)
9781004
{
979-
9801005
$order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
9811006
$payment_token = WC_Payment_Tokens::get($token_id);
9821007
if (!$payment_token || (int) $customer_id !== (int) $payment_token->get_user_id()) {
@@ -1014,6 +1039,16 @@ public function process_payment_with_token($order, $amount, $customer_id, $token
10141039
],
10151040
];
10161041

1042+
if ( (defined('USE_HOSTED_FIELDS') && USE_HOSTED_FIELDS)) {
1043+
1044+
if ( $payment_token->get_gateway_id() =='payplug' && strpos($payment_token->get_token(), 'card') !== 0) {
1045+
$payment_data = $this->hosted_fields->populateCreateWithAliasPayment($payment_data, $order, $order_id, $amount, $payment_token->get_token());
1046+
$payment_data['metadata']['woocommerce_block'] = "HOSTED_FIELDS";
1047+
}
1048+
1049+
}
1050+
1051+
10171052
$is_subscription = PayplugWoocommerceHelper::is_subscription();
10181053
if( !empty($is_subscription) && $is_subscription === true ){
10191054
$payment_data['metadata']['subscription'] = 'subscription';
@@ -1024,10 +1059,12 @@ public function process_payment_with_token($order, $amount, $customer_id, $token
10241059
$payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
10251060
$payment = $this->api->payment_create($payment_data);
10261061

1062+
$payment_id = isset($payment['TRANSACTIONID']) ? $payment['TRANSACTIONID'] : $payment->id;
1063+
10271064
// Save transaction id for the order
10281065
PayplugWoocommerceHelper::is_pre_30()
1029-
? update_post_meta($order_id, '_transaction_id', $payment->id)
1030-
: $order->set_transaction_id($payment->id);
1066+
? update_post_meta($order_id, '_transaction_id', $payment_id)
1067+
: $order->set_transaction_id($payment_id);
10311068

10321069
if (is_callable([$order, 'save'])) {
10331070
$order->save();
@@ -1042,18 +1079,26 @@ public function process_payment_with_token($order, $amount, $customer_id, $token
10421079

10431080
$this->response->process_payment($payment, true);
10441081

1045-
if(($payment->__get('is_paid'))){
1082+
if (isset($payment['EXECCODE'] )&&( $payment['EXECCODE'] == 0000))
1083+
{
10461084
$redirect = $order->get_checkout_order_received_url();
1047-
}else if(isset($payment->__get('hosted_payment')->payment_url)){
1048-
$redirect = $payment->__get('hosted_payment')->payment_url;
1049-
}else{
1085+
} else if(($payment->__get('is_paid'))){
1086+
$redirect = $order->get_checkout_order_received_url();
1087+
} else {
10501088
$redirect = $return_url;
10511089
}
10521090

1091+
if (isset($payment['EXECCODE']) )
1092+
{
1093+
$is_paid = $payment['EXECCODE'] == 00000 ? true : false;
1094+
} else {
1095+
$is_paid = $payment->__get('is_paid');
1096+
}
1097+
10531098
return [
1054-
'payment_id' => $payment->id,
1099+
'payment_id' => $payment_id,
10551100
'result' => 'success',
1056-
'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2
1101+
'is_paid' => $is_paid, // Use for path redirect before DSP2
10571102
'redirect' => $redirect
10581103
];
10591104
} catch (HttpException $e) {

0 commit comments

Comments
 (0)