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 90 Use the PayPal API Wrapper Class

figs/expert.gif figs/hack90.gif

Create a simple transaction-lookup form and make an API call with the API wrapper class.

Now that you've created a wrapper class for your API calls [Hack #89], it's time to put it to use. This hack adds one GetTransactionDetail function to your wrapper class. It then creates a user interface for the wrapper class from which you can look up the corresponding transaction details.

The first thing to do is log into Developer Central, open up your Personal Sandbox account [Hack #87], and send some money to your Sandbox merchant account.

Sending and receiving money works identically in the Sandbox and on the live PayPal site, except that the money in the Sandbox is not real and you will not receive any email messages from PayPal.


Once you have sent the money, log out of your Personal account and log into your Sandbox merchant account. You should see the money you just sent from your Personal account. Click Details next to the payment and make note of the transaction ID; you will need it later in this hack.

To use the API wrapper class to look up details of a transaction, start by adding the following GetTransactionDetail code your wrapper class by appending it to the existing code in the class:

public string GetTransactionDetail(string transactionID, string delimiter)

{

        string sReturn="";

        

        GetTransactionDetailsRequestType detailRequest=

                new GetTransactionDetailsRequestType( );

        detailRequest.TransactionID=transactionID;

        GetTransactionDetailsReq request=new GetTransactionDetailsReq( );

        request.GetTransactionDetailsRequest=detailRequest;

        GetTransactionDetailsResponseType 

                response=service.GetTransactionDetails(request);

        

        sReturn=response.Ack.ToString( )+"\n";

        

        //build out the response

        StringBuilder sb=new StringBuilder( );

        sb.Append("************** Payment Information ******************"+delimiter);

        //payment info

        PaymentInfoType payment=response.PaymentTransactionDetails.PaymentInfo;

        sb.Append("ReceiptID: "+payment.ReceiptID+delimiter);

        sb.Append("TransactionID: "+payment.TransactionID+delimiter);

        sb.Append("PaymentDate: "+payment.PaymentDate+delimiter);                        

        sb.Append("GrossAmount: "+GetAmountValue(payment.GrossAmount)+delimiter);

        sb.Append("SettleAmount: "+GetAmountValue(payment.SettleAmount)+delimiter);

        sb.Append("FeeAmount: "+GetAmountValue(payment.FeeAmount)+delimiter);

        sb.Append("TaxAmount: "+GetAmountValue(payment.TaxAmount)+delimiter);

        sb.Append("PaymentStatus: "+payment.PaymentStatus+delimiter);

        sb.Append("PaymentType: "+payment.PaymentType+delimiter);

        sb.Append("TransactionType: "+payment.TransactionType+delimiter);

        sb.Append(delimiter);

        //sReturn+=response.PaymentTransactionDetails.PaymentInfo.ToString( );

        sb.Append("************** Buyer Information ******************"+delimiter);

        

        //receiver info

        ReceiverInfoType receiver=response.PaymentTransactionDetails.ReceiverInfo;

        sb.Append("Business: "+receiver.Business+delimiter);

        sb.Append("Receiver: "+receiver.Receiver+delimiter);

        sb.Append("ReceiverID: "+receiver.ReceiverID+delimiter);

        

        //item info

        PaymentItemInfoType item=

                response.PaymentTransactionDetails.PaymentItemInfo;

        //PaymentItemType itm=new PaymentItemType( );

        sb.Append(delimiter);

        int i=1;

        sb.Append("************** Item Information ******************"+delimiter);

        sb.Append("Custom: "+item.Custom+delimiter);

        sb.Append("InvoiceID: "+item.InvoiceID+delimiter);

        sb.Append("Memo: "+item.Memo+delimiter);

        sb.Append("SalesTax: "+item.SalesTax+delimiter);

        if(item.PaymentItem!=null)

        {

                foreach(PaymentItemType itm in item.PaymentItem)

                {

                        //itm=(PaymentItemType)PaymentItem[i];

                        sb.Append(delimiter);

                        sb.Append("Item "+i.ToString( )+":"+delimiter);

                        sb.Append("Name: "+itm.Name+delimiter);

                        sb.Append("Number: "+itm.Number+delimiter);

                        sb.Append("Options: "+itm.Options+delimiter);

                        sb.Append("Quantity: "+itm.Quantity+delimiter);

                        sb.Append("SalesTax: "+itm.SalesTax+delimiter);

                        sb.Append(delimiter);

                        i++;

                }

        }

        

        sReturn=sb.ToString( );

        return sReturn;

        }

}

Next, create a Windows form in Visual Studio .NET that uses the API wrapper class to call the GetTransactionDetails API function:

  1. With the PayPal API solution opened, right-click the solution and select AddNew Project.

  2. Select Visual Studio C#/Windows Application.

  3. Name your project PayPalTestApp and click OK.

  4. Right-click the References entry in the PayPalTestApp project and select Add Reference.

  5. On the Add Reference screen, select the Project tab and select the PayPalAPI project. Click Select, and then click OK to add a reference to the PayPal API wrapper.

Check out Mastering Visual Studio .NET by Ian Griffiths, Jon Flanders, and Chris Sells (O'Reilly) for help with creating forms in .NET.


When that's finished, create a .NET form (Form1.cs) with text boxes and code to look up the details of a PayPal transaction. The form accepts the API username (txtUserName), password (txtPassword), and transaction ID (txtTransactionID) as inputs and submits them to PayPal via the click of a button (cmdDetails). Add a label control (lblResponse) to output the results to. Your form should look something like the one in Figure 8-6.

Figure 8-6. Finding transaction details quickly at PayPal
figs/pph_0806.gif


Double-click the cmdDetails button and add the following code to its click event:

private void cmdDetails_Click(object sender, System.EventArgs e) {

  string username=txtUserName.Text;

  string password=txtPassword.Text;

  string transactionID=txtTransactionID.Text;

  string certPath="C:\\certificate.cer";

  string url = "https://api.sandbox.paypal.com/2.0/";



  lblResponse.Text="Contacting PayPal....";

  PayPalAPI.APIWrapper api=new

                    PayPalAPI.APIWrapper(username,password,certPath,url);

  lblResponse.Text=api.GetTransactionDetail(transactionID,"\n");

}

Set PayPalTestApp, fill out the text boxes with your API username and password, as well as the TransactionID copied from the preceding transaction, and click the Get Details button. The information supplied on the form will be passed to the wrapper class, which will prepare the request and then call GetTransactionDetail. Assuming it's successful, the transaction details will appear in the label control, as shown in Figure 8-7.

Figure 8-7. The results of your transaction details request
figs/pph_0807.gif


Now that you have a reusable class to access the API, you can easily add code to your projects to process refunds [Hack #91], retrieve transaction details [Hack #93], and search your transaction history [Hack #94] .

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