Skip to content

Commit

Permalink
Integrate AXO payment process
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosilva-pt committed Apr 8, 2024
1 parent 4e4f00a commit 9391865
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 22 deletions.
86 changes: 76 additions & 10 deletions modules/ppcp-axo/resources/js/AxoManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Fastlane from "./Connection/Fastlane";
import MockData from "./Helper/MockData";
import {log} from "./Helper/Debug";
import DomElementCollection from "./Components/DomElementCollection";
import ShippingView from "./Views/ShippingView";
Expand All @@ -26,6 +25,7 @@ class AxoManager {
};

this.data = {
email: null,
billing: null,
shipping: null,
card: null,
Expand Down Expand Up @@ -101,6 +101,9 @@ class AxoManager {
console.log('card response', response);

if (response.selectionChanged) {

console.log('response.selectedCard.paymentToken', response.selectedCard.paymentToken);

this.setCard(response.selectedCard);
this.setBilling({
address: response.selectedCard.paymentSource.card.billingAddress
Expand Down Expand Up @@ -433,6 +436,8 @@ class AxoManager {
}

async onChangeEmail () {
this.clearData();

if (!this.status.active) {
log('Email checking skipped, AXO not active.');
return;
Expand Down Expand Up @@ -479,9 +484,13 @@ class AxoManager {

// Add addresses
this.setShipping(authResponse.profileData.shippingAddress);
// TODO : set billing
this.setBilling({
address: authResponse.profileData.card.paymentSource.card.billingAddress
});
this.setCard(authResponse.profileData.card);

console.log('authResponse', authResponse);

this.setStatus('validEmail', true);
this.setStatus('hasProfile', true);

Expand All @@ -503,12 +512,25 @@ class AxoManager {
this.setStatus('validEmail', true);
this.setStatus('hasProfile', false);

console.log('this.cardComponentData()', this.cardComponentData());

this.cardComponent = await this.fastlane
.FastlaneCardComponent(MockData.cardComponent())
.FastlaneCardComponent(
this.cardComponentData()
)
.render(this.el.paymentContainer.selector + '-form');
}
}

clearData() {
this.data = {
email: null,
billing: null,
shipping: null,
card: null,
};
}

setShipping(shipping) {
this.data.shipping = shipping;
this.shippingView.setData(this.data);
Expand All @@ -525,12 +547,50 @@ class AxoManager {
}

onClickSubmitButton() {
try {
this.cardComponent.tokenize(MockData.cardComponentTokenize()).then((response) => {
this.submit(response.nonce);
});
} catch (e) {
log('Error tokenizing.');
if (this.data.card) { // Ryan flow
log('Ryan flow.');
this.submit(this.data.card.paymentToken);

} else { // Gary flow
log('Gary flow.');
console.log('this.tokenizeData()', this.tokenizeData());

try {
this.cardComponent.tokenize(
this.tokenizeData()
).then((response) => {
this.submit(response.nonce);
});
} catch (e) {
log('Error tokenizing.');
}
}
}

cardComponentData() {
return {
fields: {
phoneNumber: {
prefill: this.billingView.inputValue('phone')
},
cardholderName: {} // optionally pass this to show the card holder name
}
}
}

tokenizeData() {
return {
name: {
fullName: this.billingView.fullName()
},
billingAddress: {
addressLine1: this.billingView.inputValue('street1'),
addressLine2: this.billingView.inputValue('street2'),
adminArea1: this.billingView.inputValue('city'),
adminArea2: this.billingView.inputValue('stateCode'),
postalCode: this.billingView.inputValue('postCode'),
countryCode: this.billingView.inputValue('countryCode'),
}
}
}

Expand All @@ -540,7 +600,13 @@ class AxoManager {
alert('nonce: ' + nonce);

// Submit form.
// this.el.defaultSubmitButton.click();
if (!this.el.axoNonceInput.get()) {
this.$('.woocommerce-checkout').append(`<input type="hidden" id="${this.el.axoNonceInput.id}" name="axo_nonce" value="" />`);
}

this.el.axoNonceInput.get().value = nonce;

this.el.defaultSubmitButton.click();
}

useEmailWidget() {
Expand Down
7 changes: 6 additions & 1 deletion modules/ppcp-axo/resources/js/Components/DomElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ class DomElement {
}

click() {
document.querySelector(this.selector).click();
this.get().click();
}

get() {
return document.querySelector(this.selector);
}

}

export default DomElement;
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class DomElementCollection {
attributes: 'data-ppcp-axo-show-gateway-selection',
});

this.axoNonceInput = new DomElement({
id: 'ppcp-axo-nonce',
selector: '#ppcp-axo-nonce',
});

}
}

Expand Down
9 changes: 7 additions & 2 deletions modules/ppcp-axo/resources/js/Components/FormFieldGroup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

class MockData {
class FormFieldGroup {

constructor(config) {
this.data = {};
Expand Down Expand Up @@ -97,6 +97,11 @@ class MockData {
field.classList.add('ppcp-axo-field-hidden');
}
}

inputValue(name) {
return document.querySelector(this.fields[name].selector).value;
}

}

export default MockData;
export default FormFieldGroup;
12 changes: 5 additions & 7 deletions modules/ppcp-axo/resources/js/Connection/Fastlane.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ class Fastlane {
this.identity = null;
this.profile = null;
this.FastlaneCardComponent = null;
this.FastlaneEmailComponent = null;
this.FastlaneAddressComponent = null;
this.FastlanePaymentComponent = null;
this.FastlaneWatermarkComponent = null;
}

connect(config) {
return new Promise((resolve, reject) => {
window.paypal.Connect(config) // TODO: migrate from 0.6 to 0.7
window.paypal.Fastlane(config)
.then((result) => {
this.init(result);
console.log('[AXO] Connected', result);
Expand All @@ -30,10 +29,9 @@ class Fastlane {
this.connection = connection;
this.identity = this.connection.identity;
this.profile = this.connection.profile;
this.FastlaneCardComponent = this.connection.ConnectCardComponent; // TODO: migrate from 0.6 to 0.7
this.FastlaneEmailComponent = null; // TODO: migrate from 0.6 to 0.7
this.FastlaneAddressComponent = null; // TODO: migrate from 0.6 to 0.7
this.FastlaneWatermarkComponent = this.connection.ConnectWatermarkComponent // TODO: migrate from 0.6 to 0.7
this.FastlaneCardComponent = this.connection.FastlaneCardComponent;
this.FastlanePaymentComponent = this.connection.FastlanePaymentComponent;
this.FastlaneWatermarkComponent = this.connection.FastlaneWatermarkComponent

console.log('[AXO] Fastlane initialized', this);
}
Expand Down
8 changes: 8 additions & 0 deletions modules/ppcp-axo/resources/js/Views/BillingView.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class BillingView {
this.billingFormFields.setData(data);
}

inputValue(name) {
return this.billingFormFields.inputValue(name);
}

fullName() {
return `${this.inputValue('firstName')} ${this.inputValue('lastName')}`.trim();
}

}

export default BillingView;
2 changes: 1 addition & 1 deletion modules/ppcp-axo/src/AxoModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static function () use ( $c, $manager ) {
add_filter(
'woocommerce_paypal_payments_sdk_components_hook',
function( $components ) {
$components[] = 'connect';
$components[] = 'fastlane';
return $components;
}
);
Expand Down
2 changes: 1 addition & 1 deletion modules/ppcp-axo/src/Gateway/AxoGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function process_payment( $order_id ) {
$wc_order = wc_get_order( $order_id );
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );

$nonce = 'tokencc_bh_vkjzxd_tgpd3n_qmjs7k_3vhxzg_p82';
$nonce = wc_clean( wp_unslash( $_POST['axo_nonce'] ?? '' ) );

try {
$shipping_preference = $this->shipping_preference_factory->from_state(
Expand Down

0 comments on commit 9391865

Please sign in to comment.