Skip to content

Commit

Permalink
Improve validateReceiptIos
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Aug 16, 2018
1 parent 36b045e commit a6cd9a5
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,37 +144,22 @@ export const consumePurchase = (token) => Platform.select({
* @returns {json | boolean}
*/
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 {
let res = await fetch(URL, {
method: 'POST',
headers: new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
}),
body: JSON.stringify(receiptBody),
});

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

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

return false;
} catch (err) {
console.log(err);
return false;
}
const url = isTest ? 'https://sandbox.itunes.apple.com/verifyReceipt' : 'https://buy.itunes.apple.com/verifyReceipt';

const response = await fetch(url, {
method: 'POST',
headers: new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
}),
body: JSON.stringify(receiptBody),
});

if (!response.ok) {
throw Object.assign(new Error(response.statusText), { statusCode: response.status })
}
console.log('No ops in android.');
return false;

return response.json();
};

/**
Expand Down

0 comments on commit a6cd9a5

Please sign in to comment.