Skip to content

Commit

Permalink
Merge pull request #890 from recurly/cash-app-billing-address
Browse files Browse the repository at this point in the history
Adds billing address override to CashApp implementation
  • Loading branch information
gilv93 authored Jul 30, 2024
2 parents e9c0f4b + 2d48067 commit c221c56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AlternativePaymentMethods extends Emitter {
async submit ({ billingAddress } = {}) {
this.validateBillingAddress(billingAddress);
if (this.gatewayStrategy.data?.paymentMethod?.type == 'cashapp') {
return await this.gatewayStrategy.submitWebComponent();
return await this.gatewayStrategy.submitWebComponent(billingAddress);
}
try {
const token = await this.tokenizePaymentMethod({ billingAddress });
Expand Down
30 changes: 16 additions & 14 deletions lib/recurly/alternative-payment-methods/gateways/adyen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class AdyenGateway extends Base {
this.state = {};
this.gatewayType = 'adyen';
this.webComponent = undefined;
this.customerBillingAddress = undefined;
}

scripts () {
Expand Down Expand Up @@ -59,17 +60,8 @@ class AdyenGateway extends Base {
this.state = state;
this.emit('change', state.isValid);
},
onSubmit: state => {
this.state = state;
this.tokenizePaymentMethod({}).then(token => {
this.emit('token', token);
}).catch(err => {
this.emit('error', err);
});
},
onError: err => {
this.emit('error', err);
},
onSubmit: state => this.onWebComponentSubmit(state),
onError: err => this.emit('error', err),
});

this.webComponent = checkout
Expand All @@ -91,11 +83,12 @@ class AdyenGateway extends Base {
return this.webComponent.handleAction(action);
}

async submitWebComponent () {
async submitWebComponent (billingAddress) {
this.customerBillingAddress = billingAddress;
return this.webComponent.submit();
}

async tokenizePaymentMethod ({ billingAddress }) {
async tokenizePaymentMethod () {
return this.recurly.request.post({
route: '/payment_methods/token',
data: {
Expand All @@ -107,10 +100,19 @@ class AdyenGateway extends Base {
paymentMethodData: this.data,
gatewayType: this.gatewayType,
returnURL: this.options.returnURL,
billingAddress,
billingAddress: this.customerBillingAddress,
},
});
}

onWebComponentSubmit (state) {
this.state = state;
this.tokenizePaymentMethod().then(token => {
this.emit('token', token);
}).catch(err => {
this.emit('error', err);
});
}
}

export default AdyenGateway;
14 changes: 11 additions & 3 deletions types/lib/alternative-payment-methods.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Address } from './address';
import { Emitter } from './emitter';

export type AlternativePaymentMethodEvents = 'token' | 'error' | 'valid';
Expand Down Expand Up @@ -31,7 +32,7 @@ export type AdyenAlternativePaymentMethodOptions = {
componentConfig?: { [key: string]: any }
};

export type AlternativePaymentMethodOptions = {
export type AlternativePaymentMethodStartOptions = {
/**
* List of payment methods to be presented to the customer.
*/
Expand Down Expand Up @@ -79,18 +80,25 @@ export type AlternativePaymentMethodOptions = {
adyen?: AdyenAlternativePaymentMethodOptions
};

export type AlternativePaymentMethodSubmitOptions = {
/**
* Sets the customer billing address on the generated token.
*/
billingAddress?: Address;
};

export interface AlternativePaymentMethodsInstance extends Emitter<AlternativePaymentMethodEvents> {
/**
* Start the PaymentMethods and render the components.
*/
start: (data: AlternativePaymentMethodOptions) => Promise<void>;
start: (data: AlternativePaymentMethodStartOptions) => Promise<void>;

/**
* Submit the customer payment information and produce a token.
* Call this function only when the payment information provided by the customer is valid, by listening the 'valid' event.
* The token can be retrieved through the 'token' event.
*/
submit: () => Promise<void>;
submit: (args: AlternativePaymentMethodSubmitOptions) => Promise<void>;

/**
* Some payment methods require additional action from the shopper such as: to scan a QR code,
Expand Down

0 comments on commit c221c56

Please sign in to comment.