Home > Software engineering >  how to wait for the response from paypal after the payment is successful?
how to wait for the response from paypal after the payment is successful?

Time:06-22

I would like to integrate a payment system but I don't see how I should wait for the response from paypal once the person has paid. all the examples on the internet are based on asp.net, I don't think we can do the same thing........................................................................................................................

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using PayPalCheckoutSdk.Core;
using PayPalCheckoutSdk.Orders;
using PayPalCheckoutSdk.Payments;
using PayPalHttp;

namespace Discord.Modules
{
    public class PaypalPayment
    {
        private static OrderRequest BuildRequestBody()
        {
            OrderRequest orderRequest = new OrderRequest()
            {
                CheckoutPaymentIntent = "CAPTURE",

                ApplicationContext = new ApplicationContext
                {
                    BrandName = "EXAMPLE INC",
                    LandingPage = "BILLING",
                    CancelUrl = "https://www.example.com",
                    ReturnUrl = "https://www.example.com",
                    UserAction = "CONTINUE",
                    ShippingPreference = "SET_PROVIDED_ADDRESS"
                },
                PurchaseUnits = new List<PurchaseUnitRequest>
                {
                    new PurchaseUnitRequest{
                        ReferenceId =  "PUHF",
                        Description = "Sporting Goods",
                        CustomId = "CUST-HighFashions",
                        SoftDescriptor = "HighFashions",
                        AmountWithBreakdown = new AmountWithBreakdown
                        {
                            CurrencyCode = "USD",
                            Value = "220.00",
                            AmountBreakdown = new AmountBreakdown
                            {
                                ItemTotal = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "180.00"
                                },
                                Shipping = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "20.00"
                                },
                                Handling = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "10.00"
                                },
                                TaxTotal = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "20.00"
                                },
                                ShippingDiscount = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "10.00"
                                }
                            }
                        },
                        Items = new List<Item>
                        {
                            new Item
                            {
                                Name = "T-shirt",
                                Description = "Green XL",
                                Sku = "sku01",
                                UnitAmount = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "90.00"
                                },
                                Tax = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "10.00"
                                },
                                Quantity = "1",
                                Category = "PHYSICAL_GOODS"
                            },
                            new Item
                            {
                                Name = "Shoes",
                                Description = "Running, Size 10.5",
                                Sku = "sku02",
                                UnitAmount = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "45.00"
                                },
                                Tax = new PayPalCheckoutSdk.Orders.Money
                                {
                                    CurrencyCode = "USD",
                                    Value = "5.00"
                                },
                                Quantity = "2",
                                Category = "PHYSICAL_GOODS"
                            }
                        },
                        ShippingDetail = new ShippingDetail
                        {
                            Name = new Name
                            {
                                FullName = "John Doe"
                            },
                            AddressPortable = new AddressPortable
                            {
                                AddressLine1 = "123 Townsend St",
                                AddressLine2 = "Floor 6",
                                AdminArea2 = "San Francisco",
                                AdminArea1 = "CA",
                                PostalCode = "94107",
                                CountryCode = "US"
                            }
                        }
                    }
                }
            };

            return orderRequest;
        }


        public async static Task<HttpResponse> CreateOrder(bool debug = false)
        {
            var request = new OrdersCreateRequest();
            request.Headers.Add("prefer", "return=representation");
            request.RequestBody(BuildRequestBody());
            var client = PayPalClient.client();
            var response = await client.Execute(request);

            if (debug)
            {
                var result = response.Result<Order>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
                Console.WriteLine("Links:");
                foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
                Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
                Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
            }

            return response;
        }

        public async static Task<HttpResponse> CaptureOrder(string OrderId, bool debug = false)
        {
            var request = new OrdersCaptureRequest(OrderId);
            request.Prefer("return=representation");
            request.RequestBody(new OrderActionRequest());
            var client = PayPalClient.client();
            var response = await client.Execute(request);

            if (debug)
            {
                var result = response.Result<Order>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
                Console.WriteLine("Links:");
                foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                Console.WriteLine("Capture Ids: ");
                foreach (PurchaseUnit purchaseUnit in result.PurchaseUnits)
                {
                    foreach (PayPalCheckoutSdk.Orders.Capture capture in purchaseUnit.Payments.Captures)
                    {
                        Console.WriteLine("\t {0}", capture.Id);
                    }
                }
                AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
                Console.WriteLine("Buyer:");
                Console.WriteLine("\tEmail Address: {0}\n\tName: {1} {2}\n",
                    result.Payer.Email,
                    result.Payer.Name.GivenName,
                    result.Payer.Name.Surname);
                Console.WriteLine("Response JSON:\n{0}", PayPalClient.ObjectToJSONString(result));
            }

            return response;
        }


        public static async Task asdasd()
        {
            Console.WriteLine("Running Capture Intent Flow..");
            var createOrderResponse = CreateOrder(true).Result;
            var createOrderResult = createOrderResponse.Result<Order>();
            Console.WriteLine("Status: {0}", createOrderResult.Status);
            Console.WriteLine("Order Id: {0}", createOrderResult.Id);
            Console.WriteLine("Intent: {0}", createOrderResult.CheckoutPaymentIntent);
            Console.WriteLine("Links:");
            foreach (PayPalCheckoutSdk.Orders.LinkDescription link in createOrderResult.Links)
            {
                Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
            AmountWithBreakdown amount = createOrderResult.PurchaseUnits[0].AmountWithBreakdown;
            Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);

            Console.WriteLine("Copy approve link and paste it in browser. Login with buyer account and follow the instructions.\nOnce approved hit enter...\n");
            Console.Read();

            Console.WriteLine("Capturing the payment...");
            var captureOrderResponse = CaptureOrder(createOrderResult.Id, true).Result;
            var captureOrderResult = captureOrderResponse.Result<Order>();
            var captureId = "";
            Console.WriteLine("Status: {0}", captureOrderResult.Status);
            Console.WriteLine("Order Id: {0}", captureOrderResult.Id);
            Console.WriteLine("Intent: {0}", captureOrderResult.CheckoutPaymentIntent);
            Console.WriteLine("Links:");
            foreach (PayPalCheckoutSdk.Orders.LinkDescription link in captureOrderResult.Links)
            {
                Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
            foreach (PurchaseUnit purchaseUnit in captureOrderResult.PurchaseUnits)
            {
                foreach (PayPalCheckoutSdk.Orders.Capture capture in purchaseUnit.Payments.Captures)
                {
                    captureId = capture.Id;
                }
            }
            AmountWithBreakdown captureAmount = captureOrderResult.PurchaseUnits[0].AmountWithBreakdown;
            Console.WriteLine("Buyer:");
            Console.WriteLine("\tEmail Address: {0}\n\tName: {1} {2}",
            captureOrderResult.Payer.Email, captureOrderResult.Payer.Name.GivenName, captureOrderResult.Payer.Name.Surname);

            Console.WriteLine("Refunding the Order....");
            var refundOrderResponse = CapturesRefund(captureId, true).Result;
            var refundOrderResult = refundOrderResponse.Result<PayPalCheckoutSdk.Payments.Refund>();
            Console.WriteLine("Status: {0}", refundOrderResult.Status);
            Console.WriteLine("Refund Id: {0}", refundOrderResult.Id);
            Console.WriteLine("Links:");
            foreach (PayPalCheckoutSdk.Payments.LinkDescription link in refundOrderResult.Links)
            {
                Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
        }

        public async static Task<HttpResponse> CapturesRefund(string CaptureId, bool debug = false)
        {
            var request = new CapturesRefundRequest(CaptureId);
            request.Prefer("return=representation");
            RefundRequest refundRequest = new RefundRequest()
            {
                Amount = new PayPalCheckoutSdk.Payments.Money
                {
                    Value = "20.00",
                    CurrencyCode = "USD"
                }
            };
            request.RequestBody(refundRequest);
            var response = await PayPalClient.client().Execute(request);

            if (debug)
            {
                var result = response.Result<PayPalCheckoutSdk.Payments.Refund>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Refund Id: {0}", result.Id);
                Console.WriteLine("Links:");
                foreach (PayPalCheckoutSdk.Payments.LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
            }
            return response;
        }

    }

    public class PayPalClient
    {
        /**
            Setting up PayPal environment with credentials with sandbox cerdentails. 
            For Live, this should be LiveEnvironment Instance. 
         */
        public static PayPalEnvironment environment()
        {
            return new SandboxEnvironment(
                 System.Environment.GetEnvironmentVariable("") != null ?
                 System.Environment.GetEnvironmentVariable("") : "",
                System.Environment.GetEnvironmentVariable("") != null ?
                 System.Environment.GetEnvironmentVariable("") : "");
        }

        /**
            Returns PayPalHttpClient instance which can be used to invoke PayPal API's.
         */
        public static HttpClient client()
        {
            return new PayPalHttpClient(environment());
        }

        public static HttpClient client(string refreshToken)
        {
            return new PayPalHttpClient(environment(), refreshToken);
        }

        /**
            This method can be used to Serialize Object to JSON string.
        */
        public static String ObjectToJSONString(Object serializableObject)
        {
            MemoryStream memoryStream = new MemoryStream();
            var writer = JsonReaderWriterFactory.CreateJsonWriter(
                        memoryStream, Encoding.UTF8, true, true, "  ");
            DataContractJsonSerializer ser = new DataContractJsonSerializer(serializableObject.GetType(), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });
            ser.WriteObject(writer, serializableObject);
            memoryStream.Position = 0;
            StreamReader sr = new StreamReader(memoryStream);
            return sr.ReadToEnd();
        }
    }

}

