PayPal Hacks. 100 Industrial-Strength Tips and Tools Free Open Book

PayPal Hacks. 100 Industrial-Strength Tips and Tools

Previous Section  < Day Day Up >  Next Section

Hack 89 Create a Wrapper Class for Your API Calls

figs/expert.gif figs/hack89.gif

Create a Windows DLL to call the API and eliminate need for the console application.

Using the API from a console application [Hack #88] is nice for testing, but for real-world applications, you'll want to use an encapsulated module to handle calls to the API. That way, you can reuse the functionality in multiple applications.

This wrapper class DLL is written in C# and assembled in Visual Studio .NET.


The underlying architecture of the PayPal API is the same for each API method, all of which use four basic classes to complete a call:


Type

This is a generic term for a class that holds information. You fill out the properties in the type and add the type to the request object.


Request

This object is responsible for creating and sending the SOAP package to the API. It hands the type to the API that contains information specific to the call (the TransactionID for example, in the GetTransactionDetail() method).


Response

This object holds the API's response to the call, including whether the call was successful. It also returns a type object, with specifics (transaction details, for example, in the GetTransactionDetail() method).


API service

This object executes the call using the request object as an argument and returns a response object.

8.5.1 Handling the Basics

The API wrapper class makes it easier for you to access the PayPal API, and you can reuse it in multiple applications. The wrapper class has four properties (APIPassword, APIPassword, CertLocation, and APIUrl) set by the class constructor method, as well as some additional methods to simplify security setup and formatting.

  1. Open Visual Studio .NET and go to FileNewProject.

  2. On the New Project screen, select Visual C# Projects and Class Library.

  3. Name your project PayPalAPI and click OK.

  4. Add a PayPal web reference [Hack #88] . Name it PayPalSvc and click Add Reference.

  5. Add a new class file to the project and name it APIWrapper.cs.

  6. Copy the following code into APIWrapper.cs, and save the project when you're done:

using System;

using System.Net;

using System.Security.Cryptography.X509Certificates;

using System.Text;

using PayPalAPI.PayPalSvc;

using System.Data;

using System.Collections;



namespace PayPalAPI 

{

        /// <summary>

        /// Summary description for APIWrapper.

        /// </summary>

        

        public class APIWrapper 

        {

                string _APIUserName="";

                string _APIPassword="";

                string _CertLocation="";

                string _APIUrl="";



                public string APIUserName

                {

                        get{return _APIUserName;}

                }

                public string APIPassword

                {

                        get{return _APIPassword;}

                }

                public string CertLocation

                {

                        get{return _CertLocation;}

                }

                public string APIUrl

                {

                        get{return _APIUrl;}

                }

                PayPalAPIInterfaceService service;

                public APIWrapper(String APIUserName, string APIPassword, 

                        string CertLocation, string APIUrl) 

                {

                        _APIUserName=APIUserName;

                        _APIPassword=APIPassword;

                        _CertLocation=CertLocation;

                        _APIUrl=APIUrl;



                        // Add the CertificatePolicy so we can post to an untrusted

                                site

                        ServicePointManager.CertificatePolicy = new

                                MyCertificateValidation( );



                        service = new PayPalAPIInterfaceService( );

                        service.Url = _APIUrl;



                        // Add the X509 Cert to the service for authentication

                        X509Certificate certificate = 

                                X509Certificate.CreateFromCertFile(_CertLocation);

                        service.ClientCertificates.Add(certificate);

                        SetHeaderCredentials(service);



                }

                void SetHeaderCredentials(PayPalAPIInterfaceService service)

                {

                        CustomSecurityHeaderType securityHeader = 

                                new CustomSecurityHeaderType( );

                        UserIdPasswordType userIdPassword = new UserIdPasswordType( );

                        userIdPassword.Username = _APIUserName;

                        userIdPassword.Password = _APIPassword;

                        //userIdPassword.Subject = subject;

                        securityHeader.Credentials = userIdPassword;

                        securityHeader.MustUnderstand = true;

                        

                        service.RequesterCredentials = securityHeader;



                }

                

                string GetAmountValue(BasicAmountType amount)

                {

                        string sOut="";

                        try

                        {

                                sOut="$"+amount.Value.ToString( );

                                amount.currencyID = CurrencyCodeType.USD;

                        }

                        catch

                        {

                                sOut="--";

                        }

                        return sOut;

                }

                

        }



}

8.5.2 Creating Your Own Certificate Handler

If you have trouble accessing the PayPal API, it might be because your .NET code does not trust the PayPal digital certificate. But you know that you're talking to PayPal, so it's not that important. Adding the following code to your API wrapper overrides .NET's default certificate policy, which is to challenge certificates issued by untrusted certificate authorities:

class MyCertificateValidation : ICertificatePolicy {

  

// Default policy for certificate validation.

public static bool DefaultValidate = false; 



public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest 

request, int problem) {        

  //implement your custom code here

  return true;

  }

}

Eventually, you'll need to implement your own code for this class, but for development purposes, you can simply tell your server to trust every certificate issuer.

--Rob Conery and Dave Nielsen

    Previous Section  < Day Day Up >  Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


         Main Menu
    PayPal Hacks
    Table of Contents
    Copyright
    Credits
    Preface
    Chapter 1. Account Management
    Chapter 2. Making Payments
    Chapter 3. Selling with PayPal
    Chapter 4. Payment Buttons
    Chapter 5. Storefronts and Shopping Carts
    Chapter 6. Managing Subscriptions
    Chapter 7. IPN and PDT
    Chapter 8. The PayPal Web Services API
    Introduction: Hacks #87-100
    8.2 Create a Developer Account
    Hack 87 Set up the Sandbox
    Hack 88 Make Your First API Call
    Hack 89 Create a Wrapper Class for Your API Calls
    Hack 90 Use the PayPal API Wrapper Class
    Hack 91 Refund Payments with the API
    Hack 92 Handle Transaction Errors within the API Wrapper
    Hack 93 Retrieve Transaction Details with the API
    Hack 94 Search for PayPal Transactions
    Hack 95 Hack the API Wrapper
    Hack 96 Issue Payments en Masse with the Mass Pay API
    Hack 97 Pay Affiliates and Suppliers on a Schedule
    Hack 98 Search eBay for Listings that Accept PayPal
    Hack 99 Test IPN and PDT in the Sandbox
    Hack 100 Go Live
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele