Example #1
0
        public async Task ShouldBuildValidRequest()
        {
            var responseFixture = "fixtures/client/create_a_redirect_flow_response.json";

            mockHttp.EnqueueResponse(201, responseFixture);
            var prefilledCustomer = new RedirectFlowCreateRequest.RedirectFlowPrefilledCustomer()
            {
                Email      = "*****@*****.**",
                GivenName  = "Frank",
                FamilyName = "Osborne"
            };

            var redirectFlowRequest = new RedirectFlowCreateRequest()
            {
                Description        = "Wine boxes",
                SessionToken       = "SESS_wSs0uGYMISxzqOBq",
                SuccessRedirectUrl = "https://example.com/pay/confirm",
                PrefilledCustomer  = prefilledCustomer
            };

            var redirectFlowResponse = await client.RedirectFlows.CreateAsync(redirectFlowRequest);

            TestHelpers.AssertResponseCanSerializeBackToFixture(redirectFlowResponse, responseFixture);

            var redirectFlow = redirectFlowResponse.RedirectFlow;

            Assert.AreEqual("http://pay.gocardless.dev/flow/RE123", redirectFlow.RedirectUrl);
            mockHttp.AssertRequestMade("POST", "/redirect_flows");
        }
Example #2
0
        public RedirectResponseDto CreateRedirectRequest(Umbraco.Core.Logging.ILogger logger, CustomerDto customer, string description, string sessionToken, string successUrl)
        {
            var request = new RedirectFlowCreateRequest()
            {
                Description        = description,
                SessionToken       = sessionToken,
                SuccessRedirectUrl = successUrl,
                // Optionally, prefill customer details on the payment page
                PrefilledCustomer = new RedirectFlowCreateRequest.RedirectFlowPrefilledCustomer()
                {
                    GivenName    = customer.GivenName,
                    FamilyName   = customer.FamilyName,
                    Email        = customer.Email,
                    AddressLine1 = customer.AddressLine1,
                    City         = customer.City,
                    PostalCode   = customer.PostalCode
                }
            };

            return(TryCreateRedirectRequest(logger, customer, request));
        }
Example #3
0
        public RedirectResponseDto TryCreateRedirectRequest(Umbraco.Core.Logging.ILogger logger, CustomerDto customer, RedirectFlowCreateRequest request)
        {
            try
            {
                RedirectFlowResponse redirectFlowResponse = _client.RedirectFlows.CreateAsync(request).Result;
                var redirectFlow = redirectFlowResponse.RedirectFlow;
                return(new RedirectResponseDto()
                {
                    Id = redirectFlow.Id,
                    RedirectUrl = redirectFlow.RedirectUrl
                });
            }
            catch (Exception ex)
            {
                string error = "GoCardless Error setting up mandate.";
                logger.Error(typeof(GoCardlessProvider), string.Format(
                                 $"Unable to CreateRedirectRequest for memberEmail: {customer.Email}, exception: {0}",
                                 ex), ex);

                var exception = ex.InnerException;
                if (exception != null)
                {
                    var apiException = exception as ApiException;

                    if (apiException != null)
                    {
                        var errors = JsonConvert.SerializeObject(apiException?.Errors);
                        error = $"GoCardless Error setting up mandate - {errors}";
                        logger.Warn(typeof(GoCardlessProvider), errors);
                        logger.Error(typeof(GoCardlessProvider), JsonConvert.SerializeObject(apiException), apiException);
                    }
                    else
                    {
                        logger.Error(typeof(GoCardlessProvider), $"GoCardless Error: {exception.ToString()}", exception);
                    }
                }

                if (request.PrefilledCustomer != null)
                {
                    request.PrefilledCustomer = null;
                    return(TryCreateRedirectRequest(logger, customer, request));
                }
                else
                {
                    return(new RedirectResponseDto()
                    {
                        Error = error
                    });
                }
            }
        }