Adyen Tokenization Integration Demo
This repository includes a tokenization example for subscriptions. Within this demo app, you'll find a simplified version of a website that offers a music subscription service.
The shopper can purchase a subscription and administrators can manage the saved (tokenized) payment methods on a separate admin panel.
The panel allows admins to make payments on behalf of the shopper using this token. We refer to this token as recurringDetailReference in this application.
Note: We've included a technical blog post that explains every step of this demo.
This demo leverages Adyen's API Library for .NET (GitHub | Docs).
You will need .NET Core SDK 6.x. to run this application locally.
- Clone this repository.
git clone https://github.com/adyen-examples/adyen-dotnet-online-payments.git
-
Open your Adyen Test Account and create a set of API keys.
-
To allow the Adyen Drop-In and Components to load, add
https://localhost:5001as allowed origin by going to your MerchantAccount in the Customer Area:Developers→API credentials→ Find yourws_user→Client settings→Add Allowed origins.
Warning You should only allow wild card (*) domains in the test environment. In a live environment, you should specify the exact URL of the application.
This demo provides a simple webhook integration at /api/webhooks/notifications. For it to work, you need to provide a way for Adyen's servers to reach your running application and add a standard webhook in the Customer Area.
To expose this endpoint locally, we have highlighted two options in step 4 or 5. Choose one or consider alternative tunneling software.
- Expose your localhost with Visual Studio using dev tunnels.
- Add
https://*.devtunnels.msto your allowed origins - Create your public (temporary/persistent) dev tunnel by following this guide
- Add
If you use Visual Studio 17.4 or higher, the webhook URL will be the generated URL (i.e. https://xd1r2txt-5001.euw.devtunnels.ms).
- Expose your localhost with tunneling software (i.e. ngrok).
- Add
https://*.ngrok.ioto your allowed origins
- Add
If you use a tunneling service like ngrok, the webhook URL will be the generated URL (i.e. https://c991-80-113-16-28.ngrok.io/api/webhooks/notifications/).
$ ngrok http 8080
Session Status online
Account ############
Version #########
Region United States (us)
Forwarding http://c991-80-113-16-28.ngrok.io -> http://localhost:8080
Forwarding https://c991-80-113-16-28.ngrok.io -> http://localhost:8080 -
To receive notifications asynchronously, add a webhook:
- In the Customer Area go to
Developers→Webhooksand add a newStandard notification webhook - Define username and password (Basic Authentication) to protect your endpoint - Basic authentication only guarantees that the notification was sent by Adyen, not that it wasn't modified during transmission
- Generate the HMAC Key - This key is used to verify whether the HMAC signature that is included in the notification, was sent by Adyen and not modified during transmission
- See script below that allows you to easily set your environmental variables
- For the URL, enter
https://ngrok.iofor now - We will need to update this webhook URL in step 10 - Make sure that the
Recurring contractsetting is enabled onMerchantaccount-level - In theCustomer Area, underDevelopers->Webhooks->Settings-> EnableRecurring contractonMerchant-level and hit "Save". - Make sure that your webhook sends the
RECURRING_CONTRACTevent when you've created the webhook - Make sure the webhook is enabled to send notifications
- In the Customer Area go to
-
Set the following environment variables in your terminal environment:
ADYEN_API_KEY,ADYEN_CLIENT_KEY,ADYEN_MERCHANT_ACCOUNTandADYEN_HMAC_KEY. Note that some IDEs will have to be restarted for environmental variables to be injected properly.
export ADYEN_API_KEY=yourAdyenApiKey
export ADYEN_MERCHANT_ACCOUNT=yourAdyenMerchantAccount
export ADYEN_CLIENT_KEY=yourAdyenClientKey
export ADYEN_HMAC_KEY=yourAdyenHmacKeyOn Windows CMD you can use this command instead.
set ADYEN_API_KEY=yourAdyenApiKey
set ADYEN_MERCHANT_ACCOUNT=yourAdyenMerchantAccount
set ADYEN_CLIENT_KEY=yourAdyenClientKey
set ADYEN_HMAC_KEY=yourAdyenHmacKey-
In the Customer Area, go to
Developers→Additional Settings→ UnderPaymentenableRecurring Detailsfor subscriptions. -
Start the application and visit localhost.
dotnet run --project subscription-example - Update your webhook in your Customer Area with the public url that is generated.
- In the Customer Area go to
Developers→Webhooks→ Select yourWebhookthat is created in step 6 →Server Configuration - Update the URL of your application/endpoint (e.g.
https://c991-80-113-16-28.ngrok.io/api/webhooks/notifications/orhttps://xd1r2txt-5001.euw.devtunnels.ms) - Hit
Apply→Save changes
- In the Customer Area go to
Note When exiting ngrok or Visual Studio a new URL is generated, make sure to update the Webhook URL in the Customer Area as described in the final step. You can find more information about webhooks in this detailed blog post.
To try out this application with test card numbers, visit Test card numbers. We recommend saving multiple test cards in your browser so you can test your integration faster in the future.
-
Visit the main page 'Shopper View' to test the application, enter one or multiple card details.
-
Once the payment is authorized, you will receive a (delayed) webhook notification with the event code
RECURRING_CONTRACTand the recurringDetailReference. -
Enter multiple cards to receive multiple different recurringDetailReferences.
-
Visit 'Admin Panel' to find the saved recurringDetailReferences and choose to make a payment request or disable the recurringDetailReference.
-
Visit the Customer Area
Developers→API logsto view your logs.
Note We currently store these values in a local memory cache, if you restart/stop the application these values are lost. However, the tokens will still be persisted on the Adyen Platform. You can view the stored payment details by going to a recent payment of the shopper in the Customer Area:
Transactions→Payments→Shopper Details→Recurring: View stored payment details.
