Skip to content

Commit

Permalink
Merge pull request #76 from MONEI/feature/update-legacy-checkout-methods
Browse files Browse the repository at this point in the history
Feature/update legacy checkout methods
  • Loading branch information
malmo-monei authored Nov 21, 2024
2 parents 59d8adc + 83bb470 commit 56d866f
Show file tree
Hide file tree
Showing 22 changed files with 719 additions and 279 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
[class*="payment_method_monei"] label {

}

label .monei-icons{
float:right;
margin-left: 15px;
margin-right:0;
padding-right:0;
max-height: 28px !important;
max-width: 80px !important;
}
.monei-icons svg{
float:right;
width: auto;
}

/* Applies to the apple/google button in checkout*/
Expand Down
1 change: 0 additions & 1 deletion assets/js/cofidis.min.js

This file was deleted.

197 changes: 197 additions & 0 deletions assets/js/monei-bizum-classic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
(function( $ ) {
'use strict';

// Checkout form.
$( document.body ).on(
'updated_checkout',
function(e, data) {
// Update cofidis_widget.total on every updated_checkout event.
if ( 'object' === typeof( data ) && data.fragments && data.fragments[ 'monei_new_total' ] ) {
wc_bizum_form.total = data.fragments[ 'monei_new_total' ];
}

if ( wc_bizum_form.is_bizum_selected() ) {
wc_bizum_form.init_checkout_bizum();
}
}
);

// Add Payment Method form.
$( 'form#add_payment_method' ).on(
'click payment_methods',
function() {
if ( wc_bizum_form.is_bizum_selected() ) {
wc_bizum_form.init_checkout_bizum();
}
}
);

// On Pay for order form.
$( 'form#order_review' ).on(
'click',
function() {
if ( wc_bizum_form.is_bizum_selected() ) {
wc_bizum_form.init_checkout_bizum();
}
}
);

var targetNode = document.getElementById('order_review');

if (targetNode) {
var observer = new MutationObserver(function(mutationsList, observer) {
for (var mutation of mutationsList) {
if (mutation.type === 'childList') {
if ( wc_bizum_form.is_bizum_selected() ) {
wc_bizum_form.on_payment_selected();
}
}
}
});

observer.observe(targetNode, { childList: true, subtree: true });
}

var wc_bizum_form = {
$checkout_form: $( 'form.woocommerce-checkout' ),
$add_payment_form: $( 'form#add_payment_method' ),
$order_pay_form: $( 'form#order_review' ),
$container: null,
$paymentForm: null,
is_checkout: false,
is_add_payment_method: false,
is_order_pay: false,
form: null,
submitted: false,
init_counter: 0,
total: wc_bizum_params.total,
init: function() {
// Checkout Page
if ( this.$checkout_form.length ) {
this.is_checkout = true;
this.form = this.$checkout_form;
}

// Add payment method Page
if ( this.$add_payment_form.length ) {
this.is_add_payment_method = true;
this.form = this.$add_payment_form;
}

// Pay for order ( change_payment_method for subscriptions)
if ( this.$order_pay_form.length ) {
if ( wc_bizum_form.is_bizum_selected() ) {
wc_bizum_form.init_checkout_bizum();
}
this.is_order_pay = true;
this.form = this.$order_pay_form;
console.log('TOTAL',this.form)
}

if ( this.form ) {
this.form.on( 'change', this.on_change );
}
},
on_change: function() {
// Triggers on payment method selection.
$( "[name='payment_method']" ).on(
'change',
function() {
wc_bizum_form.on_payment_selected();
}
);
},
on_payment_selected() {
if ( wc_bizum_form.is_bizum_selected() ) {
wc_bizum_form.init_checkout_bizum();
if ( wc_bizum_form.is_checkout ) {
$( "[name='woocommerce_checkout_place_order']" ).attr( 'bizum-data-monei', 'submit' );
}
$('#place_order').prop('disabled', true);
} else {
if ( wc_bizum_form.is_checkout ) {
$( "[name='woocommerce_checkout_place_order']" ).removeAttr( 'bizum-data-monei' );
}
//todo central state. If Apple is selected we dont want to mess with the disable after it
if(!wc_bizum_form.is_apple_selected()){
$('#place_order').prop('disabled', false);
}
}
},
is_bizum_selected: function() {
return $( '#payment_method_monei_bizum' ).is( ':checked' );
},
is_apple_selected: function() {
return $( '#payment_method_monei_apple_google' ).is( ':checked' );
},
init_bizum_component: function() {
if ( window.bizumRequest ) {
window.bizumRequest.close();
}
wc_bizum_form.instantiate_payment_request();
},
instantiate_payment_request: function() {
var paymentRequest = monei.Bizum({
accountId: wc_bizum_params.account_id,
sessionId: wc_bizum_params.session_id,
amount: parseInt( wc_bizum_form.total ),
currency: wc_bizum_params.currency,
onSubmit(result) {
$('#place_order').prop('disabled', false);
wc_bizum_form.request_token_handler( result.token );
},
onError(error) {
console.error(error);
},
});
// Render an instance of the Payment Request Component into the `payment_request_container` <div>.
paymentRequest.render('#bizum-container');
// Assign a global variable to paymentRequest so it's accessible.
window.bizumRequest = paymentRequest;
},
init_checkout_bizum: function() {
// If checkout is updated (and monei was initiated already), ex, selecting new shipping methods, checkout is re-render by the ajax call.
// and we need to reset the counter in order to initiate again the monei component.
if ( wc_bizum_form.$container && 0 === wc_bizum_form.$container.childElementCount ) {
wc_bizum_form.init_counter = 0;
}

// init monei just once, despite how many times this may be triggered.
if ( 0 !== this.init_counter ) {
return;
}

if ( wc_bizum_form.is_checkout ) {
$( "[name='woocommerce_checkout_place_order']" ).attr( 'bizum-data-monei', 'submit' );
}

wc_bizum_form.init_bizum_component();
wc_bizum_form.$container = document.getElementById( 'bizum-container' );

// We already init Bizum.
this.init_counter++;
},
request_token_handler: function (token ) {
wc_bizum_form.create_hidden_input( 'monei_payment_request_token', token );
// Once Token is created, submit form.
$('#place_order').prop('disabled', false);
wc_bizum_form.form.submit();
},
create_hidden_input: function( id, token ) {
var hiddenInput = document.createElement( 'input' );
hiddenInput.setAttribute( 'type', 'hidden' );
hiddenInput.setAttribute( 'name', id );
hiddenInput.setAttribute( 'id', id );
hiddenInput.setAttribute( 'value', token );
wc_bizum_form.$paymentForm = document.getElementById( 'monei-bizum-form' );
wc_bizum_form.$paymentForm.appendChild( hiddenInput );
}
};

$(
function() {
wc_bizum_form.init();
}
);

})( jQuery );
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
* Initialize MONEI card input and handle token creation.
*/
const initMoneiCard = () => {
console.log( parseInt( bizumData.total * 100 ) );
const bizum = monei.Bizum( {
accountId: bizumData.accountId,
sessionId: bizumData.sessionId,
Expand All @@ -65,7 +64,7 @@
}
},
onError( error ) {
console.log( error );
console.error( error );
},
} );

