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


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





        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



        // Create the MassPay Request 

        MassPayRequestType masspayRequest = new MassPayRequestType( );

        //allocate the array for the ItemTypes

        masspayRequest.MassPayRequestItemDetails = new


        // 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] =





        MassPayReq request = new MassPayReq( );

        request.MassPayRequest = masspayRequest;


        MassPayResponseType response = service.MassPay(request);

        string sReturn=CheckErrors(response);




        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.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( );




        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