Hack 90 Use the PayPal API Wrapper Class

 < Day Day Up > 

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 Add New Project.

  2. Name your project PayPalTestApp and click OK.

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

  4. 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

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

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

 < Day Day Up > 

PayPal Hacks
PayPal Hacks
ISBN: 0596007515
EAN: 2147483647
Year: 2004
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net