Expand All @@ -90,7 +89,7 @@
type: responseTypes.SUCCESS,
meta: {
paymentMethodData: {
monei_payment_token: requestToken,
monei_payment_request_token: requestToken,
monei_is_block_checkout: 'yes',
},
},
Expand All @@ -105,19 +104,17 @@
const unsubscribeSuccess = onCheckoutSuccess(
( { processingResponse } ) => {
const { paymentDetails } = processingResponse;
// Ensure we have the paymentId from the server
if ( paymentDetails && paymentDetails.paymentId ) {
const paymentId = paymentDetails.paymentId;

const tokenValue = paymentDetails.token;
// Call monei.confirmPayment to complete the payment (with 3D Secure)
monei
.confirmPayment( {
console.log(typeof paymentId)
console.log({
paymentId,
paymentToken: tokenValue})
monei.confirmPayment( {
paymentId,
tokenValue,
} )
paymentToken: tokenValue} )
.then( ( result ) => {
console.log( 'confirmed', result );
if (
result.nextAction &&
result.nextAction.mustRedirect
Expand All @@ -134,7 +131,7 @@
}
} )
.catch( ( error ) => {
console.log(
console.error(
'Error during payment confirmation:',
error
);
Expand Down Expand Up @@ -197,5 +194,6 @@
},
supports: bizumData.supports,
};
console.log('hola bizum 6')
registerPaymentMethod( MoneiBizumPaymentMethod );
} )();
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
}
} )
.catch( ( error ) => {
console.log(
console.error(
'Error during payment confirmation:',
error
);
Expand Down Expand Up @@ -412,7 +412,8 @@
}
},
onError( error ) {
console.log( error );
console.error( error );
console.error( error );
},
} );

Expand Down
Loading

0 comments on commit 56d866f

Please sign in to comment.