Add card payment for guest free trial (WIP)

This commit is contained in:
Emili Castells Guasch 2024-07-25 12:08:59 +02:00
parent 29e195e7f3
commit c4232e3957
3 changed files with 82 additions and 2 deletions

View file

@ -48,9 +48,14 @@ class CardFieldsFreeTrialRenderer {
errorHandler
);
const cardFields = paypal.CardFields(
let cardFields = paypal.CardFields(
configuration.addPaymentMethodConfiguration()
);
if ( this.defaultConfig.user.is_logged ) {
cardFields = paypal.CardFields(
configuration.cardFieldsConfiguration()
);
}
if ( cardFields.isEligible() ) {
const renderCardFields = new RenderCardFields( cardFields );

View file

@ -194,6 +194,65 @@ class Configuration {
},
};
}
addPaymentMethodConfiguration() {
return {
createVaultSetupToken: async () => {
const response = await fetch(
this.ppcp_add_payment_method.ajax.create_setup_token
.endpoint,
{
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( {
nonce: this.ppcp_add_payment_method.ajax
.create_setup_token.nonce,
payment_method: getCurrentPaymentMethod(),
} ),
}
);
const result = await response.json();
if ( result.data.id ) {
return result.data.id;
}
console.error( result );
},
onApprove: async ( { vaultSetupToken } ) => {
const response = await fetch(
this.ppcp_add_payment_method.ajax
.create_payment_token_for_guest.endpoint,
{
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( {
nonce: this.ppcp_add_payment_method.ajax
.create_payment_token_for_guest.nonce,
vault_setup_token: vaultSetupToken,
} ),
}
);
const result = await response.json();
if ( result.success === true ) {
document.querySelector( '#place_order' ).click();
return;
}
console.error( result );
},
onError: ( error ) => {
console.error( error );
},
};
}
}
export default Configuration;

View file

@ -434,6 +434,22 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
);
}
$guest_card_payment_for_free_trial = WC()->session->get( 'ppcp_guest_payment_for_free_trial' ) ?? null;
WC()->session->get( 'ppcp_guest_payment_for_free_trial', null );
if($guest_card_payment_for_free_trial) {
$customer_id = $guest_card_payment_for_free_trial->customer->id ?? '';
if ( $customer_id ) {
update_user_meta( $wc_order->get_customer_id(), '_ppcp_target_customer_id', $customer_id );
}
if ( isset( $guest_card_payment_for_free_trial->payment_source->card ) ) {
$this->wc_payment_tokens->create_payment_token_card( $wc_order->get_customer_id(), $guest_card_payment_for_free_trial );
$wc_order->payment_complete();
return $this->handle_payment_success( $wc_order );
}
}
$card_payment_token_for_free_trial = WC()->session->get( 'ppcp_card_payment_token_for_free_trial') ?? null;
WC()->session->set( 'ppcp_card_payment_token_for_free_trial', null );
if($card_payment_token_for_free_trial) {