Viincenttt / MollieApi

This project allows you to easily add the Mollie payment provider to your application.
MIT License
148 stars 85 forks source link

Unprocessable Entity - No suitable mandates found for customer #271

Closed neo1992 closed 2 years ago

neo1992 commented 2 years ago

In my case I want the first payment to be equal to any subsequent monthly payments, so I believe there's no need for an initial bank account test payment of 0.01, although that amount is what I'm using in the code below, but just for test purposes.

I get error:

"Unprocessable Entity - No suitable mandates found for customer"

On the last line of StartSubscription: Dim subscriptionResponse As Mollie.Api.Models.Subscription.SubscriptionResponse = Await subscriptionClient.CreateSubscriptionAsync(mollieCustomerId, subscriptionRequest)

A customer is successfully created, but the mandate apparently is not. On a separate note: I'm also unsure where I would actually get the IBAN code that I've now hardcoded.

When I check the Mollie Dashboard I see:

So not sure what I'm missing here. Here's what I do:

Protected Async Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    '1 create customer
    Dim customerName As String = "John Doe"
    Dim customerResponse As Models.Customer.CustomerResponse = Await CreateNewCustomer("test@test.com", customerName, Models.Payment.Locale.nl_NL)

    '2 start FIRST payment
    StartSubscription(customerResponse.Id, customerName, 150, PaymentOption.iDeal)

End Sub

Protected Async Function CreateNewCustomer(ByVal emailAddress As String, ByVal fullName As String, ByVal locale As String) As Threading.Tasks.Task(Of Models.Customer.CustomerResponse) 'Threading.Tasks.Task
    Dim customerRequest As Models.Customer.CustomerRequest = New Models.Customer.CustomerRequest() With {
    .Email = emailAddress,
    .Name = fullName,
    .Locale = locale'Models.Payment.Locale.nl_NL
}
    Dim customerClient As Client.Abstract.ICustomerClient = New Mollie.Api.Client.CustomerClient(<MYAPIKEY>)

    Dim customerResponse As Models.Customer.CustomerResponse = Await customerClient.CreateCustomerAsync(customerRequest)

    LogError("customerResponse.Id", customerResponse.Id.ToString) 'valid Id available here

    Return customerResponse
End Function

Protected Async Sub StartPayment(ByVal mollieCustomerId As String, ByVal priceInCents As Integer, ByVal pymt As MyFunctions.PaymentOption)
    Dim mollieCurrency As String = "EUR"
    mollieCurrency = Mollie.Api.Models.Currency.EUR

    Dim paymentClient As Mollie.Api.Client.Abstract.IPaymentClient = New Mollie.Api.Client.PaymentClient(<MYAPIKEY>)

    Dim paymentRequest As Models.Payment.Request.IdealPaymentRequest = New Models.Payment.Request.IdealPaymentRequest() With {
    .Amount = New Mollie.Api.Models.Amount(mollieCurrency, "0.01"),'this should be the full amount and not just 1 cent
    .CustomerId = mollieCustomerId,
    .SequenceType = "first",
    .Description = "first payment",
    .RedirectUrl = "https://www.example.com/test2.aspx?t=redirecturl",
    .WebhookUrl = "https://www.example.com/api/payments/webhook"
}

    'Dim subscriptionResponse As Mollie.Api.Models.Subscription.SubscriptionResponse = Await paymentClient.CreatePaymentAsync((mollieCustomerId, subscriptionRequest)

End Sub

Protected Async Sub CreateMandate(ByVal mollieCustomerId As String, ByVal customerName As String)
    Dim mandateclient As Client.Abstract.IMandateClient = New Client.MandateClient(<MYAPIKEY>)

    Dim mandateRequest As Models.Mandate.SepaDirectDebitMandateRequest = New Models.Mandate.SepaDirectDebitMandateRequest() With {
    .ConsumerAccount = {iban},
    .ConsumerName = customerName
}
    ' for `ConsumerAccount` I use my own account in format: "NL86INGB0000000000"

    Dim mandateResponse As Models.Mandate.MandateResponse = Await mandateclient.CreateMandateAsync(mollieCustomerId, mandateRequest)

End Sub

Protected Async Sub StartSubscription(ByVal mollieCustomerId As String, ByVal customerName As String, ByVal priceInCents As Integer, ByVal pymt As MyFunctions.PaymentOption)
    Dim mollieCurrency As String = "EUR"
    mollieCurrency = Mollie.Api.Models.Currency.EUR

    CreateMandate(mollieCustomerId, customerName)

    Dim subscriptionClient As Mollie.Api.Client.Abstract.ISubscriptionClient = New Mollie.Api.Client.SubscriptionClient(<MYAPIKEY>)

    Dim subscriptionRequest As Mollie.Api.Models.Subscription.SubscriptionRequest = New Mollie.Api.Models.Subscription.SubscriptionRequest() With {
    .Amount = New Mollie.Api.Models.Amount(mollieCurrency, "1.00"),
    .Interval = "1 month",
    .Description = "Test description"
}

    LogError("mollieCustomerId", mollieCustomerId) 'we have a valid customer Id here

    Dim subscriptionResponse As Mollie.Api.Models.Subscription.SubscriptionResponse = Await subscriptionClient.CreateSubscriptionAsync(mollieCustomerId, subscriptionRequest)

End Sub
DianaLaa commented 2 years ago

It seems you're not passing the mandateId into your CreateSubscriptionAsync call.

neo1992 commented 2 years ago

@DianaKoenraadt Thank you, that was it. I now see a new subscription in Mollie :) However, it's not realistic right now, as you can see in my code I'm now using a hardcoded ConsumerAccount value, IBAN in this case. How would I get that IBAN number in a production environment? I assume it's not something customers have to fill out themselves (maybe the same also goes for customer name). Do you know?

DianaLaa commented 2 years ago

You should really refer to the API documentation at Mollie.com. That's where I got my previous answer for you ;) Or their chat. Mollie offers good support.

I believe what I did in my application is I created a payment with the IPaymentClient, which I marked as SequenceType.First. Mollie then creates the mandate for me, with the IBAN number that the customer uses for the first payment. See here https://docs.mollie.com/payments/recurring

(FYI, this github is not maintaned by Mollie.com)

Viincenttt commented 2 years ago

Hi,

Above information is correct. The readme that is provided in this repository is meant as a reference guide on how to use the library. If you'd like to learn more on how Mollie recurring payments work, I'd like to refer you to the official Mollie documentation.

I'd also like to add that while it is true that this library is not owned by Mollie, I am in regular contact with Mollie regarding any changes they make to their API. Mollie also pays me to maintain and keep this library up to date. There are also talks to add this library to the official Mollie Github.

If you have any further questions, do not hesitate to ask.

Kind regards, Vincent