PeterStaev / nativescript-purchase

:moneybag: A NativeScript plugin for making in-app purchases!
Apache License 2.0
83 stars 28 forks source link

how to handle subscriptions with nativescript-purchase #99

Closed alexisconsuegra closed 4 years ago

alexisconsuegra commented 4 years ago

Hello, I would like to know if the plug in already handles subscriptions. Could you provide a sample in plain JavaScript code? Thanks

PeterStaev commented 4 years ago

Hey @alexisconsuegra , yes, this plugin supports subscriptions. There is no specifics to subscriptions on the app side. The only additional thing you need to do is send the store receipt to you backend so you can validate with google/apple the subscription validity/expiration.

alexisconsuegra commented 4 years ago

Thanks @PeterStaev How can the store receipt be validated? Any sample code in JavaScript is appreciated. also sorry for this question bit what the the javascript code for: import { Transaction, TransactionState } from "nativescript-purchase/transaction"; Thanks again.

PeterStaev commented 4 years ago

@alexisconsuegra , you can search over the internet how to validate the store receipts for sample code, as this largely depends on what backend server are you using (.NET/Node.js/etc).

For the JS code it should be something like this:

var Transaction = require("nativescript-purchase/transaction").Transaction;
var TransactionState = require("nativescript-purchase/transaction").TransactionState ;
alexisconsuegra commented 4 years ago

@PeterStaev, Thanks a lot. I certainly will do that although I still think something is missing. I hope you can help me understand. The workflow as I understand it goes:

  1. Initialize the IAP
  2. Check if user can purchase
  3. Buy it
  4. The transaction hookup will notify if purchase was successful
  5. Store the receipt. Not sure what the receipt looks like for a subscription but so far so good.
  6. *** Sometime later the subscription is auto-renewed. How the backed will know the subscription was renewed or if the user cancelled it? Isn't this information coming from the store?

Sorry I am still so new to this. Thanks for your help.

PeterStaev commented 4 years ago

You dont store the receipt, as it changes. Every time you need to check if the user has access to your subscription content, you send the receipt to your backend and it validates it and get's all the info (if it is renewed, if it cancelled, etc.)

alexisconsuegra commented 4 years ago

I must be missing something. You said in regards to validate the subscription "largely depends on what backend server are you using (.NET/Node.js/etc)." None of those backend servers communicate with Apple/Google store. Isn't the plugin the mean to interface with the store? You can store the receipt (having subscription terms/length etc) when it was purchased in some sort of backend. Then you can check if it expires. But you will never know with out asking the store if it was ever renewed.

PeterStaev commented 4 years ago

The plugin communicates with the stores so that you receive the receipt. This receipt you must send to you backend servers to validate with the Apple/Google servers. Both Apple & Google recommend server to server validation of the store receipts as it is more secure than validating the receipt on-device.

PeterStaev commented 4 years ago

No further response so closing this one for now.