Klevr Payments
Klevr Payments have been set up to allow both one-time and recurring payments to be made via the portal.
Feature Overview
Klevr Payments have been set up to allow both one-time and recurring payments to be made via the portal. When a user selects the pay button they are directed to the Stripe payment gateway to enter their card details. All card details are handled by Stripe and are not entered or stored in the portal in any way. The Stripe Payment gateway then responds with a success or fail message back to the portal which can be seen by the user and can control behavior and further redirecting in the portal.
Assumptions
- The customer must have a Stripe account.
- The Stripe account needs to be configured within Stripe and this affects how the payment process works.
Dependencies
The following are dependencies for this feature:
- Klevr Models
- Klevr Utilities
- Klevr Forms
- Stripe Subscription
- The Stripe API version on the stripe account matches the version in the Portal Code
- A bit of configuration and possibly some flows / plugins depending on your overall process requirements.
- There is a table in Dynamics called transactioncurrency. Depending on the currency of choice, the Currency Name for this record must be updated to the correct Currency Code (i.e. Australian Doller = AUD). This value is passed to Stripe with the payment transaction and only currency codes are accepted.
- The correct library variables need to be inputted before the portal code is deployed to the resources
- Klevr.StripeApiKey - This is the Secret Key that can be found in the Developer Dashboard on the Customer's Stripe Account on the API Keys Tab.
- Klevr.StripeEndpointSecret - In order to get this value, a webhook must be registered within the Developer Dashboard on the Customer's Stripe account. The Webhook should look like: https://<PORTAL URL>/_hooks/stripewebhook. The following events should be listened for:
- All Charge events
- All checkout events
- All customer events
- All invoice events
- All Payment Method events
- All Payout enents
- All Setup_intent events
- All Subscription_Schedule events
We may add additional events in the future, and we ignore unused events, so you could add all events too, or keep up to date with the latest changes and add events as and when we include them.
Design Details
Stripe releases updates to their API from time to time. They use dates as version numbers. They also release updates to the code packages to then match the latest API version. The Portal code has a check to ensure the versions match, otherwise it throws an error. Once you're on an API version in stripe, it allows you to stay on that version even if they release a new one. For how long I don't know. But if you have a new stripe subscription, you cannot select an older version of the API to use. This means that every time a new customer uses the stripe product code in klevr, we will need to update the code to the latest stripe modules, but then at the same time, any existing sites that upgrades their portal would then also have to update their Stripe Consoles to use the new matching version.
The current version of the API used in the portal code is logged to the Umbraco logs on startup.
The version of the API in Stripe can be found under the webhook configuration area.
There are three methods to implement payments.
Method 1 - Form:
Create a Dynamics form within Umbraco back office for the klevr_financialtransaction entity.
Add a custom button to "Pay Now" and set a custom field value (klevr_executepayment = 1).
This will check if the contact is a customer in Stripe.
If not, a stripe customer will be created, and references will be set in both systems.
Then, it will check if there is a payment session id on the payment record already.
If so, it will process the result.
If not, it will create a new payment session and set the session id and redirect the user to Stripe for payment.
On success, it will process the payment and then use the button settings to redirect the user as appropriate (normal redirecting behavior for buttons will be followed). It is recommended that you redirect to a new page where you can have a script to determine if the payment was susccessful or not based on the querystring, and give an appropriate message to the user.
Method 2 - Custom Template:
Create a custom template(the suggested method is to use the custom template as a secondary template on the message document item).
Within the custom template, generate the stripe session for a payment record, and redirect the user.
Here is an example of the minimal code snippet that will make this work, you are also in control of the return URL.
You must have a klevr_financialtransaction record populated with the correct information, and have the guid for it. You can create one using the server side API (ensure that the amount does not come from an untrusted source like the querystring, as this could be intercepted and manipulated by a rogue user), or have it created as part of another process before the user comes to this page.
@inherits UmbracoViewPage
@inject KlevrPortal.Core.Services.APIService api;
@{
Layout = "layout.cshtml";
if (Context.Request.Query["paynow"].FirstOrDefault() == "yes") {
Context.Response.Redirect(api.GeneratePaymentUrl("/message/?title=Payment%20Completed",
new Guid("Payment Record ID"), Context.Request));
}
}
<section className="section">
<div className="container">
<h1>@</SPAN>Context.Request.Query["title"].FirstOrDefault()</h1>
<p>@</SPAN>Context.Request.Query["message"].FirstOrDefault()</p>
</div>
</section>
Redirect the user to your page with ?paynow=yes query string parameter.
- The Financial Transaction record must be saved before the pay now template button should appear.
- The klevr_name, currency and amount fields are required to make the payment.
Method 3 - Payment Generator:
Method 3 attempts to do the legwork of method 2 for you. You can use a button on a page to redirect to a newly provided template that will map an amount field from an existing entity to a payment record that is created for you. the new template is called "Payment Generator".
Create a new page of type Payment Generator and populate the fields (The entity schema name should be the originating entity name).
Add a custom button to your entity form to redirect to the payment generator page.
This page will create a new financial transaction record relating to the source record and redirect the user either to the payment gateway or to a dynamics form configured to klevr_financialtransactions (if the verification step is used).
This only supports single payments at the moment, and the mapping to create the financial transaction is very basic.
Make sure to use the Poly Editor to update the klevr_regardingpolyid field on the klevr_financialtransaction record to include the originating entity from Step 1.
It will set the customer lookup to the logged in contact, so this can be used in your security model for payment records.
The payment (klevr_financialtransaction) record will be created without validating create permissions, so you don't need to add this. It will require read and update permissions for the rest of the process to succeed.
Additional functions for the user profile page
Updating Payment Method Button:
Add a button to the profile form with the following information:
- Title = "Update Payment Method"
- icon = pi pi-credit-card
Required Properties:
- Javascript Function: UpdatePaymentMethod()
- Filter: <condition attribute="statuscode" operator="ne" value="2"/>
This button only works on the profile page, since this is where the provided "UpdatePaymentMethod" is defined. For more advanced users, there is a server-side MVC line of code to generate a reset URL. It requires a success and failure return URL and the current logged in user's klevr_externalpaymentkey field.
e.g.
resetPaymentMethod = new StripePaymentService().CreatePaymentMethodSession(Context.Request.Url(), Context.Request.Url(), contact.klevr_ExternalPaymentKey).Url
Navigate to the Stripe User Portal Button:
You can configure the Stripe customer portal functions on your Stripe Dashboard. For example, this allows the users to see a bit of their history of payments, and potentially manage their subscriptions if you want to allow that.
Add a button to the profile form with the following information:
- Title = "Update Payment Method"
- icon = pi pi-credit-card
Required Properties:
- Javascript Function: CustomerBillingPortal()
- Filter: <condition attribute="statuscode" operator="ne" value="2"/>
This button only works on the profile page, since this is where the provided "CustomerBillingPortal" is defined.
Troubleshooting
Payment processing works as follows:
- when the payment is first marked as "Requested", a plugin sends the details to the payment provider (stripe/payfurl) and marks payment status as started.
- Payfurl/Stripe attempts to make the payment. Both services use web hook calls back to Klevr Portal to tell it what is going on. The web hook configuration must be set up in the relevant payment gateway settings.
- Klevr Portal processes the web hook calls and updates the payment record in CE as successful or failed. If failed, it creates the payment failure record, and links it to the specific payment processing status record. It gets the retry info from this record. Based on the retry, it will increase the retry count on the payment record, and set the "process on" timestamp to x hours in the future, according to the retry config. If it has run out of retries, or its specified as a hard fail, it updates the payment status to the one specified in the payment processing status record for the last error it received.
- The Flow runs again (configured by default as every hour) and picks up any payment that is awaiting retry and has a process on date in the past (i.e. the x hours specified has elapsed).
- The flow triggers the initial plugin again to attempt payment. This sends the process back to step 1 above to do it all over again, until it hits a hard stop failure or runs out of retries, or succeeds.
From this you can trouble shoot as follows:
Is status requested: Plugin in step 1 never executed.
Is status started and process on blank: Webhook didn't work -> check payment provider web hook details and check portal Hangfire logs to see how far it got
Is status started and process on is in the future: Wait. While waiting, check retry count and last payment failure and payment processing status to see what it should be doing. You can also check payment provider logs and Hangfire logs for the details of the last webhook for the transaction.
Is "process on" in the past: Check to see if the flow has run since then and see if it picked it up. If not, check the filter criteria on the flow and figure out why not.
Is payment status cancelled, check status description, last payment failure, and payment provider logs for more details.
Is payment status succeeded, nothing to check, you have money in the bank.
Security Considerations
- You need to configure the table permissions for klevr_financial transactions and ensure the users are only able to access their payment records.
Last updated on