import _ from 'lodash' ; import Stripe from 'stripe' ; import { PAYMENT_PROVIDER } from 'common/enums' ; import { AtomService , PaymentService } from 'connectors' ; import SlackService from 'connectors/slack' ; import { Customer } from 'definitions' ; export const updateCustomerCard = async ( { setupIntent , event , } ) => { const atomService = new AtomService ( ) ; const paymentService = new PaymentService ( ) ; const slack = new SlackService ( ) ; const slackEventData = { id : event . id , type : event . type , } ; const customerId = setupIntent . customer as string ; const paymentMethodId = setupIntent . payment_method as string ; if ( ! customerId || ! paymentMethodId ) { slack . sendStripeAlert ( { data : slackEventData , message : `customer (${ customerId }) or paymentMethod (${ paymentMethodId }) doesn't exist.` , } ) ; return ; } const customer = ( await atomService . findFirst ( { table : 'customer' , where : { customerId , provider : PAYMENT_PROVIDER . stripe , archived : false , } , } ) ) as Customer ; if ( ! customer ) { slack . sendStripeAlert ( { data : slackEventData , message : `can't find customer ${ customerId }.` , } ) ; return ; } const paymentMethod = await paymentService . stripe . getPaymentMethod ( paymentMethodId ) ; const cardLast4 = _ . get ( paymentMethod , 'card.last4' ) ; if ( ! cardLast4 ) { slack . sendStripeAlert ( { data : slackEventData , message : `cardLast4 doesn't exist.` , } ) ; return ; } const updatedCustomer = ( await atomService . update ( { table : 'customer' , where : { id : customer . id } , data : { card_last_4 : cardLast4 , } , } ) ) as Customer ; await paymentService . stripe . updateCustomer ( { id : customerId , paymentMethod : paymentMethodId , } ) ; return updatedCustomer ; } ;