Search code examples
paypalpaypal-sandboxpaypal-rest-sdk

how to calculate tax using paypal api


i installed the .net sdk of paypal and created an app in sandbox environment next i picked up the clientId and secret and used the following sample code to make a payment.

    static void Main(string[] args)
    {
        // Get a reference to the config
        var config = ConfigManager.Instance.GetProperties();

        // Use OAuthTokenCredential to request an access token from PayPal
        var accessToken = new OAuthTokenCredential(config["clientId"], config["clientSecret"]);

        var apiContext = new APIContext(accessToken.GetAccessToken());



        var payment = Payment.Create(apiContext, new Payment
        {
            intent = "sale",
            payer = new Payer
            {
                payment_method = "paypal"
            },
            transactions = new List<Transaction>
        {
            new Transaction
            {
                description = "Test",
                invoice_number = "009",
                amount = new Amount
                {
                    currency = "EUR",
                    total = "41.00",
                    details = new Details
                    {
                        tax = "0",
                        shipping = "0",
                        subtotal = "40",
                        handling_fee = "1"

                    }
                },
                item_list = new ItemList
                {
                    items = new List<Item>
                    {
                        new Item
                        {
                            name = "Room 12",
                            currency = "EUR",
                            price = "10",
                            quantity = "4",
                        }
                    }
                }
            }
        },
            redirect_urls = new RedirectUrls
            {
                return_url = "https://google.de/",
                cancel_url = "https://google.de/"
            }
        });
    }

in the transaction i have to pass Tax information.

Is there was that i let paypal calculate the tax and i just pass amount information along with address and some other information if required ?


Solution

  • No, you must calculate the tax yourself.

    By default the user will be able to select their shipping address at PayPal, which is recommended as this saves them from having to type it manually. Given that their address can change during the PayPal checkout, you may wish to calculate a new tax amount and/or shipping amount based on the selected address. You can do this using the JS SDK's onShippingChange callback.


    Firstly, though, it appears you may be using a deprecated SDK and deprecated v1/payments API, and also a redirect away from your site to PayPal, all of which is old. Don't do any of this.

    Instead: follow the PayPal Checkout integration guide and make 2 routes on your server, one for 'Create Order' and one for 'Capture Order' (see the optional step 5 in 'Add and modify the code'). Both of these routes should return only JSON data (no HTML or text). There is a Checkout-Java-SDK you can use, or integrate with your own direct HTTPS API calls (obtain an access_token first, it can be cached but expires in 9 hours).

    Inside the 2nd capture route on your server, when the capture API call is successful you should store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id, which is the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding your return JSON to the frontend caller.

    Pair those 2 routes with the frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server