-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INTER-2976] Added gateway public ID and type to fastlane instance
- Loading branch information
Showing
5 changed files
with
340 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import {IFastlaneInstance} from 'src/types'; | ||
|
||
// Do not remove. This is here to enforce adherence of this class to the interface | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const _: IFastlaneInstance = {} as Fastlane; | ||
type OmittedIFastlaneInstance = Omit<IFastlaneInstance, 'gatewayPublicId' | 'type'>; | ||
type IFastlaneAddress = Awaited<ReturnType<IFastlaneInstance['identity']['triggerAuthenticationFlow']>>['profileData']['shippingAddress']; | ||
|
||
interface IPayPalAddress { | ||
companyName?: string; | ||
name: { | ||
firstName: 'string'; | ||
lastName: 'string'; | ||
fullName: 'string'; | ||
}; | ||
address: { | ||
addressLine1: 'string'; | ||
addressLine2?: 'string'; | ||
adminArea2: 'string'; | ||
adminArea1: 'string'; | ||
postalCode: 'string', | ||
countryCode: 'string'; | ||
}; | ||
phoneNumber: { | ||
nationalNumber: 'string'; | ||
countryCode: 'string'; | ||
}; | ||
} | ||
|
||
export default class Fastlane { | ||
public profile: IFastlaneInstance['profile']; | ||
public identity: IFastlaneInstance['identity']; | ||
public FastlaneCardComponent: IFastlaneInstance['FastlaneCardComponent']; | ||
public FastlanePaymentComponent: IFastlaneInstance['FastlanePaymentComponent']; | ||
setLocale: IFastlaneInstance['setLocale']; | ||
|
||
constructor( | ||
private instance: OmittedIFastlaneInstance, | ||
public type: IFastlaneInstance['type'], | ||
public gatewayPublicId: string | ||
) { | ||
this.setLocale = instance.setLocale.bind(instance); | ||
this.identity = { | ||
triggerAuthenticationFlow: this.triggerAuthenticationFlow, | ||
lookupCustomerByEmail: instance.identity.lookupCustomerByEmail.bind(instance.identity), | ||
}; | ||
this.profile = { | ||
showShippingAddressSelector: this.showShippingAddressSelector, | ||
showCardSelector: instance.profile.showCardSelector.bind(instance.profile.showCardSelector), | ||
}; | ||
this.FastlaneCardComponent = instance.FastlaneCardComponent.bind(instance); | ||
this.FastlanePaymentComponent = instance.FastlanePaymentComponent.bind(instance); | ||
} | ||
|
||
private showShippingAddressSelector = async () => { | ||
const resp = await this.instance.profile.showShippingAddressSelector(); | ||
|
||
// No restructuring needed for PPCP unless the selection has not changed | ||
if (this.type === 'braintree' || !resp.selectionChanged) { | ||
return resp; | ||
} | ||
|
||
const paypalAddress = resp.selectedAddress as unknown as IPayPalAddress; | ||
return { | ||
selectionChanged: resp.selectionChanged, | ||
selectedAddress: this.transformPaypalAddress(paypalAddress), | ||
}; | ||
} | ||
|
||
private triggerAuthenticationFlow = async (customerContextId: string) => { | ||
const resp = await this.instance.identity.triggerAuthenticationFlow(customerContextId); | ||
|
||
if (this.type === 'braintree' || resp.authenticationState !== 'succeeded') { | ||
return resp; | ||
} | ||
|
||
const paypalAddress = resp.profileData.shippingAddress as unknown as IPayPalAddress; | ||
return { | ||
authenticationState: resp.authenticationState, | ||
profileData: { | ||
card: resp.profileData.card, | ||
name: resp.profileData.name, | ||
shippingAddress: this.transformPaypalAddress(paypalAddress), | ||
}, | ||
}; | ||
} | ||
|
||
private transformPaypalAddress = (paypalAddress: IPayPalAddress) => { | ||
return { | ||
firstName: paypalAddress.name.firstName, | ||
lastName: paypalAddress.name.lastName, | ||
locality: paypalAddress.address.adminArea2, | ||
region: paypalAddress.address.adminArea1, | ||
countryCodeAlpha2: paypalAddress.address.countryCode, | ||
phoneNumber: `${paypalAddress.phoneNumber.countryCode}${paypalAddress.phoneNumber.nationalNumber}`, | ||
postalCode: paypalAddress.address.postalCode, | ||
streetAddress: paypalAddress.address.addressLine1, | ||
extendedAddress: paypalAddress.address.addressLine2, | ||
company: paypalAddress.companyName, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import Fastlane from 'src/fastlane/fastlane'; | ||
|
||
describe('testing Fastlane class', () => { | ||
const fastlaneInstanceMock = { | ||
setLocale: jest.fn(), | ||
FastlaneCardComponent: jest.fn(), | ||
FastlanePaymentComponent: jest.fn(), | ||
identity: { | ||
lookupCustomerByEmail: jest.fn(), | ||
triggerAuthenticationFlow: jest.fn(), | ||
}, | ||
profile: { | ||
showCardSelector: jest.fn(), | ||
showShippingAddressSelector: jest.fn(), | ||
}, | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test.each([ | ||
{ | ||
type: 'ppcp', | ||
respAddress: { | ||
companyName: 'company', | ||
name: { | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
fullName: 'fullName', | ||
}, | ||
address: { | ||
addressLine1: 'addressLine1', | ||
addressLine2: 'addressLine2', | ||
adminArea2: 'adminArea2', | ||
adminArea1: 'adminArea1', | ||
postalCode: 'postalCode', | ||
countryCode: 'countryCode', | ||
}, | ||
phoneNumber: { | ||
countryCode: '1', | ||
nationalNumber: '2041234567', | ||
}, | ||
}, | ||
expected: () => ({ | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
locality: 'adminArea2', | ||
region: 'adminArea1', | ||
countryCodeAlpha2: 'countryCode', | ||
phoneNumber: '12041234567', | ||
postalCode: 'postalCode', | ||
streetAddress: 'addressLine1', | ||
extendedAddress: 'addressLine2', | ||
company: 'company', | ||
}), | ||
} as const, | ||
{ | ||
type: 'braintree', | ||
respAddress: { | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
locality: 'adminArea2', | ||
region: 'adminArea1', | ||
countryCodeAlpha2: 'countryCode', | ||
phoneNumber: '12041234567', | ||
postalCode: 'postalCode', | ||
streetAddress: 'addressLine1', | ||
extendedAddress: 'addressLine2', | ||
company: 'company', | ||
}, | ||
expected() { | ||
return this.respAddress; | ||
}, | ||
} as const, | ||
])('$type showShippingAddressSelector', async (input) => { | ||
// Arranging | ||
fastlaneInstanceMock.profile.showShippingAddressSelector.mockResolvedValue({ | ||
selectionChanged: true, | ||
selectedAddress: input.respAddress, | ||
}); | ||
|
||
// Acting | ||
const fastlane = new Fastlane(fastlaneInstanceMock, input.type, ''); | ||
const actualResp = await fastlane.profile.showShippingAddressSelector(); | ||
|
||
// Asserting | ||
expect(actualResp.selectedAddress).toStrictEqual(input.expected()); | ||
}); | ||
|
||
test.each([ | ||
{ | ||
type: 'ppcp', | ||
respProfileData: { | ||
card: {}, | ||
name: { | ||
firstName: 'firstName', | ||
lastName: 'firstName', | ||
}, | ||
shippingAddress: { | ||
companyName: 'company', | ||
name: { | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
fullName: 'fullName', | ||
}, | ||
address: { | ||
addressLine1: 'addressLine1', | ||
addressLine2: 'addressLine2', | ||
adminArea2: 'adminArea2', | ||
adminArea1: 'adminArea1', | ||
postalCode: 'postalCode', | ||
countryCode: 'countryCode', | ||
}, | ||
phoneNumber: { | ||
countryCode: '1', | ||
nationalNumber: '2041234567', | ||
}, | ||
} | ||
}, | ||
expected: () => ({ | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
locality: 'adminArea2', | ||
region: 'adminArea1', | ||
countryCodeAlpha2: 'countryCode', | ||
phoneNumber: '12041234567', | ||
postalCode: 'postalCode', | ||
streetAddress: 'addressLine1', | ||
extendedAddress: 'addressLine2', | ||
company: 'company', | ||
}), | ||
} as const, | ||
{ | ||
type: 'braintree', | ||
respProfileData: { | ||
card: {}, | ||
name: { | ||
firstName: 'firstName', | ||
lastName: 'firstName', | ||
}, | ||
shippingAddress: { | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
locality: 'adminArea2', | ||
region: 'adminArea1', | ||
countryCodeAlpha2: 'countryCode', | ||
phoneNumber: '12041234567', | ||
postalCode: 'postalCode', | ||
streetAddress: 'addressLine1', | ||
extendedAddress: 'addressLine2', | ||
company: 'company', | ||
}, | ||
}, | ||
expected() { | ||
return this.respProfileData.shippingAddress; | ||
}, | ||
} as const, | ||
])('$type triggerAuthenticationFlow', async (input) => { | ||
// Arranging | ||
fastlaneInstanceMock.identity.triggerAuthenticationFlow.mockResolvedValue({ | ||
authenticationState: 'succeeded', | ||
profileData: input.respProfileData, | ||
}); | ||
|
||
// Acting | ||
const fastlane = new Fastlane(fastlaneInstanceMock, input.type, ''); | ||
const actualResp = await fastlane.identity.triggerAuthenticationFlow('1'); | ||
|
||
// Asserting | ||
expect(actualResp.profileData.shippingAddress).toStrictEqual(input.expected()); | ||
}); | ||
}); |
Oops, something went wrong.