mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 16:24:33 +08:00
Add card payment for guest free trial (WIP)
This commit is contained in:
parent
29e195e7f3
commit
c4232e3957
3 changed files with 82 additions and 2 deletions
|
@ -48,9 +48,14 @@ class CardFieldsFreeTrialRenderer {
|
||||||
errorHandler
|
errorHandler
|
||||||
);
|
);
|
||||||
|
|
||||||
const cardFields = paypal.CardFields(
|
let cardFields = paypal.CardFields(
|
||||||
|
configuration.addPaymentMethodConfiguration()
|
||||||
|
);
|
||||||
|
if ( this.defaultConfig.user.is_logged ) {
|
||||||
|
cardFields = paypal.CardFields(
|
||||||
configuration.cardFieldsConfiguration()
|
configuration.cardFieldsConfiguration()
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if ( cardFields.isEligible() ) {
|
if ( cardFields.isEligible() ) {
|
||||||
const renderCardFields = new RenderCardFields( cardFields );
|
const renderCardFields = new RenderCardFields( cardFields );
|
||||||
|
|
|
@ -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;
|
export default Configuration;
|
||||||
|
|
|
@ -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;
|
$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 );
|
WC()->session->set( 'ppcp_card_payment_token_for_free_trial', null );
|
||||||
if($card_payment_token_for_free_trial) {
|
if($card_payment_token_for_free_trial) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue