Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe paymentelement migration #345

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
import { PaymentElement, useStripe, useElements } from '@stripe/react-stripe-js';
import React, { useState, useEffect } from 'react';
import { CardElement, useStripe, useElements } from '@stripe/react-stripe-js';
import { useQuery } from 'urql';
import { useCheckout } from '@components/common/context/checkout';
import './CheckoutForm.scss';
Expand Down Expand Up @@ -76,7 +76,7 @@ export default function CheckoutForm({ stripePublishableKey }) {
const [clientSecret, setClientSecret] = useState('');
const [showTestCard, setShowTestCard] = useState('success');
const stripe = useStripe();
const elements = useElements();
const elms = useElements();
const { cartId, orderId, orderPlaced, paymentMethods, checkoutSuccessUrl } =
useCheckout();

Expand All @@ -88,9 +88,14 @@ export default function CheckoutForm({ stripePublishableKey }) {
pause: orderPlaced === true
});

useEffect(() => {
useEffect(async () => {
// Create PaymentIntent as soon as the order is placed
if (orderId) {
const {error: submitError} = await elms.submit();
if (submitError) {
return;
}

window
.fetch('/api/stripe/paymentIntents', {
method: 'POST',
Expand All @@ -110,9 +115,10 @@ export default function CheckoutForm({ stripePublishableKey }) {
const pay = async () => {
const billingAddress =
result.data.cart.billingAddress || result.data.cart.shippingAddress;
const payload = await stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: elements.getElement(CardElement),
const {error} = await stripe.confirmPayment({
elements: elms,
clientSecret,
payment_method_data: {
billing_details: {
name: billingAddress.fullName,
email: result.data.cart.customerEmail,
Expand All @@ -125,16 +131,14 @@ export default function CheckoutForm({ stripePublishableKey }) {
city: billingAddress.city
}
}
}
},
confirmParams: {
return_url: `${checkoutSuccessUrl}/${orderId}`,
},
});

if (payload.error) {
if (error) {
setError(`Payment failed ${payload.error.message}`);
} else {
setError(null);
setSucceeded(true);
// Redirect to checkout success page
window.location.href = `${checkoutSuccessUrl}/${orderId}`;
}
};

Expand All @@ -144,7 +148,7 @@ export default function CheckoutForm({ stripePublishableKey }) {
}, [orderPlaced, clientSecret, result]);

const handleChange = (event) => {
// Listen for changes in the CardElement
// Listen for changes in the PaymentElement
// and display any errors as the customer types their card details
setDisabled(event.empty);
if (event.complete === true && !event.error) {
Expand Down Expand Up @@ -185,7 +189,7 @@ export default function CheckoutForm({ stripePublishableKey }) {
testFailure={testFailure}
/>
)}
<CardElement
<PaymentElement
id="card-element"
options={cardStyle}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ module.exports = async (request, response, delegate, next) => {
metadata: {
// eslint-disable-next-line camelcase
orderId: order_id
},
automatic_payment_methods: {
enabled: true,
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ const stripeLoader = (publishKey) => {
};

function StripeApp({ stripePublishableKey }) {
const options = {
mode: 'payment',
currency: 'eur',
amount: 1337,
fields: {
billingDetails: 'never',
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ultimate-tester ,

Whoops. We can not hardcoded the amount and currency like this. It must be from the cart

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to help me with this? My experience with React is limited so I don't have a clue on how to add these fields into context :(

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will take care of it. Please give me sometime

};

return (
// eslint-disable-next-line react/jsx-filename-extension
<div className="App">
<Elements stripe={stripeLoader(stripePublishableKey)}>
<Elements stripe={stripeLoader(stripePublishableKey)} options={options}>
<CheckoutForm stripePublishableKey={stripePublishableKey} />
</Elements>
</div>
Expand Down
Loading