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 97 Pay Affiliates and Suppliers on a Schedule

figs/expert.gif figs/hack97.gif

Automate Mass Pay API calls to schedule mass payments at regular intervals.

When you have a lot of people to pay, setting up and executing online payments one at a time can quickly get tedious. Likewise, repeatedly setting up Mass Pay requests can get tedious if you have to do it every month or every week. Here is a great real-world example that shows you how to give away your money faster than you thought possible.

8.13.1 The Code

Start with the code from [Hack #96] and extend it with two new classes: MassPayee and MassPayeeTable (which supplements the ArrayList object):

//a class which holds the payee info

public class MassPayee{

        public string Note="";

        public string Email="";

        public string EmailSubject="";

        public string ReferenceID="";

        public double Amount=0;

}



//a class which holds the MassPayees

public class MassPayTable:ArrayList{



        public void AddPayee(MassPayee payee){

                //the API will only allow 250 payees

                if(Payess.Count=250){

                        throw new Execption("A maximum of 250 payees are allowed");

                }else{

                        Payees.Add(payee);

                }

        }

        public void ClearPayees( ){

                Payees.Clear( );

        }

        public int Count{

                get{return Payees.Count;}

        }

}

Here's the code for the RunMassPay routine:

public string RunMassPay(MassPayTable PayeeTable){



        // Build the Security Header

        this.SetHeaderCredentials(service);

                                

        // Create the MassPay Request 

        MassPayRequestType masspayRequest = new MassPayRequestType( );

        //allocate the array for the ItemTypes

        masspayRequest.MassPayRequestItemDetails = new

                        MassPayRequestItemType[PayeeTable.Count];



        // create the Amount

        BasicAmountType amount;

        

        // Create the MassPay Request Item

        MassPayRequestItemType masspayRequestItem;;

        

        //our indexer

        int counter=0;

        

    //loop through the MassPayee List and add the

    //information to the PayPal API objects.

        for(int i=0;i<PayeeTable.Count;i++){

                masspayRequestItem= new MassPayRequestItemType( );

                amount= new BasicAmountType( );

                amount.currencyID = CurrencyCodeType.USD;

                MassPayee payee=(MassPayee)PayeeTable[i];

        

                amount.Value = payee.Amount.ToString( );

                masspayRequestItem.Amount = amount;

                masspayRequestItem.ReceiverEmail = payee.Email;

                masspayRequestItem.UniqueID = payee.ReferenceID;

                masspayRequestItem.Note = payee.Note;

                masspayRequest.EmailSubject = payee.EmailSubject;

                

                // add the previously created MassPayRequestItemType object 

                        to this array

                masspayRequest.MassPayRequestItemDetails[counter] =

                        masspayRequestItem;

                

                counter++;

        }



        MassPayReq request = new MassPayReq( );

        request.MassPayRequest = masspayRequest;

        

        MassPayResponseType response = service.MassPay(request);

        string sReturn=CheckErrors(response);

        if(sReturn==""){

                sReturn=response.Ack;

        }

        return sReturn;

}

To use this routine, gather the payee information from your site database and execute the call:

public string SendMassPay( ){

        

        //get the payees from the database

        string sql="MyPayeeSQL";

        SqlConnection conn=new SqlConnection("MyConnectionString");

        SqlCommand cmd=new SqlCommand(sql,conn);

        SqlDataReader rdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);

        APIWrapper api=new

                APIWrapper("MyUserName","MyPassword","MyCertLocation","APIUrl");

        

        APIWrapper.MassPayeeTable Payees=new APIWrapper.MassPayeeTable( );

        APIWrapper.MassPayee payee;

        while(rdr.Read( )){

                payee=new APIWrapper.MassPayee( );

                payee.Note=rdr["Note"].ToString( );

                payee.Email=rdr["Email"].ToString( );

                payee.EmailSubject=rdr["EmailSubject"].ToString( );

                payee.ReferenceID=rdr["ReferenceID"].ToString( );

                payee.Amount=(double)rdr["Amount"];

                Payees.Add(payee);

        }

        string result=api.RunMassPay(Payees);

        rdr.Close( );

        conn.Close( );

        return result;

}

8.13.2 Running The Hack

To pay affiliates and suppliers on a schedule, implement the code by following these steps:

  1. Create a new project: select Visual C# Projects and then Console Application.

  2. Add the MassPayee and MassPayTable classes to the Class1.cs file.

  3. Add the RunMassPay routine to the Class1.cs file.

  4. Add the SendMassPay routine to the Class1.cs file.

  5. Replace the MyPayeeSQL value with the name of a procedure stored in your database that you've created. The stored procedure should return the following fields: Email, EmailSubject, Amount, Note, and ReferenceID. Make sure one of the email addresses is your Sandbox Personal account so that you can confirm you sent the money.

  6. Replace the MyConnectionString with your own database connection.

  7. Compile and run the console application.

The response from PayPal will either be Success or a list of errors. See [Hack #92] for more information on errors and return codes.

Confirm that your payments have been sent and received by logging into your Sandbox Personal account.

Souvik Das, 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