Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit 093c573

Browse files
Luke KirbyLuke Kirby
authored andcommitted
Changes to BatchPayment to avoid timezone Issue
The payment date of the batch payment should be passed as a string in the format yyyy-MM-dd according to the Xero API docs page however as it was being parsed as a date, it was subject to the timezone difference between the API user and Xero, so if you parsed a date object without a timepart, you could end up with yesterday's date being set for the payment date. See https://developer.xero.com/documentation/api/batch-payments#PUT
1 parent c7da77c commit 093c573

4 files changed

Lines changed: 119 additions & 3 deletions

File tree

CoreTests/CoreTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
<Compile Include="Integration\BankTransfers\BankTransfersTest.cs" />
8787
<Compile Include="Integration\BankTransfers\Create.cs" />
8888
<Compile Include="Integration\BankTransfers\Find.cs" />
89+
<Compile Include="Integration\BatchPayment\Create.cs" />
90+
<Compile Include="Integration\BatchPayment\BatchPaymentsTest.cs" />
8991
<Compile Include="Integration\ContactGroups\Add_Contact.cs" />
9092
<Compile Include="Integration\ContactGroups\ContactGroupsTest.cs" />
9193
<Compile Include="Integration\ContactGroups\Create.cs" />
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Xero.Api.Core.Model;
4+
using Xero.Api.Core.Model.Status;
5+
using Xero.Api.Core.Model.Types;
6+
7+
namespace CoreTests.Integration.BatchPayments
8+
{
9+
public abstract class BatchPaymentsTest : ApiWrapperTest
10+
{
11+
protected BatchPayment Given_a_batch_payment(decimal invoiceAmount, DateTime date, decimal amount, bool isReconciled = false)
12+
{
13+
var batchPayment = CreateBatchPayment(invoiceAmount, date, amount, isReconciled);
14+
15+
return Api.BatchPayments.Create(batchPayment);
16+
}
17+
18+
protected BatchPayment CreateBatchPayment(decimal invoiceAmount, DateTime date, decimal amount, bool isReconciled = false)
19+
{
20+
var invoice = Given_an_invoice(invoiceAmount, Account.Code);
21+
var bankCode = BankAccount.Id;
22+
23+
var payment = new BatchPayment
24+
{
25+
Account = new Account { Id = bankCode },
26+
Date = date,
27+
Payments = new List<BatchPaymentPayment> { new BatchPaymentPayment {
28+
Amount = amount,
29+
Invoice = new Invoice { Id = invoice.Id},
30+
BankAccountNumber = BankAccount.BankAccountNumber,
31+
}}
32+
};
33+
34+
if (isReconciled)
35+
{
36+
payment.IsReconciled = true;
37+
}
38+
39+
return payment;
40+
}
41+
42+
private Invoice Given_an_invoice(decimal amount = 100m, string accountCode = "100")
43+
{
44+
return Api.Create(new Invoice
45+
{
46+
Contact = new Contact { Name = "Richard" },
47+
Number = Random.GetRandomString(10),
48+
Type = InvoiceType.AccountsPayable,
49+
Date = DateTime.UtcNow,
50+
DueDate = DateTime.UtcNow.AddDays(90),
51+
LineAmountTypes = LineAmountType.Inclusive,
52+
Status = InvoiceStatus.Authorised,
53+
LineItems = new List<LineItem>
54+
{
55+
new LineItem
56+
{
57+
AccountCode = accountCode,
58+
Description = "Good value item",
59+
LineAmount = amount
60+
}
61+
}
62+
});
63+
}
64+
65+
}
66+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using Xero.Api.Core.Model;
6+
7+
namespace CoreTests.Integration.BatchPayments
8+
{
9+
[TestFixture]
10+
public class Create : BatchPaymentsTest
11+
{
12+
[TestFixtureSetUp]
13+
public void CreateBatchPaymentsSetUp()
14+
{
15+
SetUp();
16+
}
17+
18+
[Test]
19+
public void create_simple_batch_payment()
20+
{
21+
var date = DateTime.UtcNow;
22+
const decimal expectedTotal = 32.6m;
23+
const decimal invoiceAmount = 100;
24+
25+
var batchPayment = Given_a_batch_payment(invoiceAmount, date, expectedTotal);
26+
27+
Assert.IsNotNull(batchPayment);
28+
29+
Assert.AreEqual(expectedTotal, batchPayment.Total);
30+
Assert.AreEqual(date.Date, batchPayment.Date);
31+
32+
33+
}
34+
35+
}
36+
}

Xero.Api/Core/Model/BatchPayment.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Xero.Api.Common;
55
using Xero.Api.Core.Model.Status;
66
using Xero.Api.Core.Model.Types;
7+
using Xero.Api.Infrastructure.ThirdParty.ServiceStack.Text.Common;
78

89
namespace Xero.Api.Core.Model
910
{
@@ -37,10 +38,21 @@ public class BatchPayment : HasUpdatedDate, IHasId
3738
[DataMember(Name = "Narrative", EmitDefaultValue = false)]
3839
public string Narrative { get; set; }
3940

40-
[DataMember(Name = "Date", EmitDefaultValue = false)]
41-
public DateTime? Date { get; set; }
41+
public DateTime? Date { get; set; }
4242

43-
[DataMember(Name = "Payments")]
43+
[DataMember(Name = "Date", EmitDefaultValue = false)]
44+
public string DateString {
45+
get
46+
{
47+
return Date.Value.ToString("yyyy-MM-dd");
48+
}
49+
set
50+
{
51+
Date = DateTimeSerializer.ParseDateTimeOffset(value).UtcDateTime;
52+
}
53+
}
54+
55+
[DataMember(Name = "Payments")]
4456
public List<BatchPaymentPayment> Payments { get; set; }
4557

4658
[DataMember(Name = "TotalAmount", EmitDefaultValue = false)]

0 commit comments

Comments
 (0)