-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(payment): PAYPAL-4937 Update PaymentRequestSender with new GQL API #2754
base: master
Are you sure you want to change the base?
Changes from 3 commits
74aa351
508e965
7afaa0f
01ba966
a14e11b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { HeadlessPaymentMethod } from './headless-payment'; | ||
|
||
export const initializationData = { | ||
merchantId: '100000', | ||
paymentButtonStyles: { | ||
checkoutTopButtonStyles: { color: 'blue', label: 'checkout', height: '36' }, | ||
}, | ||
}; | ||
|
||
export const encodedInitializationData = btoa(JSON.stringify(initializationData)); | ||
|
||
export function getHeadlessPaymentMethod(): HeadlessPaymentMethod { | ||
return { | ||
paymentWalletWithInitializationData: { | ||
clientToken: 'clientToken', | ||
initializationData: encodedInitializationData, | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { HeadlessPaymentMethodType } from './headless-payment-method-type'; | ||
|
||
const HeadlessPaymentMethodConfig: Record<string, HeadlessPaymentMethodType> = { | ||
paypalcommerce: HeadlessPaymentMethodType.PAYPALCOMMERCE, | ||
paypalcommercecredit: HeadlessPaymentMethodType.PAYPALCOMMERCECREDIT, | ||
}; | ||
|
||
export default HeadlessPaymentMethodConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import HeadlessPaymentMethod from './headless-payment-method'; | ||
|
||
export interface HeadlessPaymentMethodResponse { | ||
data: { | ||
site: HeadlessPaymentMethod; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum HeadlessPaymentMethodType { | ||
PAYPALCOMMERCE = 'paypalcommerce.paypal', | ||
PAYPALCOMMERCECREDIT = 'paypalcommerce.paypalcredit', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default interface HeadlessPaymentMethod { | ||
paymentWalletWithInitializationData: { | ||
clientToken?: string; | ||
// INFO:: initializationData given in base64 format | ||
initializationData?: string; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { RequestOptions } from '../../common/http-request'; | ||
|
||
export default interface HeadlessPaymentRequestOptions extends RequestOptions { | ||
body?: { query: string }; | ||
headers: { Authorization: string; [key: string]: string }; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { default as HeadlessPaymentMethod } from './headless-payment-method'; | ||
export { default as HeadlessPaymentMethodConfig } from './headless-payment-method-config'; | ||
export { default as HeadlessPaymentRequestOptions } from './headless-payment-request-options'; | ||
|
||
export { HeadlessPaymentMethodType } from './headless-payment-method-type'; | ||
export { HeadlessPaymentMethodResponse } from './headless-payment-method-response'; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,12 @@ import { | |||||
SDK_VERSION_HEADERS, | ||||||
} from '../common/http-request'; | ||||||
|
||||||
import { | ||||||
HeadlessPaymentMethodConfig, | ||||||
HeadlessPaymentMethodResponse, | ||||||
HeadlessPaymentMethodType, | ||||||
HeadlessPaymentRequestOptions, | ||||||
} from './headless-payment'; | ||||||
import PaymentMethod from './payment-method'; | ||||||
|
||||||
export default class PaymentMethodRequestSender { | ||||||
|
@@ -44,4 +50,79 @@ export default class PaymentMethodRequestSender { | |||||
params, | ||||||
}); | ||||||
} | ||||||
|
||||||
/** | ||||||
* GraphQL payment requests | ||||||
*/ | ||||||
loadPaymentWalletWithInitializationData( | ||||||
methodId: string, | ||||||
options: HeadlessPaymentRequestOptions, | ||||||
): Promise<Response<PaymentMethod>> { | ||||||
const entityId = this.getPaymentEntityId(methodId); | ||||||
|
||||||
const graphQLQuery = ` | ||||||
query { | ||||||
site { | ||||||
paymentWalletWithInitializationData(filter: {paymentWalletEntityId: "${entityId}"}) { | ||||||
clientToken | ||||||
initializationData | ||||||
} | ||||||
} | ||||||
} | ||||||
`; | ||||||
|
||||||
const requestOptions: HeadlessPaymentRequestOptions = { | ||||||
headers: { | ||||||
...options.headers, | ||||||
'Content-Type': 'application/json', | ||||||
}, | ||||||
body: { | ||||||
query: graphQLQuery, | ||||||
}, | ||||||
}; | ||||||
|
||||||
return this._requestSender | ||||||
.post<HeadlessPaymentMethodResponse>('/graphql', requestOptions) | ||||||
.then((response) => this.transformToPaymentMethodResponse(response, methodId)); | ||||||
} | ||||||
|
||||||
private transformToPaymentMethodResponse( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also maybe refactor this to a separate transformer file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to the separate file |
||||||
response: Response<HeadlessPaymentMethodResponse>, | ||||||
methodId: string, | ||||||
): Response<PaymentMethod> { | ||||||
const { | ||||||
body: { | ||||||
data: { | ||||||
site: { | ||||||
paymentWalletWithInitializationData: { clientToken, initializationData }, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
} = response; | ||||||
|
||||||
return { | ||||||
...response, | ||||||
body: { | ||||||
initializationData: initializationData | ||||||
? JSON.parse(atob(initializationData)) | ||||||
: null, | ||||||
clientToken, | ||||||
id: methodId, | ||||||
config: {}, | ||||||
method: '', | ||||||
supportedCards: [], | ||||||
type: 'PAYMENT_TYPE_API', | ||||||
}, | ||||||
}; | ||||||
} | ||||||
|
||||||
private getPaymentEntityId(methodId: string): HeadlessPaymentMethodType { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
const entityId = HeadlessPaymentMethodConfig[methodId]; | ||||||
|
||||||
if (!entityId) { | ||||||
throw new Error('Unable to get payment entity id.'); | ||||||
} | ||||||
Comment on lines
+93
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen after throwing this error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are not be able to make a request in this case. It will be catch with |
||||||
|
||||||
return entityId; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done