Skip to content

Commit

Permalink
Undo latest 2 commits and push them into #1831. My bad for accidently…
Browse files Browse the repository at this point in the history
… pushing into develop.
  • Loading branch information
ricardo committed May 17, 2021
1 parent 9ebad68 commit 7decc3a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
24 changes: 5 additions & 19 deletions client/payment-request/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,24 @@ import { PAYMENT_METHOD_NAME_PAYMENT_REQUEST } from '../../checkout/constants';
import { PaymentRequestExpress } from './payment-request-express';
import { applePayImage } from './apple-pay-preview';
import { getConfig } from '../../utils/checkout';
import { getPaymentRequest } from '../utils';

const ApplePayPreview = () => <img src={ applePayImage } alt="" />;

const paymentRequestPaymentMethod = ( api ) => ( {
name: PAYMENT_METHOD_NAME_PAYMENT_REQUEST,
content: <PaymentRequestExpress api={ api } stripe={ api.loadStripe() } />,
edit: <ApplePayPreview />,
canMakePayment: ( cartData ) => {
canMakePayment: () => {
// If in the editor context, always return true to display the `edit` prop preview.
// https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/4101.
if ( getConfig( 'is_admin' ) ) {
return true;
}

if ( 'undefined' === typeof wcpayPaymentRequestParams ) {
return false;
}

return api.loadStripe().then( ( stripe ) => {
// Create a payment request and check if we can make a payment to determine whether to
// show the Payment Request Button or not. This is necessary because a browser might be
// able to load the Stripe JS object, but not support Payment Requests.
const pr = getPaymentRequest( {
stripe,
total: parseInt( cartData?.cartTotals?.total_price ?? 0, 10 ),
requestShipping: cartData?.cartNeedsShipping,
displayItems: [],
} );

return pr.canMakePayment();
} );
return (
!! api.getStripe() &&
'undefined' !== typeof wcpayPaymentRequestParams
);
},
paymentMethodId: PAYMENT_METHOD_NAME_PAYMENT_REQUEST,
supports: {
Expand Down
3 changes: 2 additions & 1 deletion client/payment-request/blocks/payment-request-express.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const PaymentRequestExpressComponent = ( {
paymentRequest,
// paymentRequestType,
// isProcessing,
canMakePayment,
onButtonClick,
} = useInitialization( {
api,
Expand All @@ -56,7 +57,7 @@ const PaymentRequestExpressComponent = ( {
},
};

if ( ! paymentRequest ) {
if ( ! canMakePayment || ! paymentRequest ) {
return null;
}

Expand Down
18 changes: 7 additions & 11 deletions client/payment-request/blocks/use-initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
getPaymentRequest,
updatePaymentRequest,
canDoPaymentRequest,
normalizeLineItems,
} from '../utils';

Expand All @@ -32,6 +33,7 @@ export const useInitialization = ( {
const [ paymentRequest, setPaymentRequest ] = useState( null );
const [ isFinished, setIsFinished ] = useState( false );
const [ isProcessing, setIsProcessing ] = useState( false );
const [ canMakePayment, setCanMakePayment ] = useState( false );
const [ paymentRequestType, setPaymentRequestType ] = useState( '' );

// Create the initial paymentRequest object. Note, we can't do anything if stripe isn't available yet or we have zero total.
Expand All @@ -53,17 +55,10 @@ export const useInitialization = ( {
displayItems: normalizeLineItems( billing?.cartTotalItems ),
} );

pr.canMakePayment().then( ( result ) => {
if ( result ) {
setPaymentRequest( pr );
if ( result.applePay ) {
setPaymentRequestType( 'apple_pay' );
} else if ( result.googlePay ) {
setPaymentRequestType( 'google_pay' );
} else {
setPaymentRequestType( 'payment_request_api' );
}
}
canDoPaymentRequest( pr ).then( ( result ) => {
setPaymentRequest( pr );
setPaymentRequestType( result.requestType || '' );
setCanMakePayment( result.canPay );
} );
}, [ stripe, paymentRequest ] );

Expand Down Expand Up @@ -136,6 +131,7 @@ export const useInitialization = ( {
return {
paymentRequest,
isProcessing,
canMakePayment,
onButtonClick,
paymentRequestType,
};
Expand Down
10 changes: 7 additions & 3 deletions client/payment-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
paymentMethodHandler,
} from './event-handlers.js';

import { shouldUseGooglePayBrand, getPaymentRequest } from './utils';
import {
shouldUseGooglePayBrand,
getPaymentRequest,
canDoPaymentRequest,
} from './utils';

jQuery( ( $ ) => {
// Don't load if blocks checkout is being loaded.
Expand Down Expand Up @@ -181,8 +185,8 @@ jQuery( ( $ ) => {
);

// Check the availability of the Payment Request API first.
paymentRequest.canMakePayment().then( ( result ) => {
if ( ! result ) {
canDoPaymentRequest( paymentRequest ).then( ( result ) => {
if ( ! result || ! result.canPay ) {
return;
}

Expand Down
29 changes: 29 additions & 0 deletions client/payment-request/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ export const updatePaymentRequest = ( {
} );
};

/**
* Returns whether or not the current session can make payments and what type of request it uses.
*
* @param {Object} paymentRequest A Stripe PaymentRequest instance.
*
* @return {Promise} Object containing canPay and the requestType, which can be either
* - payment_request_api
* - apple_pay
* - google_pay
*/
export const canDoPaymentRequest = ( paymentRequest ) => {
return new Promise( ( resolve ) => {
paymentRequest.canMakePayment().then( ( result ) => {
if ( result ) {
let paymentRequestType = 'payment_request_api';
if ( result.applePay ) {
paymentRequestType = 'apple_pay';
} else if ( result.googlePay ) {
paymentRequestType = 'google_pay';
}

resolve( { canPay: true, requestType: paymentRequestType } );
} else {
resolve( { canPay: false } );
}
} );
} );
};

/**
* Get WC AJAX endpoint URL.
*
Expand Down

0 comments on commit 7decc3a

Please sign in to comment.