< Day Day Up > |
Hack 97 Pay Affiliates and Suppliers on a Schedule
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 CodeStart 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 HackTo pay affiliates and suppliers on a schedule, implement the code by following these steps:
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 |
< Day Day Up > |