import ApiClient from '.'; import { CaptureContextRequest, CaptureContextResponse } from './types/Payment'; import { AddPaymentMethodRequest, AddPaymentMethodResponse, GetPaymentMethod, GetPaymentMethodRequest, GetPaymentMethodResponse, PaymentMethod, } from './types/PaymentMethod'; import urls from './urls'; const mapGetPaymentMethodResponse = ({ response, ...otherValues }: GetPaymentMethodResponse): GetPaymentMethod => { const payment_methods = response?.payment_methods; return { response: { paymentMethods: payment_methods ? payment_methods?.map( ({ billing_city: billingCity, billing_country: billingCountry, billing_company: billingCompany, billing_email: billingEmail, billing_first_name: billingFirstName, billing_last_name: billingLastName, billing_phone: billingPhone, billing_street: billingStreet, billing_state: billingState, billing_postal_code: billingPostalCode, card_type: cardType, card_expiration_month: cardExpirationMonth, card_expiration_year: cardExpirationYear, card_last_four: cardLastFour, name_on_card: nameOnCard, payment_type: paymentType, is_active: isActive, billing_street_2: billingStreet2, ...otherValues }) => ({ billingCity, billingCountry, billingCompany, billingEmail, billingFirstName, billingLastName, billingPhone, billingState, billingStreet, billingPostalCode, cardType, cardExpirationMonth, cardExpirationYear, cardLastFour, isActive, nameOnCard, paymentType, billingStreet2, ...otherValues, }), ) : [], }, ...otherValues, }; }; export default class PaymentClient { captureContext(this: ApiClient): Promise { return this.requestProtected( { method: 'GET', url: urls.captureContext(), }, ); } async getPaymentMethods(this: ApiClient): Promise { const data = await this.requestProtected< GetPaymentMethodRequest, GetPaymentMethodResponse >({ method: 'GET', url: urls.paymentMethods(), }); return mapGetPaymentMethodResponse(data); } async addPaymentMethod( this: ApiClient, data: AddPaymentMethodRequest, ): Promise { return this.requestProtected< AddPaymentMethodRequest, AddPaymentMethodResponse >({ body: data, method: 'POST', url: urls.paymentMethods(), }); } }