Introduction
In our previous article we saw how to call PayPal API in SSIS. Now in this article, we will see How to Make PayPal API Call in ODBC from the SQL Server. This blog mainly focuses on ODBC approach but steps mentioned to call PayPal APIs can be useful for any developer regardless of which programming language or tool set you use. We will also see How to send basic Authorization header along with body in OAuth2.
We will go through the steps to Create PayPal Order from MS SQL Server.
In nutshell, this post will focus on how to make PayPal API Call using ODBC Driver.
Prerequisites
Before we perform the steps listed in this article, you will need to make sure the following prerequisites are met:
- A first requirement, make sure that Microsoft Access installed
- Make sure to have ZappySys ODBC PowerPack installed.
- Credentials of PayPal API. Click here to create API.
What is PayPal?
PayPal is an American company operating a worldwide online payments system that supports online money transfers and serves as an electronic alternative to traditional paper methods like checks and money orders. The company operates as a payment processor for online vendors, auction sites, and many other commercial users, for which it charges a fee in exchange for benefits such as one-click transactions and password memory. PayPal’s payment system, also called PayPal, is considered a type of payment rail.
Get Started with PayPal API
If you have need to automate PayPal operations (e.g. read / write / update / delete ) then you can use PayPal REST API. Before you can integrate a PayPal product or solution, you must set up your development environment to get OAuth 2.0 client ID and secret credentials for the sandbox and live environments. You exchange these credentials for an access token that authorizes your REST API calls. To test your web and mobile apps, you create sandbox accounts.
Get PayPal credentials
To generate OAuth 2.0 credentials for the sandbox and live environments:
- Log into Dashboard and type your PayPal personal or business account email and password.
- In the REST API apps section, click Create App. The purpose of this app is to generate your credentials.
- Type a name for your app and click Create App. The page shows your sandbox app information, which includes your credentials.
Note: To show your live app information, toggle to Live.
- Copy and save the client ID and secret for your sandbox app.
- Review your app details and save your app.
Call PayPal Create Order API in SQL Server (T-SQL) using Data Gateway
Let’s start with an example. We use JSON Driver to make the call to PayPal API, we will Create the PayPal Order from SQL Server.
In REST API calls, include the URL to the API service for the environment:
- Sandbox:
https://api.sandbox.paypal.com
- Live:
https://api.paypal.com
- To do this, first of all, we will open the ZappySys Data Gateway Configuration:
- Add the Native – ZappySys JSON Driver Data source.
- Now it’s time to connect with PayPal. Let’s use JSON Driver Data source with the following URL:
1https://api.sandbox.paypal.com/v2/checkout/orders
1234567891011{"intent": "CAPTURE","purchase_units": [{"amount": {"currency_code": "USD","value": "100.00"}}]} - Create Basic OAuth Client Credentials Grant (Configure ZS-OAuth Connection).
In the SSIS ZS OAuth Connection Manager, Select Provider as Custom, select version as OAuth2 and select Grant Type as Client Credentials Grant. Enter the credentials and access token url and click OK.
1https://api.sandbox.paypal.com/v1/oauth2/token - Furthermore, Now click on Test Connection button to test PayPal Connection.
- Write your Query and Preview Data.
- (Optional Step) If your Query use POST method (e.g. Create Invoice) to submit data then you have Save Metadata (Read More). We will use this meta file in later step when we call POST request.
- Now using Code Generator we will generate the query.
Setup Linked Server in MS SQL Server for PayPal
Once you configured the data source in Gateway, we can now setup Linked Server in SQL Server to query API Calls.
- Assuming you have installed SQL Server and SSMS. If not then get both for FREE from here: Get SQL Server Express and Get SSMS
- Open SSMS and connect to SQL Server.
- Go to Root > Server Objects > Linked Servers node. Right click and click New Linked Server…
- Now enter the linked server name, select Provider as SQL Native Client
- Enter data source as GatewayServerName, PORT_NUMBER where server name is where ZappySys Gateway is running (Can be same as SQL Server machine or remote machine). Default PORT_NUMBER is 5000 but confirm on Data gateway > General tab in case its different.
- Enter Catalog Name. This must match name from Data gateway Data sources grid > Name column
- Click on Security Tab and select last option “Be made using this security context“. Enter your gateway user account here.
- Click OK to save Linked Server
Create Order in PayPal from SQL Server (Call POST API Method)
- To Create Order in PayPal we need to execute the query like this. Please refer to this link for more information: Create Order example.
Important Note*: Below query uses META property (fully explained here). This is needed if you creating anew record using POST, PUT. If you don’t pass META='{xxxx}’ in query driver calls REST API two times (first time to parse metadata and second time to get actual data). So that you find some odd thing in PayPal if you execute the query without Meta then you will find two same Orders created in your PayPal as the driver makes two calls as mentioned above.123456789101112131415161718192021-- In below query rather than WITH(META=''[{"Name": "Success","Type": "Int32"}]'' ....-- You can also use WITH(META=''c:\my-meta-file.txt'' .... We created this file in previous sectionSELECT * FROM OPENQUERY([MY_LINKED_SERVER_NAME], 'SELECT * FROM $WITH(META=''[{"Name": "Success","Type": "Int32"}]'',Src=''https://api.sandbox.paypal.com/v2/checkout/orders'',RequestData=''{"intent": "CAPTURE","purchase_units": [{"amount": {"currency_code": "USD","value": "100.00"}}]}'',RequestContentTypeCode=''ApplicationJson'',RequestMethod=''POST'')') - Here is the preview after you run REST API query in SQL Server. It will return created Order Id, Links and status. Notice that you can override default configuration by supplying many parameters in WITH clause.
- You can also find this created Order by opening that URL in the browser.
PayPal API integration in ODBC apps (e.g. Power BI, Excel, Informatica, Access, C#…)
So far we have looked at SQL Server integration only, but what if you like to use same powerful ZappySys API drivers part of ODBC PowerPack in other Apps like Power BI, Excel, MS Access, Informatica and many more… ?? Well not to worry if you know that App supports ODBC you can use ZappySys ODBC Driver same way described earlier… only difference is you dont need Linked Server and you dont have to use OPENQUERY statement when you use ODBC Drivers.
BI / Reporting Tools Integration |
ETL Tools Integration | Programming Languages Integration |
|
|
|
Conclusion
So in this blog, we learned how to generate a PayPal API Token, Load PayPal data in MS SQL Server table using ODBC JSON / REST API Driver in a very simple way. You can achieve many more functionalities with this tool. Check our blogs/articles on JSON / REST API Driver to find out what this tool is capable of more.
References
Finally, you can use the following links for more information about the use of PayPal API with our tools:
- Landing Page for ODBC JSON / REST API Driver, you can also find Tutorial Video here.
- Help File: Documentation of JSON Driver.
- PayPal API