O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
PayPal Hacks
By Shannon Sofield, Dave Nielsen, Dave Burchell
September 2004
More Info

HACK
#97
Pay Affiliates and Suppliers on a Schedule
Automate Mass Pay API calls to schedule mass payments at regular intervals
The Code
[Discuss (0) | Link to this hack]

The Code

Start with the code from 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;
}


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.