Add free trial block checkout support for card payment

This commit is contained in:
Emili Castells Guasch 2024-07-30 11:37:03 +02:00
parent d0b8f4ccba
commit 19fd6c806d
4 changed files with 79 additions and 13 deletions

View file

@ -10,7 +10,7 @@
"Edge >= 14"
],
"dependencies": {
"@paypal/react-paypal-js": "^8.3.0",
"@paypal/react-paypal-js": "^8.5.0",
"core-js": "^3.25.0",
"react": "^17.0.0",
"react-dom": "^17.0.0"

View file

@ -7,7 +7,12 @@ import {
} from '@paypal/react-paypal-js';
import { CheckoutHandler } from './checkout-handler';
import { createOrder, onApprove } from '../card-fields-config';
import {
createOrder,
onApprove,
createVaultSetupToken,
onApproveSavePayment,
} from '../card-fields-config';
import { cartHasSubscriptionProducts } from '../Helper/Subscription';
export function CardFields( {
@ -70,8 +75,21 @@ export function CardFields( {
} }
>
<PayPalCardFieldsProvider
createOrder={ createOrder }
onApprove={ onApprove }
createVaultSetupToken={
config.scriptData.is_free_trial_cart
? createVaultSetupToken
: undefined
}
createOrder={
config.scriptData.is_free_trial_cart
? undefined
: createOrder
}
onApprove={
config.scriptData.is_free_trial_cart
? onApproveSavePayment
: onApprove
}
onError={ ( err ) => {
console.error( err );
} }

View file

@ -44,3 +44,51 @@ export async function onApprove( data ) {
console.error( err );
} );
}
export async function createVaultSetupToken() {
const config = wc.wcSettings.getSetting( 'ppcp-credit-card-gateway_data' );
return fetch( config.scriptData.ajax.create_setup_token.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( {
nonce: config.scriptData.ajax.create_setup_token.nonce,
payment_method: 'ppcp-credit-card-gateway',
} ),
} )
.then( ( response ) => response.json() )
.then( ( result ) => {
console.log( result );
return result.data.id;
} )
.catch( ( err ) => {
console.error( err );
} );
}
export async function onApproveSavePayment( { vaultSetupToken } ) {
const config = wc.wcSettings.getSetting( 'ppcp-credit-card-gateway_data' );
const response = await fetch(
config.scriptData.ajax.create_payment_token.endpoint,
{
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( {
nonce: config.scriptData.ajax.create_payment_token.nonce,
vault_setup_token: vaultSetupToken,
is_free_trial_cart: config.scriptData.is_free_trial_cart,
} ),
}
);
const result = await response.json();
if ( result.success !== true ) {
console.error( result );
}
}

View file

@ -1005,19 +1005,19 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"
"@paypal/paypal-js@^8.0.5":
version "8.0.5"
resolved "https://registry.yarnpkg.com/@paypal/paypal-js/-/paypal-js-8.0.5.tgz#77bc461b4d1e5a2c6f081269e3ef0b2e3331a68c"
integrity sha512-yQNV7rOILeaVCNU4aVDRPqEnbIlzfxgQfFsxzsBuZW1ouqRD/4kYBWJDzczCiscSr2xOeA/Pkm7e3a9fRfnuMQ==
"@paypal/paypal-js@^8.1.0":
version "8.1.0"
resolved "https://registry.yarnpkg.com/@paypal/paypal-js/-/paypal-js-8.1.0.tgz#4e7d10e0a0b4164985029cfdac748e5694d117e9"
integrity sha512-f64bom5xYwmxyeKPJUFS/XpM0tXojQEgjRIADPqe1R9WmK+PFqL4SEkT85cGU0ZXLVx4EGbjwREHhqEOR+OstA==
dependencies:
promise-polyfill "^8.3.0"
"@paypal/react-paypal-js@^8.3.0":
version "8.3.0"
resolved "https://registry.yarnpkg.com/@paypal/react-paypal-js/-/react-paypal-js-8.3.0.tgz#a103080b752766b8ff59b8620887abf802e1a01b"
integrity sha512-SX17d2h1CMNFGI+wtjb329AEDaBR8Ziy2LCV076eDcY1Q0MFKRkfQ/v0HOAvZtk3sJoydRmYez2pq47BRblwqQ==
"@paypal/react-paypal-js@^8.5.0":
version "8.5.0"
resolved "https://registry.yarnpkg.com/@paypal/react-paypal-js/-/react-paypal-js-8.5.0.tgz#cf17483202c8fa7a33dae86798d50a102705f182"
integrity sha512-YIAyLw4OiUoHHoUgXvibrBDdluzqnqVMGsJXyBcoOzlWHQIe5zhh8dgYezNNRXjXwy6t22YmPljjw7lr+eD9cw==
dependencies:
"@paypal/paypal-js" "^8.0.5"
"@paypal/paypal-js" "^8.1.0"
"@paypal/sdk-constants" "^1.0.122"
"@paypal/sdk-constants@^1.0.122":