Skip to content

Commit 48269fe

Browse files
authored
feat: add PayPal Credit button example (#45)
1 parent 53d4d33 commit 48269fe

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

client/oneTimePayment/html/src/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ <h1>One-Time Payment Integrations</h1>
1111
<ul>
1212
<li>
1313
<a href="/recommended/index.html"
14-
>Recommended integration with PayPal, Venmo, and PayLater buttons</a
14+
>Recommended integration with PayPal, Venmo, PayLater, and PayPal
15+
Credit buttons</a
1516
>
1617
</li>
1718
<li>

client/oneTimePayment/html/src/recommended/app.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ async function onPayPalLoaded() {
2424
paymentMethods.getDetails("paylater");
2525
setupPayLaterButton(sdkInstance, paylaterPaymentMethodDetails);
2626
}
27+
28+
if (paymentMethods.isEligible("credit")) {
29+
const paypalCreditPaymentMethodDetails =
30+
paymentMethods.getDetails("credit");
31+
setupPayPalCreditButton(sdkInstance, paypalCreditPaymentMethodDetails);
32+
}
2733
} catch (error) {
2834
console.error(error);
2935
}
@@ -107,6 +113,31 @@ async function setupPayLaterButton(sdkInstance, paylaterPaymentMethodDetails) {
107113
});
108114
}
109115

116+
async function setupPayPalCreditButton(
117+
sdkInstance,
118+
paypalCreditPaymentMethodDetails,
119+
) {
120+
const paypalCreditPaymentSession =
121+
sdkInstance.createPayPalCreditOneTimePaymentSession(paymentSessionOptions);
122+
123+
const { countryCode } = paypalCreditPaymentMethodDetails;
124+
const paypalCreditButton = document.querySelector("#paypal-credit-button");
125+
126+
paypalCreditButton.countryCode = countryCode;
127+
paypalCreditButton.removeAttribute("hidden");
128+
129+
paypalCreditButton.addEventListener("click", async () => {
130+
try {
131+
await paypalCreditPaymentSession.start(
132+
{ presentationMode: "auto" },
133+
createOrder(),
134+
);
135+
} catch (error) {
136+
console.error(error);
137+
}
138+
});
139+
}
140+
110141
async function getBrowserSafeClientToken() {
111142
const response = await fetch("/paypal-api/auth/browser-safe-client-token", {
112143
method: "GET",

client/oneTimePayment/html/src/recommended/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ <h1>One-Time Payment Recommended Integration</h1>
2222
id="paylater-button"
2323
hidden
2424
></paypal-pay-later-button>
25+
<paypal-credit-button
26+
id="paypal-credit-button"
27+
hidden
28+
></paypal-credit-button>
2529
</div>
2630
<script src="app.js"></script>
2731

0 commit comments

Comments
 (0)