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 8d40aad
Showing 1 changed file with 19 additions and 35 deletions.
54 changes: 19 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,44 +137,28 @@ 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.
* Validate receipt for iOS.
* @param {object} 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}
* @returns {Promise<object>}
*/
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;
}
export const validateReceiptIos = async (receiptBody, isTest) => {
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 8d40aad

Please sign in to comment.