Viincenttt / MollieApi

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

Get customerId when creating a new customer via Customers API #263

Closed neo1992 closed 2 years ago

neo1992 commented 2 years ago

I want to create a new customer and then start a first payment of a unlimited series of recurring payments (until customer cancels).

I have this so far:

Protected Async Function CreateNewCustomer(ByVal emailAddress As String, ByVal fullName As String, ByVal locale As String) As Threading.Tasks.Task
    Dim customerRequest As Models.Customer.CustomerRequest = New Models.Customer.CustomerRequest() With {
    .Email = emailAddress,
    .Name = fullName,
    .Locale = locale'Models.Payment.Locale.en_US
}
    Dim customerClient As Client.Abstract.ICustomerClient = New Mollie.Api.Client.CustomerClient(ConfigurationManager.AppSettings("mollie_api_testkey"))

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

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

    Dim subscriptionClient As Mollie.Api.Client.Abstract.ISubscriptionClient = New Mollie.Api.Client.SubscriptionClient(ConfigurationManager.AppSettings("mollie_api_testkey"))

    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"
    'STEP 1: can't add property `Sequencetype` here to indicate first payment request
}

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

End Sub

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

    Dim customerResponse As Models.Customer.CustomerResponse = Await CreateNewCustomer("test@test.com", "John Doe", Models.Payment.Locale.en_US) 'getting design time error "Expression does not produce a value"

    'STEP 2: I need customerResponse.Id here for the next function, how do I get it from the previous call?

    StartPayment(customerResponse.Id, 150)
End Sub

However, I don't know how to get the response of the customer creation request (STEP 2) so I can use the new customer id in the StartPayment method.

On a separate note, in Mollie's documentation there's mention of a Sequencetype property that needs to be set for "first", however this is not a property of SubscriptionRequest (see STEP 1) https://docs.mollie.com/payments/recurring#payments-recurring-first-payment

neo1992 commented 2 years ago

Anyone?

Viincenttt commented 2 years ago

Hi @neo1992 ,

The customer id can be retrieved from the CustomerResponse class after making the request. For example:

string name = "Smit";
string email = "johnsmit@mollie.com";
CustomerClient customerClient = new CustomerClient(this.ApiKey);
CustomerRequest customerRequest = new CustomerRequest() {
    Email = email,
    Name = name,
    Locale = Locale.nl_NL
};

CustomerResponse result = await customerClient.CreateCustomerAsync(customerRequest);
string customerId = result.Id;

The SequenceType property is part of the PaymentRequest class, since the customer needs to give consent through a first payment. This is explained further in the Mollie documentation on recurring payments, which you can find here: https://docs.mollie.com/payments/recurring

Kind regards, Vincent

neo1992 commented 2 years ago

Hi Vincent,

Thank you for you reply :)

Item 1: I thought your example is exactly what I'm trying to do with this code? I however get a design time error "Expression does not produce a value".

Protected Async Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim customerResponse As Models.Customer.CustomerResponse = Await CreateNewCustomer("test@test.com", "John Doe", Models.Payment.Locale.en_US) 'getting design time error "Expression does not produce a value"
    'STEP 2: I need customerResponse.Id here for the next function, how do I get it from the previous call?
    StartPayment(customerResponse.Id, 150)
End Sub

Item 2: Ok, so SequenceType is part of the paymentrequest class, and in my example I wanted it to make that part of SubscriptionRequest. Does it mean I need to do 2 calls somehow, one using paymentrequest for 1 first payment and a second one subscriptionrequest for ongoing payments? My reasoning was that the first payment would be equal to the monthly subscription so I could just call SubscriptionRequest to both initiate the initial as well as subsequent payments?

Viincenttt commented 2 years ago

Hi @neo1992

Regarding issue 1: I am no VB.NET expert, but shouldn't the method CreateNewCustomer return the customerResponse object? Right now it isn't returning anything and I think that is causing the issue. For example:

Protected Async Function CreateNewCustomer(ByVal emailAddress As String, ByVal fullName As String, ByVal locale As String) As Threading.Tasks.Task
    Dim customerRequest As Models.Customer.CustomerRequest = New Models.Customer.CustomerRequest() With {
    .Email = emailAddress,
    .Name = fullName,
    .Locale = locale'Models.Payment.Locale.en_US
}
    Dim customerClient As Client.Abstract.ICustomerClient = New Mollie.Api.Client.CustomerClient(ConfigurationManager.AppSettings("mollie_api_testkey"))

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

Regarding issue 2: The customer first needs to do a payment with the customerId and sequenceType properties set. sequenceType should be set to first. After that, you'll have a mandate which you can use to setup recurring payments.

neo1992 commented 2 years ago

Thanks. Here's a working code sample for other people that may be interested:

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(ConfigurationManager.AppSettings("mollie_api_livekey"))

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

    Return customerResponse
End Function