Skip to content

Commit

Permalink
Merge pull request #141 from dooboolab/dev
Browse files Browse the repository at this point in the history
Support RN version below 54 for validation.
  • Loading branch information
hyochan authored Apr 26, 2018
2 parents 8dfbe31 + 2da0292 commit 5275f65
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Also there are some other methods that is not supported in ios and implemented i
Lastly, this module also supports types for typescript users from `0.2.5`.

## Changelogs
- **[0.3.19]**
+ Upted `validateReceiptIos` and `validateReceiptAndroid` methods to support all RN version.
- **[0.3.17]**
+ Implemented receipt validation. See the `Receipt validation` section in the readme. For `android`, you should have your own backend to get `access_token` from `googleapis`.
- **[0.3.13]**
Expand Down Expand Up @@ -144,8 +146,8 @@ Lastly, this module also supports types for typescript users from `0.2.5`.
| consumeProduct | `string` Purchase token | `Promise<void>` | Consume a product (on Android.) No-op on iOS. |
| endConnection | | `Promise<void>` | End billing connection (on Android.) No-op on iOS. |
| refreshItems | | `Promise<void>` | Consume all items in android so they are able to buy again (on Android.) No-op on iOS. |
| validateReceiptIos | `object` receiptBody, `boolean` isTest | `object or boolean` result | validate receipt for ios. Only works RN `>= 0.55` |
| validateReceiptAndroid | `string` packageName, `string` productId, `string` productToken, `string` accessToken, `boolean` isSubscription | `object or boolean` result | validate receipt for android. Only works RN `>= 0.55` |
| validateReceiptIos | `object` receiptBody, `boolean` isTest, `number` RNVersion | `object or boolean` result | validate receipt for ios. |
| validateReceiptAndroid | `string` packageName, `string` productId, `string` productToken, `string` accessToken, `boolean` isSubscription, `number` RNVersion | `object or boolean` result | validate receipt for android. |

## Npm repo
https://www.npmjs.com/package/react-native-iap
Expand Down Expand Up @@ -329,15 +331,15 @@ You need to test with one sandbox account, because the account holds previous pu


## Receipt validation
From `[email protected]`, we support receipt validation. For android, you need seperate json file from service account to get the `access_token` from `google-apis`, therefore it is impossible to implement serverlessly. You should have your own backend and get `access_token` from there. With that you can simple call `validateReceiptAndroid` method we implemented. Further reading is [here](https://stackoverflow.com/questions/35127086/android-inapp-purchase-receipt-validation-google-play?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa).
From `[email protected]`, we support receipt validation. For android, you need seperate json file from service account to get the `access_token` from `google-apis`, therefore it is impossible to implement serverlessly. You should have your own backend and get `access_token`. With `access_token` you can simplly call `validateReceiptAndroid` method we implemented. Further reading is [here](https://stackoverflow.com/questions/35127086/android-inapp-purchase-receipt-validation-google-play?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa).

Only, serverless receipt validation is possible currently using `validateReceiptIos` method. First parameter, you should pass `transactionReceipt` which returns after `buyProduct`. Second parameter, you should pass whether this is `test` environment. If `true`, it will request to `sandbox` and `false` it will request to `production`.
Currently, serverless receipt validation is possible using `validateReceiptIos` method. First parameter, you should pass `transactionReceipt` which returns after `buyProduct`. Second parameter, you should pass whether this is `test` environment. If `true`, it will request to `sandbox` and `false` it will request to `production`.

```javascript
const receiptBody = {
'receipt-data': purchase.transactionReceipt,
};
const result = await validateReceiptIos(receiptBody, false);
const result = await validateReceiptIos(receiptBody, false, 54);
console.log(result);
```
For further information, please refer to [guide](https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html).
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function consumePurchase(token: string) : Promise<void>;
* Validate receipt for ios.
* @param {receipt-data: string, password?: string} receiptBody the receipt body to send to apple server.
* @param {string} isTest whether this is in test environment which is sandbox.
* @param {number} RNVersion version of react-native.
* @returns {json | boolean}
*/
export function validateReceiptIos(receiptBody: object, isTest:boolean) : object | boolean;
Expand All @@ -115,6 +116,7 @@ export function validateReceiptIos(receiptBody: object, isTest:boolean) : object
* @param {string} productToken token for your purchase. Found in `transanctionReceipt` after `buyProduct` method.
* @param {string} accessToken accessToken from googleApis.
* @param {boolean} isSub whether this is subscription or inapp. `true` for subscription.
* @param {number} RNVersion version of react-native.
* @returns {json | boolean}
*/
export function validateReceiptAndroid (packageName: string, productId: string, productToken: string, accessToken: string, isSub: boolean);
34 changes: 21 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ export const consumePurchase = (token) => Platform.select({
* Validate receipt for ios.
* @param {receipt-data: string, password?: string} receiptBody the receipt body to send to apple server.
* @param {string} isTest whether this is in test environment which is sandbox.
* @param {number} RNVersion version of react-native.
* @returns {json | boolean}
*/
export const validateReceiptIos = async (receiptBody, isTest) => {
export const validateReceiptIos = async (receiptBody, isTest, RNVersion) => {
if (Platform.OS === 'ios') {
const URL = !isTest ? 'https://sandbox.itunes.apple.com/verifyReceipt' : 'https://buy.itunes.apple.com/verifyReceipt';
try {
Expand All @@ -131,14 +132,17 @@ export const validateReceiptIos = async (receiptBody, isTest) => {
}),
body: JSON.stringify(receiptBody),
});

const json = await res.text();
console.log(json);
res = JSON.parse(json);


if (res) {
return res;
if (RNVersion < 54) {
const json = JSON.parse(res._bodyInit);
return json;
}

const json = await res.text();
res = JSON.parse(json);
}

return false;
} catch (err) {
console.log(err);
Expand All @@ -156,9 +160,10 @@ export const validateReceiptIos = async (receiptBody, isTest) => {
* @param {string} productToken token for your purchase.
* @param {string} accessToken accessToken from googleApis.
* @param {boolean} isSub whether this is subscription or inapp. `true` for subscription.
* @param {number} RNVersion version of react-native.
* @returns {json | boolean}
*/
export const validateReceiptAndroid = async (packageName, productId, productToken, accessToken, isSub) => {
export const validateReceiptAndroid = async (packageName, productId, productToken, accessToken, isSub, RNVersion) => {
const URL = !isSub
? `https://www.googleapis.com/androidpublisher/v2/applications/${packageName}/purchases/products/${productId}/tokens/${productToken}?access_token=${accessnToken}`
: `https://www.googleapis.com/androidpublisher/v2/applications/${packageName}/purchases/subscriptions/${productId}/tokens/${productToken}?access_token=${accessToken}`;
Expand All @@ -171,13 +176,16 @@ export const validateReceiptAndroid = async (packageName, productId, productToke
}),
});

const json = await res.text();
console.log(json);
res = JSON.parse(json);

if (res) {
return res;
if (RNVersion < 54) {
const json = JSON.parse(res._bodyInit);
return json;
}

const json = await res.text();
res = JSON.parse(json);
}

return false;
} catch (err) {
console.log(err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-iap",
"version": "0.3.18",
"version": "0.3.19",
"description": "React Native In App Purchase Module.",
"main": "index.js",
"types": "index.d.ts",
Expand Down

0 comments on commit 5275f65

Please sign in to comment.