How it works...

In order to draw credit from the customer's account, we will need a service key generated from the IAP platform. In the Registering an IAP service in Odoo recipe of this chapter, we generated a service key. (It's no problem if you have lost the service key; it can be regenerated from the service page). We have added an isbn_service_key field in the general settings so that we can store a service key in Odoo. You may have noticed that we used the config_parameter attribute in the file definition.

The use of this attribute in the field will store the value in the ir.config_parameter model, also known as the System Parameters. After saving it, you can check its value in the Technical | Parameters | System Parameters menu, in developer mode. While capturing IAP credits, we will retrieve the service key from the System Parameters. To retrieve the values from the System Parameters, you can use get_param(). For example, you can fetch a service key like this:

self.env['ir.config_parameter'].sudo().get_param('iap.isbn_service_key', False)

Here, the first argument is the key of the parameter that has a value you want to access, and the second argument is a default value. If the requested key is not present in the database, then the default value will be returned.

Next, we updated the /get_book_data route. Now, it is accepting two arguments:

  • The account_token, which is the customer token used to identify the user. The credit purchased by the customer for the service will be linked to this account_token in the IAP platform. Service providers will send this token while capturing credit.
  • The isbn_number is the ISBN number of the book whose information the customer wants in exchange for credit.
These arguments are not fixed here. Our example service needs an isbn_number, so we have passed it. However, you can pass any number of arguments that you want. Just make sure that you have passed the account_token, because without it, you cannot capture credit from the customer account.

The IAP provides the iap.charge() helper method, which handles the process of capturing credit from the customer account. The charge() method accepts four parameters: the environment, the provider service key, the customer account token, and the amount of credit to capture. The charge() method manages the following things:

  • Creating the transaction object and reserving the specified number of credits. If a customer account doesn't have enough credit, then it will raise InsufficientCreditError.
  • If enough credit is found in a customer account, it will run code in the with block.
  • If the code in the with block runs successfully, it captures the received credit.
  • If the code in the with block generates an exception, it will release the reserved credit, as the service request cannot be completed.

In the previous example, we used the same iap.charge() method to capture credit for a book request. We used our service key and customer account token to reserve the 1 credit for the book info. Then, inside the with block, we used the _books_data_by_isbn() method to get book data based on the ISBN number. If the book data is found, then it will execute the with block without any errors and one reserved credit will be deducted from the customer account. Later, we will return this data to the customer. If the book data is not found, then we raise the Exception so that the reserved credit is released.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset