Пример #1
0
        public async Task Enrol_Handles_SuccessfulResponse()
        {
            #region Setup Mocked Client

            var responseHistory = new Dictionary <string, CreateOrderResponse>();

            var handler = new MockHttpHandler();

            handler.AddMock
            (
                HttpMethod.Post, "https://api-ci.partpay.co.nz/pos/terminal/enrol",
                (httpRequest) =>
            {
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent
                              (
                        System.Text.Json.JsonSerializer.Serialize
                        (
                            new
                    {
                        client_id = "3D888947-1CA3-428B-A428-F53532FB8593",
                        client_secret = "6FFD61D3-76CA-4059-8AE2-C246B52FF452"
                    },
                            _SerialiserOptions
                        ),
                        System.Text.UTF8Encoding.UTF8,
                        "application/json"
                              )
                }));
            }
            );

            var httpClient = new HttpClient(handler);

            #endregion

            using (var client = CreateTestClient(httpClient))
            {
                var request = new EnrolRequest()
                {
                    ActivationCode = "ABC123",
                    Secret         = "zipme!",
                    Terminal       = "2531"
                };

                var result = await client.EnrolAsync(request);

                Assert.IsNotNull(result);
                Assert.AreEqual("3D888947-1CA3-428B-A428-F53532FB8593", result.ClientId);
                Assert.AreEqual("6FFD61D3-76CA-4059-8AE2-C246B52FF452", result.ClientSecret);
            }
        }
Пример #2
0
        public async Task CreateOrder_ThrowsOnRedirectResponseWithNoLocation()
        {
            #region Setup Mocked Client

            var responseHistory = new Dictionary <string, CreateOrderResponse>();

            var handler = new MockHttpHandler();

            handler.AddMock
            (
                HttpMethod.Post, "https://api-ci.partpay.co.nz/pos/order",
                async(httpRequest) =>
            {
                HttpStatusCode responseCode = HttpStatusCode.Created;
                var order = System.Text.Json.JsonSerializer.Deserialize <ZipOrder>(await httpRequest.Content.ReadAsStringAsync(), _SerialiserOptions);

                var key = order.MerchantReference;
                if (!responseHistory.TryGetValue(key, out var responseBody))
                {
                    responseBody = responseHistory[key] = new CreateOrderResponse()
                    {
                        OrderId     = System.Guid.NewGuid().ToString(),
                        OrderExpiry = DateTime.UtcNow.AddHours(1)
                    };
                }
                else
                {
                    responseCode = HttpStatusCode.Found;
                }

                var retVal = new HttpResponseMessage(responseCode)
                {
                    Content = responseCode == HttpStatusCode.Found ? null : new StringContent(System.Text.Json.JsonSerializer.Serialize(responseBody, _SerialiserOptions), System.Text.UTF8Encoding.UTF8, "application/json")
                };

                return(retVal);
            }
            );

            var httpClient = new HttpClient(handler);

            #endregion

            using (var client = CreateTestClient(httpClient))
            {
                var request = new CreateOrderRequest()
                {
                    TerminalId = "2531",
                    EnableUniqueMerchantReferenceCheck = true,
                    Order = new ZipOrder()
                    {
                        Amount = 50M,
                        CustomerApprovalCode = "206931",
                        MerchantReference    = System.Guid.NewGuid().ToString(),
                        Operator             = "Test",
                        PaymentFlow          = ZipPaymentFlow.Payment,
                        Items = new System.Collections.Generic.List <ZipOrderItem>()
                        {
                            new ZipOrderItem()
                            {
                                Name        = "Test Item",
                                Description = "0110A Blue 12",
                                Price       = 50M,
                                Quantity    = 1,
                                Sku         = "123"
                            }
                        }
                    }
                };

                var result1 = await client.CreateOrderAsync(request);

                Assert.IsNotNull(result1);
                Assert.IsFalse(String.IsNullOrWhiteSpace(result1.OrderId));
                Assert.IsNotNull(result1.OrderExpiry);

                await Assert.ThrowsExceptionAsync <ZipApiException>
                (
                    async() => { var result2 = await client.CreateOrderAsync(request); }
                );
            }
        }