How it works...

We have created a /get_book_data route in the service module. This route is used to handle a customer's IAP requests. So, from this client module, we will make a JSON-RPC request to that route. This IAP request will capture the credit and fetch the book's data. Luckily, the IAP module provides a wrapper for a jsonrpc request, so we will use it.

The library.book model of the my_library module doesn't have the ISBN and cover_image field, so we have extra fields in the library.book model by inheritance. Refer to the Adding features to a Model using inheritance recipe from chapter 5 Application Models. We have added fields through inheritance because we don't want to use these fields when the iap_isbn_client module is not installed.

In order to initiate a request, we have added a button to the book's form view through inheritance. A button click will trigger the fetch_book_data() method, and in that method, we have made the jsonrpc request to the service endpoint. With the request, we have passed two parameters: the customer account tokens and the ISBN number for the book data.

You can get a customer account token from the get() method of the iap.account model. The token generation is automatic. You just need to call the get() method with the name of the service. In our case, the service name is book_isbn. This will return the record set of the customer IAP account, and you can grab the customer token account_token field.

We have made a jsonrpc request to fetch the book info. If the customer doesn't have enough credit, the service module will generate InsufficientCreditError. The jsonrpc will handle this exception automatically, and it will display a popup to the customer to purchase the credit. The popup will have a link to the page where the customer can purchase the service plans. As we are using the sandbox, you can get any pack without payment. However, in production, the customer needs to make a payment for the service.

Upon a button click, if everything goes well, the customer has enough credit, and our database has data for the requested ISBN, the credit will be deducted from the customer account and jsonrpc will return the book's data. Then, we simply pass the result to the process_result() method and write data to the book's record.

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

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