Charge end user

If you run the standard authentication package, you will have your end users as SysUser objects.

Your service might cost some money to consume. To help you charge a SysUser some money when he or she performs an action or wants to access a resource behind your paywall, we have added some streamlined functionality to get you going in minutes.

Add this method to SysUser in your model:

Consume(amount:Double; message:String; tag:string):String

Also, add:

GetPurchaseUrl(amount:Double; amountreadonly:Boolean; product:string; tag:String):String  <- set this as IsQuery=true

Mark both methods with a tagged value: Eco.ExternalLateBound = Existence

2020-05-17 22h14 46.png

When you call this method, the Turnkey will do the following:

  • It will post this data to https://portal.mdriven.net/ Consume which is a RestAllowed ViewModel in Portal. The root object is looked up as the TurnkeySite-object that exists in the portal based on its Guid. The Guid in your Turnkey system finds in the TurnkeySettings.xml file that the portal writes when you say SendSettings in the portal.

The key in SendSettings is:

<PortalGuid>a guid from the TurnkeySite object in portal</PortalGuid>

The settings file also includes the SharedSecret that you define on the login tab in the portal:

<SharedSecret>a password shared between your site and portal</SharedSecret>

Your Turnkey system will sign a UTC timestamp with this shared secret, and the portal will decrypt and verify that your system sent the Consume command and that it is not an old recorded Consume command.

An example of how to charge a SysUser:

self.ConsumeRes:=SysSingleton.oclSingleton.CurrentUser
             .Consume(1.23,'You bought a magic strawberry','optional tag for you')

This call will return a JSON that looks like this:

{"Guid":"guid of the SysConsumer in portal",
 "ChangeTime":"2020-05-17T19:31:02.69",
"MCoinAccount_CurrentStanding":-2.46,
"LastTransactionTriedAmount":1.23,
"LastTransactionTriedMessage":"You bought a magic strawberry",
"LastTransactionTriedStatus":"Success",
"MCoinTransactions":[
  {"Guid":"a58753b9-2fe2-4fff-87ce-15294086fd9a",
   "MCoinAmount":-1.23,
   "Description":"Test20:06:40","ExitStanding":-2.46
  },
  {"Guid":"5d51e13b-c5cb-4329-aa72-de669c0d7109",
   "ChangeTime":"2020-05-17T19:31:02.69",
   "MCoinAmount":-1.23,
   "Description":"Test21:31:01","ExitStanding":-1.23
  }
]}

This JSON will contain the current transaction and the five earlier transactions for this SysUser - that has now become a SysConsumer in Portal. A SysConsumer has an account with MCoins/Euros that the consumer can top up.

You can decide to handle the returned JSON with Tajson Merge to build up a replica of the account information stored in the portal - but you do not have to.

Topping up the Consumer Account

To bring up a Purchase UI that allows your SysUser to top up the Account with Euros, do this:

Install the PortalConsumer component in EXT_Components

In a ViewModel, ensure that you have the following ViewModelColumns:

  • PurchaseUrl:SysSingleton.oclsingleton.CurrentUser.GetPurchaseUrl(amount:Double; amountreadonly:Boolean; product:string; tag:String):String
  • PayButtonText (string): This ViewModelColumn's value will be used by the PortalConsumer components Buy-Button
  • ReturnFromPurchaseAction (action): This action will be executed when the user closes the purchase window; ex vNewVar:=SysSingleton.oclsingleton.CurrentUser.Consume( 0 ,  'check' )
  • PortalConsumer: [Angular_Ext_Component=PortalConsumer] self->asset
This page was edited 158 days ago on 06/17/2024. What links here