-
Notifications
You must be signed in to change notification settings - Fork 216
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 request to proxy server #2754
base: master
Are you sure you want to change the base?
Changes from 1 commit
74aa351
508e965
7afaa0f
01ba966
a14e11b
98337a4
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,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<T = any> { | ||
data: { | ||
site: HeadlessPaymentMethod<T>; | ||
}; | ||
} |
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,6 @@ | ||
export default interface HeadlessPaymentMethod<T = any> { | ||
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. Can 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. Would be better to use the |
||
paymentWalletWithInitializationData: { | ||
clientToken?: string; | ||
initializationData?: T; | ||
}; | ||
} |
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. Could you please move this to |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import HeadlessPaymentMethod from './headless-payment-method'; | ||
|
||
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,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 | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,10 @@ import { | |||||
SDK_VERSION_HEADERS, | ||||||
} from '../common/http-request'; | ||||||
|
||||||
import HeadlessPaymentMethodConfig from './headless-payment-method-config'; | ||||||
import { HeadlessPaymentMethodResponse } from './headless-payment-method-response'; | ||||||
import { HeadlessPaymentMethodType } from './headless-payment-method-type'; | ||||||
import HeadlessPaymentRequestOptions from './headless-payment-request-options'; | ||||||
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. Can we group them into a 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 folder |
||||||
import PaymentMethod from './payment-method'; | ||||||
|
||||||
export default class PaymentMethodRequestSender { | ||||||
|
@@ -44,4 +48,79 @@ export default class PaymentMethodRequestSender { | |||||
params, | ||||||
}); | ||||||
} | ||||||
|
||||||
/** | ||||||
* GraphQL payment requests | ||||||
*/ | ||||||
loadPaymentWalletWithInitializationData( | ||||||
methodId: string, | ||||||
options: HeadlessPaymentRequestOptions, | ||||||
): Promise<Response<PaymentMethod>> { | ||||||
const url = `/graphql`; | ||||||
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. Will this be reused? 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. removed |
||||||
|
||||||
const entityId = this.getPaymentEntityId(methodId); | ||||||
|
||||||
const graphQLQuery = ` | ||||||
query { | ||||||
site { | ||||||
paymentWalletWithInitializationData(filter: {paymentWalletEntityId: "${entityId}"}) { | ||||||
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 |
||||||
clientToken | ||||||
initializationData | ||||||
} | ||||||
} | ||||||
} | ||||||
`; | ||||||
|
||||||
const requestOptions: HeadlessPaymentRequestOptions = { | ||||||
headers: { | ||||||
...options.headers, | ||||||
'Content-Type': 'application/json', | ||||||
}, | ||||||
body: { | ||||||
query: graphQLQuery, | ||||||
}, | ||||||
}; | ||||||
|
||||||
return this._requestSender | ||||||
.post<HeadlessPaymentMethodResponse<string>>(url, 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 }, | ||||||
}, | ||||||
}, | ||||||
} = response; | ||||||
|
||||||
return { | ||||||
...response, | ||||||
body: { | ||||||
initializationData: JSON.parse( | ||||||
atob(paymentWalletWithInitializationData.initializationData), | ||||||
), | ||||||
clientToken: paymentWalletWithInitializationData.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
+81
to
+83
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.
Will it work for you, if
any
be changed withunknown
?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.
Are we not able to define the actual type here?
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.
@bc-peng removed T parameter as unnecessary in this case. we can define type as
string