From 0be11c1e5285c9406fb8fdc594ac9cd7c60f5292 Mon Sep 17 00:00:00 2001 From: Will Franklin Date: Wed, 3 Apr 2024 17:55:42 +0100 Subject: [PATCH] Get billing address from Stripe --- src/api/controllers/SignupController.ts | 7 +------ src/api/dto/SignupFlowDto.ts | 8 +------ src/core/providers/payment/StripeProvider.ts | 21 ++++++++++++------- ...2163090314-RemoveJoinFormBillingAddress.ts | 19 +++++++++++++++++ src/models/JoinForm.ts | 3 --- 5 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 src/migrations/1712163090314-RemoveJoinFormBillingAddress.ts diff --git a/src/api/controllers/SignupController.ts b/src/api/controllers/SignupController.ts index c3f56f32d..010a73c73 100644 --- a/src/api/controllers/SignupController.ts +++ b/src/api/controllers/SignupController.ts @@ -69,12 +69,7 @@ export class SignupController { } // Merge additional data into the join form - if ( - data.firstname || - data.lastname || - data.billingAddress || - data.vatNumber - ) { + if (data.firstname || data.lastname || data.vatNumber) { Object.assign(joinFlow.joinForm, data); await getRepository(JoinFlow).save(joinFlow); } diff --git a/src/api/dto/SignupFlowDto.ts b/src/api/dto/SignupFlowDto.ts index 86ebda5c0..e4d5b51cf 100644 --- a/src/api/dto/SignupFlowDto.ts +++ b/src/api/dto/SignupFlowDto.ts @@ -42,8 +42,7 @@ export class StartSignupFlowDto implements CompleteUrls { export class CompleteSignupFlowDto extends CompleteJoinFlowDto - implements - Pick + implements Pick { @IsOptional() @IsString() @@ -53,11 +52,6 @@ export class CompleteSignupFlowDto @IsString() lastname?: string; - @IsOptional() - @ValidateNested() - @Type(() => UpdateAddressDto) - billingAddress?: UpdateAddressDto; - @IsOptional() @IsString() vatNumber?: string; diff --git a/src/core/providers/payment/StripeProvider.ts b/src/core/providers/payment/StripeProvider.ts index 170438c80..c933048cf 100644 --- a/src/core/providers/payment/StripeProvider.ts +++ b/src/core/providers/payment/StripeProvider.ts @@ -81,18 +81,23 @@ export default class StripeProvider extends PaymentProvider { } async updatePaymentMethod(flow: CompletedPaymentFlow): Promise { + const paymentMethod = await stripe.paymentMethods.retrieve(flow.mandateId); + const address = paymentMethod.billing_details.address; + const customerData: Stripe.CustomerUpdateParams = { invoice_settings: { default_payment_method: flow.mandateId }, - ...(flow.joinForm.billingAddress && { - address: { - line1: flow.joinForm.billingAddress.line1, - line2: flow.joinForm.billingAddress.line2 || "", - city: flow.joinForm.billingAddress.city, - postal_code: flow.joinForm.billingAddress.postcode - } - }) + address: address + ? { + line1: address.line1 || "", + ...(address.city && { city: address.city }), + ...(address.country && { country: address.country }), + ...(address.line2 && { line2: address.line2 }), + ...(address.postal_code && { postal_code: address.postal_code }), + ...(address.state && { state: address.state }) + } + : null }; if (this.data.customerId) { diff --git a/src/migrations/1712163090314-RemoveJoinFormBillingAddress.ts b/src/migrations/1712163090314-RemoveJoinFormBillingAddress.ts new file mode 100644 index 000000000..0039f2eee --- /dev/null +++ b/src/migrations/1712163090314-RemoveJoinFormBillingAddress.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class RemoveJoinFormBillingAddress1712163090314 + implements MigrationInterface +{ + name = "RemoveJoinFormBillingAddress1712163090314"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "join_flow" DROP COLUMN "joinFormBillingaddress"` + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "join_flow" ADD "joinFormBillingaddress" jsonb` + ); + } +} diff --git a/src/models/JoinForm.ts b/src/models/JoinForm.ts index b70d240bd..fabdae268 100644 --- a/src/models/JoinForm.ts +++ b/src/models/JoinForm.ts @@ -39,9 +39,6 @@ export default class JoinForm implements PaymentForm, ReferralGiftForm { @Column({ type: String, nullable: true }) lastname?: string | null; - @Column({ type: "jsonb", nullable: true }) - billingAddress?: Address | null; - @Column({ type: String, nullable: true }) vatNumber?: string | null;