Skip to content

Commit

Permalink
[ECE] Add Stripe Link Support on Block Cart and Block Checkout pages (#…
Browse files Browse the repository at this point in the history
…3540)

* Add Stripe Link to block pages

* Update comments

* Fix comment

* Fix comments

* Add Stripe link preview

* Add blank line to bottom of scss file

* Add changelog entries

* Add hover styling to give illusion of a button

* Remove extra line
  • Loading branch information
james-allan authored Oct 25, 2024
1 parent ba36d78 commit ba01a9b
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fix - Prevent marking orders on-hold with order note "Process order to take payment" when the payment has failed.
* Fix - Fix payment methods count on settings page.
* Tweak - Add error logging in ECE critical Ajax requests.
* Add - Add support for Stripe Link payments via the new Stripe Checkout Element on the block cart and block checkout pages.
* Add - Add support for Stripe Link payments via the new Stripe Checkout Element on the product, cart, checkout and pay for order pages.

= 8.8.0 - 2024-10-17 =
Expand Down
6 changes: 5 additions & 1 deletion client/blocks/express-checkout/express-checkout-container.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import React from 'react';
import { Elements } from '@stripe/react-stripe-js';
import ExpressCheckoutComponent from './express-checkout-component';
import { getPaymentMethodTypesForExpressMethod } from 'wcstripe/express-checkout/utils';

export const ExpressCheckoutContainer = ( props ) => {
const { stripe, billing } = props;
const { stripe, billing, expressPaymentMethod } = props;
const options = {
mode: 'payment',
paymentMethodCreation: 'manual',
amount: billing.cartTotal.value,
currency: billing.currency.code.toLowerCase(),
paymentMethodTypes: getPaymentMethodTypesForExpressMethod(
expressPaymentMethod
),
};

return (
Expand Down
33 changes: 32 additions & 1 deletion client/blocks/express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT } from './constants';
import { ExpressCheckoutContainer } from './express-checkout-container';
import ApplePayPreview from './apple-pay-preview';
import GooglePayPreview from './google-pay-preview';
import StripeLinkPreview from './stripe-link-preview';
import { loadStripe } from 'wcstripe/blocks/load-stripe';
import { getBlocksConfiguration } from 'wcstripe/blocks/utils';
import { checkPaymentMethodIsAvailable } from 'wcstripe/express-checkout/utils/check-payment-method-availability';
Expand Down Expand Up @@ -66,4 +67,34 @@ const expressCheckoutElementsApplePay = ( api ) => ( {
},
} );

export { expressCheckoutElementsGooglePay, expressCheckoutElementsApplePay };
const expressCheckoutElementsStripeLink = ( api ) => ( {
name: PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT + '_link',
content: (
<ExpressCheckoutContainer
api={ api }
stripe={ stripePromise }
expressPaymentMethod="link"
/>
),
edit: <StripeLinkPreview />,
canMakePayment: ( { cart } ) => {
// eslint-disable-next-line camelcase
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
return false;
}

return new Promise( ( resolve ) => {
checkPaymentMethodIsAvailable( 'link', api, cart, resolve );
} );
},
paymentMethodId: PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT,
supports: {
features: getBlocksConfiguration()?.supports ?? [],
},
} );

export {
expressCheckoutElementsGooglePay,
expressCheckoutElementsApplePay,
expressCheckoutElementsStripeLink,
};
9 changes: 9 additions & 0 deletions client/blocks/express-checkout/stripe-link-preview/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions client/blocks/express-checkout/stripe-link-preview/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable max-len */
import icon from './icon.svg';
import './style.scss';

const useStripeLinkPreview = () => (
<div className="stripe-link-preview">
<img src={ icon } alt="" />
</div>
);

