Try this exercise. Fill in the missing part by typing it in.
To integrate third-party payment gateways like Stripe and PayPal into our Payment App, we can use the ___ libraries and ___ APIs provided by the payment gateway providers. These libraries and APIs allow us to interact with the payment gateway services and perform various operations such as creating payment intents, processing payments, and handling webhooks.
The payment gateway libraries provide functions and classes that we can use to initialize the payment gateway clients and perform different actions. For example, with the Stripe library, we can create a Stripe client by passing our ___ key:
1const stripe = require('stripe')('YOUR_STRIPE_SECRET_KEY');
Similarly, we can initialize the PayPal client by passing the ___ and ___ keys:
1const paypal = require('paypal-rest-sdk');
2paypal.configure({
3 mode: 'sandbox', // or 'live' for production
4 client_id: 'YOUR_PAYPAL_CLIENT_ID',
5 client_secret: 'YOUR_PAYPAL_CLIENT_SECRET',
6});
Once we have initialized the payment gateway client, we can use it to perform various operations. For example, with Stripe, we can create a payment intent by calling the create
method on the paymentIntents
object:
1const paymentIntent = await stripe.paymentIntents.create({
2 amount: 1000, // Amount in cents
3 currency: 'usd', // Currency code
4});
And with PayPal, we can create a payment by calling the create
method on the payment
object:
1const create_payment_json = {
2 intent: 'sale',
3 payer: {
4 payment_method: 'paypal',
5 },
6 redirect_urls: {
7 return_url: 'http://localhost:3000/success',
8 cancel_url: 'http://localhost:3000/cancel',
9 },
10 transactions: [
11 {
12 item_list: {
13 items: [
14 {
15 name: 'Item Name',
16 sku: 'Item SKU',
17 price: '10.00',
18 currency: 'USD',
19 quantity: 1,
20 },
21 ],
22 },
23 amount: {
24 currency: 'USD',
25 total: '10.00',
26 },
27 description: 'This is the payment description.',
28 },
29 ],
30};
31
32paypal.payment.create(create_payment_json, function (error, payment) {
33 if (error) {
34 throw error;
35 } else {
36 console.log(payment);
37 }
38});
By integrating these payment gateway libraries and APIs into our Payment App, we can provide our users with a seamless and secure payment experience by supporting different payment methods and handling transactions through these trusted payment gateway services.
Write the missing line below.