CodePudding user response:

    private static OrderRequest BuildRequestBody()
    {
        OrderRequest orderRequest = new OrderRequest()
        {
            CheckoutPaymentIntent = "CAPTURE",

            ApplicationContext = new ApplicationContext
            {
                BrandName = "EXAMPLE INC",
                LandingPage = "BILLING",
                CancelUrl = "https://www.example.com",
                ReturnUrl = "https://www.example.com",
                UserAction = "CONTINUE",
                ShippingPreference = "SET_PROVIDED_ADDRESS"
            },
            PurchaseUnits = new List<PurchaseUnitRequest>
            {
                new PurchaseUnitRequest{
                    ReferenceId =  "PUHF",
                    Description = "Sporting Goods",
                    CustomId = "CUST-HighFashions",
                    SoftDescriptor = "HighFashions",
                    AmountWithBreakdown = new AmountWithBreakdown
                    {
                        CurrencyCode = "USD",
                        Value = "220.00",
                        AmountBreakdown = new AmountBreakdown
                        {
                            ItemTotal = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "180.00"
                            },
                            Shipping = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "20.00"
                            },
                            Handling = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "10.00"
                            },
                            TaxTotal = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "20.00"
                            },
                            ShippingDiscount = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "10.00"
                            }
                        }
                    },
                    Items = new List<Item>
                    {
                        new Item
                        {
                            Name = "T-shirt",
                            Description = "Green XL",
                            Sku = "sku01",
                            UnitAmount = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "90.00"
                            },
                            Tax = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "10.00"
                            },
                            Quantity = "1",
                            Category = "PHYSICAL_GOODS"
                        },
                        new Item
                        {
                            Name = "Shoes",
                            Description = "Running, Size 10.5",
                            Sku = "sku02",
                            UnitAmount = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "45.00"
                            },
                            Tax = new PayPalCheckoutSdk.Orders.Money
                            {
                                CurrencyCode = "USD",
                                Value = "5.00"
                            },
                            Quantity = "2",
                            Category = "PHYSICAL_GOODS"
                        }
                    },
                    ShippingDetail = new ShippingDetail
                    {
                        Name = new Name
                        {
                            FullName = "John Doe"
                        },
                        AddressPortable = new AddressPortable
                        {
                            AddressLine1 = "123 Townsend St",
                            AddressLine2 = "Floor 6",
                            AdminArea2 = "San Francisco",
                            AdminArea1 = "CA",
                            PostalCode = "94107",
                            CountryCode = "US"
                        }
                    }
                }
            }
        };

        return orderRequest;
    }


    public async static Task<HttpResponse> CreateOrder(bool debug = false)
    {
        var request = new OrdersCreateRequest();
        request.Headers.Add("prefer", "return=representation");
        request.RequestBody(BuildRequestBody());
        var client = PayPalClient.client();
        var response = await client.Execute(request);

        if (debug)
        {
            var result = response.Result<Order>();
            Console.WriteLine("Status: {0}", result.Status);
            Console.WriteLine("Order Id: {0}", result.Id);
            Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
            Console.WriteLine("Links:");
            foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
            {
                Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
            AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
            Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
            Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
        }

        return response;
    }

    public async static Task<HttpResponse> CaptureOrder(string OrderId, bool debug = false)
    {
        var request = new OrdersCaptureRequest(OrderId);
        request.Prefer("return=representation");
        request.RequestBody(new OrderActionRequest());
        var client = PayPalClient.client();
        var response = await client.Execute(request);

        if (debug)
        {
            var result = response.Result<Order>();
            Console.WriteLine("Status: {0}", result.Status);
            Console.WriteLine("Order Id: {0}", result.Id);
            Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
            Console.WriteLine("Links:");
            foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
            {
                Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
            Console.WriteLine("Capture Ids: ");
            foreach (PurchaseUnit purchaseUnit in result.PurchaseUnits)
            {
                foreach (PayPalCheckoutSdk.Orders.Capture capture in purchaseUnit.Payments.Captures)
                {
                    Console.WriteLine("\t {0}", capture.Id);
                }
            }
            AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
            Console.WriteLine("Buyer:");
            Console.WriteLine("\tEmail Address: {0}\n\tName: {1} {2}\n",
                result.Payer.Email,
                result.Payer.Name.GivenName,
                result.Payer.Name.Surname);
            Console.WriteLine("Response JSON:\n{0}", PayPalClient.ObjectToJSONString(result));
        }

        return response;
    }


    public static async Task asdasd()
    {
        Console.WriteLine("Running Capture Intent Flow..");
        var createOrderResponse = CreateOrder(true).Result;
        var createOrderResult = createOrderResponse.Result<Order>();
        Console.WriteLine("Status: {0}", createOrderResult.Status);
        Console.WriteLine("Order Id: {0}", createOrderResult.Id);
        Console.WriteLine("Intent: {0}", createOrderResult.CheckoutPaymentIntent);
        Console.WriteLine("Links:");
        foreach (PayPalCheckoutSdk.Orders.LinkDescription link in createOrderResult.Links)
        {
            Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
        }
        AmountWithBreakdown amount = createOrderResult.PurchaseUnits[0].AmountWithBreakdown;
        Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);

        Console.WriteLine("Copy approve link and paste it in browser. Login with buyer account and follow the instructions.\nOnce approved hit enter...\n");
        Console.Read();

        Console.WriteLine("Capturing the payment...");
        var captureOrderResponse = CaptureOrder(createOrderResult.Id, true).Result;
        var captureOrderResult = captureOrderResponse.Result<Order>();
        var captureId = "";
        Console.WriteLine("Status: {0}", captureOrderResult.Status);
        Console.WriteLine("Order Id: {0}", captureOrderResult.Id);
        Console.WriteLine("Intent: {0}", captureOrderResult.CheckoutPaymentIntent);
        Console.WriteLine("Links:");
        foreach (PayPalCheckoutSdk.Orders.LinkDescription link in captureOrderResult.Links)
        {
            Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
        }
        foreach (PurchaseUnit purchaseUnit in captureOrderResult.PurchaseUnits)
        {
            foreach (PayPalCheckoutSdk.Orders.Capture capture in purchaseUnit.Payments.Captures)
            {
                captureId = capture.Id;
            }
        }
        AmountWithBreakdown captureAmount = captureOrderResult.PurchaseUnits[0].AmountWithBreakdown;
        Console.WriteLine("Buyer:");
        Console.WriteLine("\tEmail Address: {0}\n\tName: {1} {2}",
        captureOrderResult.Payer.Email, captureOrderResult.Payer.Name.GivenName, captureOrderResult.Payer.Name.Surname);

        Console.WriteLine("Refunding the Order....");
        var refundOrderResponse = CapturesRefund(captureId, true).Result;
        var refundOrderResult = refundOrderResponse.Result<PayPalCheckoutSdk.Payments.Refund>();
        Console.WriteLine("Status: {0}", refundOrderResult.Status);
        Console.WriteLine("Refund Id: {0}", refundOrderResult.Id);
        Console.WriteLine("Links:");
        foreach (PayPalCheckoutSdk.Payments.LinkDescription link in refundOrderResult.Links)
        {
            Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
        }
    }

    public async static Task<HttpResponse> CapturesRefund(string CaptureId, bool debug = false)
    {
        var request = new CapturesRefundRequest(CaptureId);
        request.Prefer("return=representation");
        RefundRequest refundRequest = new RefundRequest()
        {
            Amount = new PayPalCheckoutSdk.Payments.Money
            {
                Value = "20.00",
                CurrencyCode = "USD"
            }
        };
        request.RequestBody(refundRequest);
        var response = await PayPalClient.client().Execute(request);

        if (debug)
        {
            var result = response.Result<PayPalCheckoutSdk.Payments.Refund>();
            Console.WriteLine("Status: {0}", result.Status);
            Console.WriteLine("Refund Id: {0}", result.Id);
            Console.WriteLine("Links:");
            foreach (PayPalCheckoutSdk.Payments.LinkDescription link in result.Links)
            {
                Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
            Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
        }
        return response;
    }

}

public class PayPalClient
{
    /**
        Setting up PayPal environment with credentials with sandbox cerdentails. 
        For Live, this should be LiveEnvironment Instance. 
     */
    public static PayPalEnvironment environment()
    {
        return new SandboxEnvironment(
             System.Environment.GetEnvironmentVariable("") != null ?
             System.Environment.GetEnvironmentVariable("") : "",
            System.Environment.GetEnvironmentVariable("") != null ?
             System.Environment.GetEnvironmentVariable("") : "");
    }

    /**
        Returns PayPalHttpClient instance which can be used to invoke PayPal API's.
     */
    public static HttpClient client()
    {
        return new PayPalHttpClient(environment());
    }

    public static HttpClient client(string refreshToken)
    {
        return new PayPalHttpClient(environment(), refreshToken);
    }

    /**
        This method can be used to Serialize Object to JSON string.
    */
    public static String ObjectToJSONString(Object serializableObject)
    {
        MemoryStream memoryStream = new MemoryStream();
        var writer = JsonReaderWriterFactory.CreateJsonWriter(
                    memoryStream, Encoding.UTF8, true, true, "  ");
        DataContractJsonSerializer ser = new DataContractJsonSerializer(serializableObject.GetType(), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });
        ser.WriteObject(writer, serializableObject);
        memoryStream.Position = 0;
        StreamReader sr = new StreamReader(memoryStream);
        return sr.ReadToEnd();
    }
}

}

CodePudding user response:

Your code is already using the Checkout-NET-SDK (that abstracts the v2/checkout/orders PayPal REST API) to create and capture a PayPal order. The capture API response provides an immediate notification of success or failure. In particular purchase_units[0].payments.captures[0].id is the PayPal transaction ID and what should be persisted in your database. The order total should also be validated at this point in time.


Separately, it appears you are using an old redirect-based flow for the approval. Instead, follow the PayPal Checkout integration guide and have the create order and capture order API steps be performed by 2 routes (url paths) on your server. Both of these routes should return only JSON data (no HTML or text). The 2nd route takes an order id as an input parameter (path or body variable) and as mentioned should examine the result of the operation to verify the amount was correct and store its resulting payment details, as well as performing any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding your return JSON to the frontend caller. In the event of an error forward the JSON details of it as well, since the frontend must handle such cases.

Pair those 2 routes with this frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server . (If you need to send any additional data from the client to the server, such as an items array or selected options, add a body parameter to the fetch with a value that is a JSON string or object)

  • Related