How do I set up and process an API call using the Payflow .NET SDK?
Disclaimer: This code isn't intended to be used in a production environment; it's just an example and should be used as such. The code is supplied "as is" with no warranty and should be used only for reference.
Download the Payflow .NET SDK
- Log in to the PayPal Developer Portal and download the Payflow .NET SDK.
- Extract the files from the .zip file, and install the SDK.
- Once the SDK is installed, rename the file from payflow_dotNET_2.0.dll to payflow_dotNET.dll.
Create a new project in Visual Studio
- Open Visual Studio.
- Select File > New Project.
Create a new application
- For this example, create a C#, ASP.NET Web Application called "MyFirstAPICall" as shown here:
- Click OK, and you'll see your first page, Default.aspx.
Reference the Payflow_dotNET.dll file
- Right-click References under Solution Explorer, then select Add Reference.
- On the Browse tab, go to the Payflow_dotNET.dll file, then click OK.
Add a new button to the page
- Go back to the Default.aspx page.
- Click Design.
- Drag a Button from the toolbox onto the page.
View the button code and add using statements
- Double-click the new button.
- By default you'll see the following code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace MyFirstAPICall{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){
}
protected void Button1_Click(object sender, EventArgs e){
}
}
}
- Add the following statements to the "using" section to include the PayPal SDK DLL for the file:
using PayPal.Payments.Common.Utility; using PayPal.Payments.Communication; using PayPal.Payments.DataObjects;
Add code for the Button1_Click function This step uses the actual code of the application.
- The following is sample code for the Button1_Click function:
//Set your API Credentials ///////////////////////////////////////////// //PLEASE NOTE THIS IS NOT CONSIDERED SECURE// //THIS IS FOR EXAMPLE PURPOSES ONLY////////// ///////////////////////////////////////////// string USER = "xxxx"; string VENDOR = "xxxx"; string PARTNER = "PayPal"; string PWD = "xxxxx"; //set the port int hostPort = 443; //set the timeout int timeOut = 30; //set the host address (pilot-) for testing string hostAddress = "pilot-payflowpro.paypal.com"; //build a test request string string Request = "USER=" + USER + "&PWD=" + PWD + "&VENDOR=" + VENDOR + "&PARTNER=" + PARTNER + "&TRXTYPE=S&TENDER=C&ACCT=4111111111111111&EXPDATE=1213&CVV2=123&AMT=99.00&FIRSTNAME=John&LASTNAME=Smith&STREET=123 Main St.&CITY=San Jose&STATE=CA&ZIP=12345&COMMENT1=Reservation&VERBOSITY=MEDIUM"; //build a Payflow API Object with the address, port, and timeout PayflowNETAPI pfObj = new PayflowNETAPI(hostAddress, hostPort, timeOut); //set the response variable to the value of the response from the call and make the call string response = pfObj.SubmitTransaction(Request + PayflowUtility.RequestId, PayflowUtility.RequestId); //get the status from the response string status = PayflowUtility.GetStatus(response); //print the status to the page Response.Write(status); Response.Write("<br />");//line break //API Request: Response.Write("API Request:\n"); Response.Write("<br />");//line break //print the raw request string to the page Response.Write(Request); Response.Write("<br />");//line break //API Response: Response.Write("\nAPI Response:\n"); Response.Write("<br />");//line break //print the raw API response String to the page Response.Write(response);
- Set the USER, VENDOR, PARTNER, and PWD variables to your API credentials for the Payflow API.
- Set the preceding code between the curly braces for the Button1_Click function.
Build and run the application
- Under Build, select Build MyFirstAPICall.
- Click the green arrow to run the application.
- Your browser will open and you'll see the button, which will be labeled Button (unless you changed it).
- Click the button to show your API Request and Response. For example, the following message indicates a successful transaction: