Ejemplo n.º 1
0
 private void IsOnlyFailed(string status)
 {
     Assert.IsFalse(LaybuyOrderStatus.IsPendingStatus(status));
     Assert.IsTrue(LaybuyOrderStatus.IsFailed(status));
     Assert.IsFalse(LaybuyOrderStatus.IsApproved(status));
 }
Ejemplo n.º 2
0
        private static async Task TestLaybyOrderLifecycle(ILaybuyClient client, CreateOrderRequest createRequest)
        {
            System.Diagnostics.Trace.WriteLine("Merchant Ref: " + createRequest.MerchantReference);

            var createOrderResponse = await client.Create(createRequest).ConfigureAwait(false);

            Assert.IsNotNull(createOrderResponse);
            Assert.AreEqual(createOrderResponse.Result, LaybuyStatus.Success);
            Assert.IsFalse(String.IsNullOrWhiteSpace(createOrderResponse.Token));
            Assert.IsTrue(String.IsNullOrWhiteSpace(createOrderResponse.Error));
            Assert.IsNotNull(createOrderResponse.PaymentUrl);

            //Poll until 'order' reaches a final status
            var done          = false;
            var started       = DateTime.Now;
            var statusRequest = new OrderStatusRequest()
            {
                MerchantReference = createRequest.MerchantReference
            };
            OrderStatusResponse statusResponse;

            while (!done)
            {
                await Task.Delay(5000);                 //Laybuy say poll every 5-10 seconds

                statusResponse = await client.GetOrderStatus(statusRequest).ConfigureAwait(false);

                done = !LaybuyOrderStatus.IsPendingStatus(statusResponse.Status);

                if (DateTime.Now.Subtract(started).TotalMinutes > 5)
                {
                    throw new TimeoutException("Order not accepted after 5 minutes.");
                }
                if (LaybuyOrderStatus.IsFailed(statusResponse.Status))
                {
                    throw new LaybuyApiException(statusResponse.Status + " - " + statusResponse.Error);
                }
            }

            var orderResponse = await client.GetOrder(new OrderRequest()
            {
                MerchantReference = statusRequest.MerchantReference
            }).ConfigureAwait(false);

            Assert.AreEqual(orderResponse.Result, LaybuyStatus.Success);
            System.Diagnostics.Trace.WriteLine("Order Id: " + orderResponse.OrderId);
            System.Diagnostics.Trace.WriteLine("Payment token: " + orderResponse.Token);
            Assert.IsNotNull(orderResponse.Refunds);
            Assert.IsFalse(String.IsNullOrEmpty(orderResponse.Currency));
            Assert.IsNotNull(orderResponse.Processed);
            Assert.IsTrue(orderResponse.OrderId > 0);
            Assert.AreEqual(orderResponse.MerchantReference, createRequest.MerchantReference);
            //TODO: Laybuy doesn't actually return this here?
            //Assert.AreEqual(orderResponse.Token, createOrderResponse.Token);

            //Refund the order
            var refundRequest = new RefundRequest()
            {
                RefundMerchantReference = System.Guid.NewGuid().ToString(), OrderId = orderResponse.OrderId, Amount = orderResponse.Amount, Note = "Test refund"
            };

            System.Diagnostics.Trace.WriteLine("Refund Ref: " + refundRequest.RefundMerchantReference);
            var refundResponse = await client.Refund(refundRequest).ConfigureAwait(false);

            Assert.IsNotNull(refundResponse);
            Assert.AreEqual(refundResponse.Result, LaybuyStatus.Success);
            Assert.IsTrue(refundResponse.RefundId > 0);
            Assert.AreEqual(createRequest.MerchantReference, refundResponse.MerchantReference);

            //Can get the order by order id
            var orderRequest = new OrderRequest()
            {
                OrderId = orderResponse.OrderId
            };

            orderResponse = await client.GetOrder(orderRequest).ConfigureAwait(false);

            Assert.AreEqual(orderResponse.Result, LaybuyStatus.Success);
            Assert.AreEqual(1, orderResponse.Refunds.Count());
        }