PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 4, 2022

[FIXED] how to calculate tax using paypal api

 July 04, 2022     paypal, paypal-rest-sdk, paypal-sandbox     No comments   

Issue

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



Answered By - Preston PHX
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing