adyen-examples / adyen-node-online-payments

Accept payments on your Node.js/Express-based website with cards, wallets, and key local payment methods
MIT License
50 stars 51 forks source link

Recurring payments #42

Closed anesuc closed 2 years ago

anesuc commented 2 years ago

Hi,

I have been having difficulty trying to figure out how to setup recurring payments with this. The documentation on this (recurring payments) isn't as clear as I would like (for me anyway). Any samples or direction on how I would adjust something like this to have recurring payments?

Thanks

jlengrand commented 2 years ago

Hey @anesuc ,

you are correct, that sample does not support take recurring payments. I'll add this to our list of samples in the future 😊.

In the meantime, I created a merge request with the changes you should make to the sample for it to accept recurring payments. I took the example of subscriptions but you should be able to implement any of the scenarios mentioned here with minor changes.

In short, we need to start tokenizing the payment information received, so they can be reused in the future. We do this by adding extra information in our /sessions or / payments call

      shopperInteraction : "Ecommerce",
      recurringProcessingModel: "Subscription",
      storePaymentMethod: true,

(Note that you can also enable tokenisation for other things than recurring payments, for example to avoid your customers having to insert their card information again in future visits). See here for more information.

Then, you need to be ready to receive the shopperReference and recurringDetailReference information that will be returned in the notifications once the payments has been validated.

You can do this in two ways :

image image image

You will then start receiving additional notifications. This is the choice I have made in the merge request.

In both cases, you should start capturing that information on your end, and coupling it with your unique customer ID and probably the date of recurring payment.

At that point, you can use that information to initiate future payments yourself from the server side, for example by daily batches. I added an example of such a call in the merge request, though note that in my case it is not connected to anything because the sample is not made for it.

Hope that helps! Let me know if you have any additional question!

anesuc commented 2 years ago

Fantastic! Let me try this

anesuc commented 2 years ago

Hi @jlengrand

Thanks for the well documented response! I have been getting the error Erro validating HMAC Signature recurring is not defined. Any suggestions as to why this might be? I have re-gnerrated the HMAC Key just incase to no avail.

Thanks

anesuc commented 2 years ago

Hi again,

Sorry, my bad, I figured it out. Just needed quotes around 'recurring.shopperReference' and 'recurring.recurringDetailReference'.

i.e

console.log('shopperReference:' + notification.additionalData['recurring.shopperReference']);
console.log('recurring.recurringDetailReference:' + notification.additionalData['recurring.recurringDetailReference']);
jlengrand commented 2 years ago

Great to hear. Did you manage to get it all working?

anesuc commented 2 years ago

Hi @jlengrand ,

Sorry ended up being pretty busy in the last couple of days. It works! However, when I make a payment using the stored details on renewal, I have no idea who it belongs to as I don't get shopperReference back from the AUTHORISATION event on the webhook(which is the only event I receive at this point. Note this is not during the initial payment but further payments).

The second one related to this, the user has to click "save for my next payment" which for my use-case has to be by default as this is for a subscription account. If they don't I also don't get the recurring details (for obvious reasons) alongside the shopperReference and therefore don't know who it belongs to. Maybe if there is a way to check before processing to let the user know they need to enable it when paying for a subscription then that would be great. This is because there is other payment options not subscription related. But for subscriptions I want to be sure I can process it at the end of the month (well as long as the card is active etc).

Thanks again!

jlengrand commented 2 years ago

Hey @anesuc !

Right, let me answer your questions :

NOTE: Starting with API V49, we have renamed recurringDetailReference to storepaymentmethodid. You may want to plan accordingly.

Hope that helps!

Here is a sample AUTHORISATION I receive after activating the additional details :

{
  "additionalData": {
....
    "PaymentAccountReference": "K5QVz9Kw5PUg6LhbPcAEiOetLlpbY1",
    "checkoutSessionId": "C123S7BE63F963F7BA7BD",
    "recurring.recurringDetailReference": "J2BT2X9KC6KXWD82rthrthdxs",
    "recurringProcessingModel": "Subscription",
    "recurring.shopperReference": "a104ee10-2d10-4ad7-925b-5bfeb0c19ef3"
  },
  "amount": {
    "currency": "EUR",
    "value": 1000
  },
  "eventCode": "AUTHORISATION",
  "eventDate": "2022-06-01T15:37:13+02:00",
...
}
jlengrand commented 2 years ago

Hey @anesuc, still having issues?

anesuc commented 2 years ago

@jlengrand Sorry for the late reply, was away for a bit. Started working on this but hadn't finished testing things related to this so waited to reply. I'll update this later today!

anesuc commented 2 years ago

Hi @jlengrand ,

I finally managed to have time today to work on this!

Edit: Figured out what was happening. Working fine now

Thanks for the help really appreciate it!

anesuc commented 2 years ago

I have updated the comment above, this was an issue on my end. I just realised the email notifications on this won't show the edits s I thought I would make a new one clarifying.

jlengrand commented 2 years ago

Alright, that's great to hear 😊.

Can I close the issue?

anesuc commented 2 years ago

Yep thanks! I'll do it now actually!