Ejemplo n.º 1
0
        public async Task <bool> CreateSubscriptionAsync(string customerId, string customerName, string iban, PaymentMethod paymentMethod, string description, DateTime startDate, decimal amount, string interval, int?times, string webhookUrl)
        {
            var result = false;

            var mandateClient = GetMandateClient();

            // Create "mandate" via Mollie to be able to create a customer subscription.
            // Mandates allow you to charge a customer’s credit card or bank account recurrently.
            var mandateRequest = new MandateRequest
            {
                ConsumerName    = customerName,
                ConsumerAccount = iban,
                Method          = paymentMethod
            };

            MandateResponse mandateResponse = await mandateClient.CreateMandateAsync(customerId, mandateRequest);

            if (mandateResponse.Status == MandateStatus.Valid)
            {
                // Create your customer payment record here

                var mollieSettings = _configuration.GetSection("Mollie");
                var currency       = mollieSettings.GetValue <string>("DefaultCurrency");

                var subscriptionRequest = new SubscriptionRequest
                {
                    Amount = new Amount
                    {
                        Currency = currency,
                        Value    = amount.ToString()
                    },
                    Description = description,
                    StartDate   = startDate,
                    Method      = paymentMethod,
                    Interval    = interval,  // Interval to wait between charges like 1 month(s) or 14 days
                    Times       = times,     // Total number of charges for the subscription to complete. Leave empty for an on-going subscription
                    WebhookUrl  = webhookUrl // The url that Mollie calls with the payment id when the payment status changes.
                };

                var subscriptionClient = GetSubscriptionClient();

                SubscriptionResponse subscriptionResponse = await subscriptionClient.CreateSubscriptionAsync(customerId, subscriptionRequest);

                if (subscriptionResponse.Status == SubscriptionStatus.Active)
                {
                    // Update your customer payment record here with new subscription Id

                    result = true;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task ShouldCreateAMandate()
        {
            var responseFixture = "fixtures/client/create_a_mandate_response.json";

            mockHttp.EnqueueResponse(201, responseFixture,
                                     resp => resp.Headers.Location = new Uri("/mandates/MD000126", UriKind.Relative));
            MandateResponse mandateResponse = await client.Mandates.CreateAsync(TestHelpers.CreateMandateCreateRequest());

            Assert.AreEqual(new DateTimeOffset(2017, 06, 19, 17, 01, 06, TimeSpan.FromHours(3)),
                            mandateResponse.Mandate.CreatedAt, "DateTimeOffset not correct");

            TestHelpers.AssertResponseCanSerializeBackToFixture(mandateResponse, responseFixture);
            var mandate = mandateResponse.Mandate;

            mockHttp.AssertRequestMade("POST", "/mandates", "fixtures/client/create_a_mandate_request.json");
        }
Ejemplo n.º 3
0
        private async Task <MandateResponse> GetFirstValidMandate()
        {
            ListResponse <CustomerResponse> customers = await fixture.CustomerClient.GetCustomerListAsync();

            foreach (CustomerResponse customer in customers?.Data)
            {
                ListResponse <MandateResponse> customerMandates = await fixture.MandateClient.GetMandateListAsync(customer.Id);

                MandateResponse firstValidMandate = customerMandates?.Data.FirstOrDefault(x => x.Status == MandateStatus.Valid);

                if (firstValidMandate != null)
                {
                    return(firstValidMandate);
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        public void CanCreatePaymentWithMandate()
        {
            // If: We create a payment with a mandate id
            MandateResponse validMandate   = this.GetFirstValidMandate();
            PaymentRequest  paymentRequest = new PaymentRequest()
            {
                Amount        = 100,
                Description   = "Description",
                RedirectUrl   = this.DefaultRedirectUrl,
                RecurringType = RecurringType.Recurring,
                CustomerId    = validMandate.CustomerId,
                MandateId     = validMandate.Id
            };

            // When: We send the payment request to Mollie
            PaymentResponse result = this._paymentClient.CreatePaymentAsync(paymentRequest).Result;

            // Then: Make sure we get the mandate id back in the details
            Assert.AreEqual(validMandate.Id, result.MandateId);
        }
Ejemplo n.º 5
0
        public async Task CanCreateMandate()
        {
            // We can only test this if there are customers
            ListResponse <CustomerResponse> customers = await fixture.CustomerClient.GetCustomerListAsync();

            if (customers.TotalCount > 0)
            {
                // If: We create a new mandate request
                MandateRequest mandateRequest = new MandateRequest()
                {
                    ConsumerAccount = "NL26ABNA0516682814",
                    ConsumerName    = "John Doe"
                };

                // When: We send the mandate request
                MandateResponse mandateResponse = await fixture.MandateClient.CreateMandateAsync(customers.Data.First().Id, mandateRequest);

                // Then: Make sure we created a new mandate
                Assert.Equal(mandateRequest.ConsumerAccount, mandateResponse.Details.ConsumerAccount);
                Assert.Equal(mandateRequest.ConsumerName, mandateResponse.Details.ConsumerName);
            }
        }
Ejemplo n.º 6
0
        public async Task CanCreateRecurringPaymentAndRetrieveIt()
        {
            // If: we create a new recurring payment
            MandateResponse mandate = await this.GetFirstValidMandate();

            PaymentRequest paymentRequest = new PaymentRequest()
            {
                Amount        = 100,
                Description   = "Description",
                RedirectUrl   = fixture.DefaultRedirectUrl,
                RecurringType = RecurringType.First,
                CustomerId    = mandate.CustomerId
            };

            // When: We send the payment request to Mollie and attempt to retrieve it
            PaymentResponse paymentResponse = await fixture.PaymentClient.CreatePaymentAsync(paymentRequest);

            PaymentResponse result = await fixture.PaymentClient.GetPaymentAsync(paymentResponse.Id);

            // Then: Make sure the recurringtype parameter is entered
            Assert.Equal(RecurringType.First, result.RecurringType);
        }
Ejemplo n.º 7
0
        private MandateResponse GetFirstValidMandate()
        {
            ListResponse <CustomerResponse> customers = this._customerClient.GetCustomerListAsync().Result;

            if (!customers.Data.Any())
            {
                Assert.Inconclusive("No customers found. Unable to test recurring payment tests");
            }

            foreach (CustomerResponse customer in customers.Data)
            {
                ListResponse <MandateResponse> customerMandates = this._mandateClient.GetMandateListAsync(customer.Id).Result;
                MandateResponse firstValidMandate = customerMandates.Data.FirstOrDefault(x => x.Status == MandateStatus.Valid);
                if (firstValidMandate != null)
                {
                    return(firstValidMandate);
                }
            }

            Assert.Inconclusive("No mandates found. Unable to test recurring payments");
            return(null);
        }
Ejemplo n.º 8
0
        public async Task CanCreatePaymentWithMandate()
        {
            // If: We create a payment with a mandate id
            MandateResponse validMandate = await this.GetFirstValidMandate();

            CustomerResponse customer = await this._customerClient.GetCustomerAsync(validMandate.Links.Customer);

            PaymentRequest paymentRequest = new PaymentRequest()
            {
                Amount       = new Amount(Currency.EUR, "100.00"),
                Description  = "Description",
                RedirectUrl  = this.DefaultRedirectUrl,
                SequenceType = SequenceType.Recurring,
                CustomerId   = customer.Id,
                MandateId    = validMandate.Id
            };

            // When: We send the payment request to Mollie
            PaymentResponse result = await this._paymentClient.CreatePaymentAsync(paymentRequest);

            // Then: Make sure we get the mandate id back in the details
            Assert.AreEqual(validMandate.Id, result.MandateId);
        }
Ejemplo n.º 9
0
        public async Task CanCreateRecurringPaymentAndRetrieveIt()
        {
            // If: we create a new recurring payment
            MandateResponse mandate = await this.GetFirstValidMandate();

            CustomerResponse customer = await this._customerClient.GetCustomerAsync(mandate.Links.Customer);

            PaymentRequest paymentRequest = new PaymentRequest()
            {
                Amount       = new Amount(Currency.EUR, "100.00"),
                Description  = "Description",
                RedirectUrl  = this.DefaultRedirectUrl,
                SequenceType = SequenceType.First,
                CustomerId   = customer.Id
            };

            // When: We send the payment request to Mollie and attempt to retrieve it
            PaymentResponse paymentResponse = await this._paymentClient.CreatePaymentAsync(paymentRequest);

            PaymentResponse result = await this._paymentClient.GetPaymentAsync(paymentResponse.Id);

            // Then: Make sure the recurringtype parameter is entered
            Assert.AreEqual(SequenceType.First, result.SequenceType);
        }
Ejemplo n.º 10
0
        public async Task <ActionResult> Detail(string customerId, string mandateId)
        {
            MandateResponse mandate = await this._mandateClient.GetMandateAsync(customerId, mandateId);

            return(this.View(mandate));
        }