export default useStripeLinkPreview;
12 changes: 12 additions & 0 deletions client/blocks/express-checkout/stripe-link-preview/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.stripe-link-preview {
display: flex;
justify-content: center;
align-items: center;
background-color: #00d66f;
border-radius: 5px;
height: 40px;
&:hover {
cursor: pointer;
filter: opacity(0.7);
}
}
2 changes: 2 additions & 0 deletions client/blocks/upe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import paymentRequestPaymentMethod from 'wcstripe/blocks/payment-request';
import {
expressCheckoutElementsGooglePay,
expressCheckoutElementsApplePay,
expressCheckoutElementsStripeLink,
} from 'wcstripe/blocks/express-checkout';
import WCStripeAPI from 'wcstripe/api';
import { getBlocksConfiguration } from 'wcstripe/blocks/utils';
Expand Down Expand Up @@ -97,6 +98,7 @@ if ( getBlocksConfiguration()?.isECEEnabled ) {
// Register Express Checkout Element.
registerExpressPaymentMethod( expressCheckoutElementsGooglePay( api ) );
registerExpressPaymentMethod( expressCheckoutElementsApplePay( api ) );
registerExpressPaymentMethod( expressCheckoutElementsStripeLink( api ) );
} else {
// Register Stripe Payment Request.
registerExpressPaymentMethod( paymentRequestPaymentMethod );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ReactDOM from 'react-dom';
import { ExpressCheckoutElement, Elements } from '@stripe/react-stripe-js';
import { memoize } from 'lodash';
import { getPaymentMethodTypesForExpressMethod } from 'wcstripe/express-checkout/utils';

export const checkPaymentMethodIsAvailable = memoize(
( paymentMethod, api, cart, resolve ) => {
Expand All @@ -22,6 +23,9 @@ export const checkPaymentMethodIsAvailable = memoize(
paymentMethodCreation: 'manual',
amount: Number( cart.cartTotals.total_price ),
currency: cart.cartTotals.currency_code.toLowerCase(),
paymentMethodTypes: getPaymentMethodTypesForExpressMethod(
paymentMethod
),
} }
>
<ExpressCheckoutElement
Expand All @@ -37,7 +41,7 @@ export const checkPaymentMethodIsAvailable = memoize(
paymentMethod === 'googlePay'
? 'always'
: 'never',
link: 'never',
link: paymentMethod === 'link' ? 'auto' : 'never',
paypal: 'never',
},
} }
Expand Down
35 changes: 34 additions & 1 deletion client/express-checkout/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global wc_stripe_express_checkout_params */

import { isLinkEnabled, getPaymentMethodTypes } from 'wcstripe/stripe-utils';
import { getBlocksConfiguration } from 'wcstripe/blocks/utils';

export * from './normalize';

Expand Down Expand Up @@ -273,3 +273,36 @@ export const getExpressPaymentMethodTypes = ( paymentMethodType = null ) => {

return expressPaymentMethodTypes;
};

/**
* Fetches the payment method types required to process a payment for an Express method.
*
* @see https://docs.stripe.com/elements/express-checkout-element/accept-a-payment#enable-payment-methods - lists the method types
* supported and which ones are required by each Express Checkout method.
*
* @param {*} paymentMethodType The express payment method type. eg 'link', 'googlePay', or 'applePay'.
* @return {Array} Array of payment method types necessary to process a payment for an Express method.
*/
export const getPaymentMethodTypesForExpressMethod = ( paymentMethodType ) => {
const paymentMethodsConfig = getBlocksConfiguration()?.paymentMethodsConfig;
const paymentMethodTypes = [];

if ( ! paymentMethodsConfig ) {
return paymentMethodTypes;
}

// All express payment methods require 'card' payments. Add it if it's enabled.
if ( paymentMethodsConfig?.card !== undefined ) {
paymentMethodTypes.push( 'card' );
}

// Add 'link' payment method type if enabled and requested.
if (
paymentMethodType === 'link' &&
isLinkEnabled( paymentMethodsConfig )
) {
paymentMethodTypes.push( 'link' );
}

return paymentMethodTypes;
};
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
* Fix - Prevent marking orders on-hold with order note "Process order to take payment" when the payment has failed.
* Fix - Fix payment methods count on settings page.
* Tweak - Add error logging in ECE critical Ajax requests.
* Add - Add support for Stripe Link payments via the new Stripe Checkout Element on the block cart and block checkout pages.
* Add - Add support for Stripe Link payments via the new Stripe Checkout Element on the product, cart, checkout and pay for order pages.

[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

0 comments on commit ba01a9b

Please sign in to comment.