Skip to content

Commit 3432fcc

Browse files
authored
Add StripeLink to WooCommerce Checkout block (#2386)
1 parent 05c0015 commit 3432fcc

File tree

9 files changed

+220
-7
lines changed

9 files changed

+220
-7
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Add - Stripe Link: Add beta headers for Stripe server requests
55
* Add - Stripe Link payment method option in admin
66
* Add - Stripe Link payment method on checkout form
7+
* Add - Stripe Link payment method on blocks checkout form
78

89
= 6.4.3 - 2022-06-30 =
910
* Fix - Replace unnecessary throws with empty string when keys are invalid.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const PAYMENT_METHOD_NAME = 'stripe';
2+
export const WC_STORE_CART = 'wc/store/cart';

client/blocks/upe/fields.js

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,52 @@
11
import { useState, useEffect } from '@wordpress/element';
2+
import { useDispatch, useSelect } from '@wordpress/data';
23
import { __ } from '@wordpress/i18n';
34
import {
45
Elements,
56
ElementsConsumer,
67
PaymentElement,
78
} from '@stripe/react-stripe-js';
9+
import { getAppearance } from '../../styles/upe';
810
import { confirmUpePayment } from './confirm-upe-payment';
911
import { getBlocksConfiguration } from 'wcstripe/blocks/utils';
10-
import { PAYMENT_METHOD_NAME } from 'wcstripe/blocks/credit-card/constants';
12+
import {
13+
PAYMENT_METHOD_NAME,
14+
WC_STORE_CART,
15+
} from 'wcstripe/blocks/credit-card/constants';
16+
import enableStripeLinkPaymentMethod from 'wcstripe/stripe-link';
17+
import './styles.scss';
18+
19+
const useCustomerData = () => {
20+
const { customerData, isInitialized } = useSelect( ( select ) => {
21+
const store = select( WC_STORE_CART );
22+
return {
23+
customerData: store.getCustomerData(),
24+
isInitialized: store.hasFinishedResolution( 'getCartData' ),
25+
};
26+
} );
27+
const {
28+
setShippingAddress,
29+
setBillingAddress,
30+
setBillingData,
31+
} = useDispatch( WC_STORE_CART );
32+
33+
let customerBillingAddress = customerData.billingData;
34+
let setCustomerBillingAddress = setBillingData;
35+
36+
//added for backwards compatibility -> billingData was renamed to billingAddress
37+
if ( customerData.billingData === undefined ) {
38+
customerBillingAddress = customerData.billingAddress;
39+
setCustomerBillingAddress = setBillingAddress;
40+
}
41+
42+
return {
43+
isInitialized,
44+
billingAddress: customerBillingAddress,
45+
shippingAddress: customerData.shippingAddress,
46+
setBillingAddress: setCustomerBillingAddress,
47+
setShippingAddress,
48+
};
49+
};
1150

1251
const UPEField = ( {
1352
api,
@@ -63,6 +102,131 @@ const UPEField = ( {
63102
createIntent();
64103
}, [ paymentIntentId, hasRequestedIntent, api, errorMessage ] );
65104

105+
const customerData = useCustomerData();
106+
107+
useEffect( () => {
108+
if (
109+
paymentMethodsConfig.link !== undefined &&
110+
paymentMethodsConfig.card !== undefined
111+
) {
112+
const shippingAddressFields = {
113+
line1: 'shipping-address_1',
114+
line2: 'shipping-address_2',
115+
city: 'shipping-city',
116+
state: 'components-form-token-input-1',
117+
postal_code: 'shipping-postcode',
118+
country: 'components-form-token-input-0',
119+
};
120+
const billingAddressFields = {
121+
line1: 'billing-address_1',
122+
line2: 'billing-address_2',
123+
city: 'billing-city',
124+
state: 'components-form-token-input-3',
125+
postal_code: 'billing-postcode',
126+
country: 'components-form-token-input-2',
127+
};
128+
129+
const appearance = getAppearance();
130+
elements.update( { appearance } );
131+
132+
enableStripeLinkPaymentMethod( {
133+
api,
134+
elements,
135+
emailId: 'email',
136+
fill_field_method: ( address, nodeId, key ) => {
137+
const setAddress =
138+
shippingAddressFields[ key ] === nodeId
139+
? customerData.setShippingAddress
140+
: customerData.setBillingAddress;
141+
const customerAddress =
142+
shippingAddressFields[ key ] === nodeId
143+
? customerData.shippingAddress
144+
: customerData.billingAddress;
145+
146+
if ( undefined === customerAddress ) {
147+
return;
148+
}
149+
150+
if ( address.address[ key ] === null ) {
151+
address.address[ key ] = '';
152+
}
153+
154+
if ( key === 'line1' ) {
155+
customerAddress.address_1 = address.address[ key ];
156+
} else if ( key === 'line2' ) {
157+
customerAddress.address_2 = address.address[ key ];
158+
} else if ( key === 'postal_code' ) {
159+
customerAddress.postcode = address.address[ key ];
160+
} else {
161+
customerAddress[ key ] = address.address[ key ];
162+
}
163+
164+
if ( undefined !== customerData.billingAddress ) {
165+
customerAddress.email = getEmail();
166+
}
167+
168+
setAddress( customerAddress );
169+
170+
function getEmail() {
171+
return document.getElementById( 'email' ).value;
172+
}
173+
174+
customerData.billingAddress.email = getEmail();
175+
customerData.setBillingAddress(
176+
customerData.billingAddress
177+
);
178+
},
179+
show_button: ( linkAutofill ) => {
180+
jQuery( '#email' )
181+
.parent()
182+
.append(
183+
'<button class="stripe-gateway-stripelink-modal-trigger"></button>'
184+
);
185+
if ( jQuery( '#email' ).val() !== '' ) {
186+
jQuery(
187+
'.stripe-gateway-stripelink-modal-trigger'
188+
).show();
189+
190+
const linkButtonTop =
191+
jQuery( '#email' ).position().top +
192+
( jQuery( '#email' ).outerHeight() - 40 ) / 2;
193+
jQuery(
194+
'.stripe-gateway-stripelink-modal-trigger'
195+
).show();
196+
jQuery(
197+
'.stripe-gateway-stripelink-modal-trigger'
198+
).css( 'top', linkButtonTop + 'px' );
199+
}
200+
201+
//Handle StripeLink button click.
202+
jQuery( '.stripe-gateway-stripelink-modal-trigger' ).on(
203+
'click',
204+
( event ) => {
205+
event.preventDefault();
206+
// Trigger modal.
207+
linkAutofill.launch( {
208+
email: jQuery( '#email' ).val(),
209+
} );
210+
}
211+
);
212+
},
213+
complete_shipping: () => {
214+
return (
215+
document.getElementById( 'shipping-address_1' ) !== null
216+
);
217+
},
218+
shipping_fields: shippingAddressFields,
219+
billing_fields: billingAddressFields,
220+
complete_billing: () => {
221+
return (
222+
document.getElementById( 'billing-address_1' ) !== null
223+
);
224+
},
225+
} );
226+
}
227+
// eslint-disable-next-line react-hooks/exhaustive-deps
228+
}, [ elements ] );
229+
66230
useEffect(
67231
() =>
68232
onPaymentProcessing( () => {

client/blocks/upe/styles.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
button.stripe-gateway-stripelink-modal-trigger {
2+
display: none;
3+
position: absolute;
4+
right: 5px;
5+
width: 64px;
6+
height: 40px;
7+
background: no-repeat
8+
url( '../../payment-method-icons/link/icon.svg' );
9+
background-color: transparent !important;
10+
cursor: pointer;
11+
border: none;
12+
}
13+
14+
button.stripe-gateway-stripelink-modal-trigger:hover {
15+
background-color: transparent;
16+
border-color: transparent;
17+
}

client/classic/upe/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,18 @@ jQuery( function ( $ ) {
329329
api,
330330
elements,
331331
emailId: 'billing_email',
332-
complete_billing: true,
332+
complete_billing: () => {
333+
return true;
334+
},
333335
complete_shipping: () => {
334-
return ! document.getElementById(
335-
'ship-to-different-address-checkbox'
336-
).checked;
336+
return (
337+
document.getElementById(
338+
'ship-to-different-address-checkbox'
339+
) &&
340+
document.getElementById(
341+
'ship-to-different-address-checkbox'
342+
).checked
343+
);
337344
},
338345
shipping_fields: {
339346
line1: 'shipping_address_1',

client/stripe-link/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const enableStripeLinkPaymentMethod = ( options ) => {
5151
}
5252
};
5353

54-
if ( options.complete_shipping ) {
54+
if ( options.complete_shipping() ) {
5555
fillWith( shippingAddress, options.shipping_fields.line1, 'line1' );
5656
fillWith( shippingAddress, options.shipping_fields.line2, 'line2' );
5757
fillWith( shippingAddress, options.shipping_fields.city, 'city' );
@@ -67,7 +67,8 @@ const enableStripeLinkPaymentMethod = ( options ) => {
6767
'country'
6868
);
6969
}
70-
if ( options.complete_billing ) {
70+
71+
if ( options.complete_billing() ) {
7172
fillWith( billingAddress, options.billing_fields.line1, 'line1' );
7273
fillWith( billingAddress, options.billing_fields.line2, 'line2' );
7374
fillWith( billingAddress, options.billing_fields.city, 'city' );
@@ -83,6 +84,7 @@ const enableStripeLinkPaymentMethod = ( options ) => {
8384
'country'
8485
);
8586
}
87+
jQuery( 'select' ).trigger( 'change' );
8688
} );
8789
};
8890

includes/class-wc-stripe-blocks-support.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ private function register_upe_payment_method_script_handles() {
9595
: $dependencies;
9696
}
9797

98+
wp_enqueue_style(
99+
'wc-stripe-blocks-checkout-style',
100+
WC_STRIPE_PLUGIN_URL . '/build/upe_blocks.css',
101+
[],
102+
$version
103+
);
104+
98105
wp_register_script(
99106
'wc-stripe-blocks-integration',
100107
WC_STRIPE_PLUGIN_URL . '/build/upe_blocks.js',

includes/class-wc-stripe-intent-controller.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,19 @@ public function update_payment_intent( $payment_intent_id = '', $order_id = null
414414
if ( '' !== $selected_upe_payment_type ) {
415415
// Only update the payment_method_types if we have a reference to the payment type the customer selected.
416416
$request['payment_method_types'] = [ $selected_upe_payment_type ];
417+
if (
418+
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID === $selected_upe_payment_type &&
419+
in_array(
420+
WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID,
421+
$gateway->get_upe_enabled_payment_method_ids(),
422+
true
423+
)
424+
) {
425+
$request['payment_method_types'] = [
426+
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID,
427+
WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID,
428+
];
429+
}
417430
$order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type );
418431
}
419432
if ( ! empty( $customer ) && $customer->get_id() ) {

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
132132
* Add - Stripe Link payment method option in admin
133133
* Add - Stripe Link: Add beta headers for Stripe server requests
134134
* Add - Stripe Link payment method on checkout form
135+
* Add - Stripe Link payment method on blocks checkout form
135136

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

0 commit comments

Comments
 (0)