public ResponseHolder ApplyPaymentToMultipleInvoices(String accountId, String[] invoiceIds, String paymentMethodId, Decimal amount, String type)
{
//create payment in draft
Payment payment = new Payment();
payment.Amount = amount;
payment.AmountSpecified = true;
payment.EffectiveDate = DateTime.Now;
payment.EffectiveDateSpecified = true;
payment.AccountId = accountId;
payment.PaymentMethodId = paymentMethodId;
payment.Type = type;
payment.Status = "Draft";
List<ResponseHolder> payCreateRes = zs.Create(new List<zObject> { payment }, false);
foreach (ResponseHolder rh in payCreateRes)
{
if (!rh.Success)
{
return rh;
}
}
List<zObject> ipList = new List<zObject>();
//create invoice payment objects for the invoice amount
foreach(String invId in invoiceIds)
{
ResponseHolder qRes = zs.Query("SELECT id, Amount FROM Invoice WHERE id = '" + invId + "'");
Invoice inv = (Invoice)qRes.Objects[0];
InvoicePayment ip = new InvoicePayment();
ip.AmountSpecified = true;
ip.Amount = inv.Amount;
ip.InvoiceId = inv.Id;
ip.PaymentId = payCreateRes[0].Id;
ipList.Add(ip);
}
List<ResponseHolder> ipCreateRes = zs.Create(ipList, false);
foreach (ResponseHolder rh in ipCreateRes)
{
if (!rh.Success)
{
return rh;
}
}
//update the original payment to be Proccessed
Payment updatePayment = new Payment();
updatePayment.Id = payCreateRes[0].Id;
updatePayment.Status = "Processed";
List<ResponseHolder> updateRes = zs.Update(new List<zObject>{ updatePayment });
return updateRes[0];
}