From f98d001ea61386a5046a550d0fe00a5964bca8bd Mon Sep 17 00:00:00 2001 From: Michael Davidson Date: Thu, 18 Aug 2022 16:34:48 +1000 Subject: [PATCH 1/7] feat: add stripe payment element support --- .../src/api/addPaymentMethod/index.ts | 23 +++++ .../src/api/getPaymentIntent/index.ts | 28 +++++++ packages/api-client/src/index.server.ts | 4 + .../Checkout/PaymentMethod/Stripe.vue | 83 +++++++++++-------- packages/theme/pages/Checkout/Payment.vue | 16 ++-- 5 files changed, 115 insertions(+), 39 deletions(-) create mode 100644 packages/api-client/src/api/addPaymentMethod/index.ts create mode 100644 packages/api-client/src/api/getPaymentIntent/index.ts diff --git a/packages/api-client/src/api/addPaymentMethod/index.ts b/packages/api-client/src/api/addPaymentMethod/index.ts new file mode 100644 index 00000000..4f713cd8 --- /dev/null +++ b/packages/api-client/src/api/addPaymentMethod/index.ts @@ -0,0 +1,23 @@ +/* eslint-disable camelcase */ +import { ApiContext } from '../../types'; +import getCurrentBearerOrCartToken from '../authentication/getCurrentBearerOrCartToken'; + +export default async function addPaymentMethod({ client, config }: ApiContext, methodId: number): Promise { + const token = await getCurrentBearerOrCartToken({ client, config }); + const currency = await config.internationalization.getCurrency(); + + const result = await client.checkout.orderUpdate(token, { + order: { + payments_attributes: [ + { + payment_method_id: methodId.toString() + } + ] + }, + currency + }); + + if (result.isFail()) { + throw result.fail(); + } +} \ No newline at end of file diff --git a/packages/api-client/src/api/getPaymentIntent/index.ts b/packages/api-client/src/api/getPaymentIntent/index.ts new file mode 100644 index 00000000..402edb76 --- /dev/null +++ b/packages/api-client/src/api/getPaymentIntent/index.ts @@ -0,0 +1,28 @@ +import axios from 'axios'; +import { ApiContext } from '../../types'; +import getCurrentBearerOrCartToken from '../authentication/getCurrentBearerOrCartToken'; +import getAuthorizationHeaders from '../authentication/getAuthorizationHeaders'; +import { Logger } from '@vue-storefront/core'; + +export default async function getPaymentIntent({ client, config }: ApiContext, methodId) { + try { + const token = await getCurrentBearerOrCartToken({ client, config }); + const currency = await config.internationalization.getCurrency(); + const endpoint = config.backendUrl.concat('/api/v2/storefront/intents/create'); + const response = await axios.post( + endpoint, { + currency: currency, + payment_method_id: methodId + }, + { + headers: getAuthorizationHeaders(token) + } + ); + return { + clientSecret: response.data.client_secret + }; + } catch (e) { + Logger.error(e); + throw e; + } +} \ No newline at end of file diff --git a/packages/api-client/src/index.server.ts b/packages/api-client/src/index.server.ts index 9e80a188..c5d34707 100644 --- a/packages/api-client/src/index.server.ts +++ b/packages/api-client/src/index.server.ts @@ -1,6 +1,7 @@ import { ApiClientExtension, apiClientFactory } from '@vue-storefront/core'; import addAddress from './api/addAddress'; +import addPaymentMethod from './api/addPaymentMethod'; import addToCart from './api/addToCart'; import addToWishlist from './api/addToWishlist'; import applyCoupon from './api/applyCoupon'; @@ -24,6 +25,7 @@ import getOrCreateCart from './api/getOrCreateCart'; import getOrder from './api/getOrder'; import getOrders from './api/getOrders'; import getPaymentConfirmationData from './api/getPaymentConfirmationData'; +import getPaymentIntent from './api/getPaymentIntent'; import getPaymentMethods from './api/getPaymentMethods'; import getProduct from './api/getProduct'; import getProducts from './api/getProducts'; @@ -120,6 +122,8 @@ const { createApiClient } = apiClientFactory({ getPaymentMethods, savePaymentMethod, getPaymentConfirmationData, + addPaymentMethod, + getPaymentIntent, handlePaymentConfirmationResponse, makeOrder, forgotPassword, diff --git a/packages/theme/components/Checkout/PaymentMethod/Stripe.vue b/packages/theme/components/Checkout/PaymentMethod/Stripe.vue index 58f669ce..26e23d6d 100644 --- a/packages/theme/components/Checkout/PaymentMethod/Stripe.vue +++ b/packages/theme/components/Checkout/PaymentMethod/Stripe.vue @@ -1,11 +1,19 @@