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 and to help you to charge a SysUser an amount of 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):String

Mark it with a tagged value: Eco.ExternalLateBound = Existence

2020-05-17 22h14 46.png

What turnkey will do when you call this method is the following:

It will post this data to https://portal.mdriven.net/Consume that is a RestAllowed viewmodel in Portal. The rootobject is looked up as the TurnkeySite-object that exists in portal based on its guid. The guid your turnkey system finds in the TurnkeySettings.xml file that portal writes when you say SendSettings in 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 portal:

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

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

An example on how to charge a SysUser:

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

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 5 earlier transactions for this SysUser - that now has become a SysConsumer in Portal. A SysConsumer has an account with MCoins/Euros that the consumer can top up.

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

This page was edited 48 days ago on 02/10/2024. What links here