Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve validateReceiptIos #239

Merged
merged 2 commits into from
Aug 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 50 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,42 +147,30 @@ 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.
* @returns {json | boolean}
* @returns {Promise<object>}
*/
export const validateReceiptIos = async (receiptBody, isTest) => {
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),
});
const url = isTest ? 'https://sandbox.itunes.apple.com/verifyReceipt' : 'https://buy.itunes.apple.com/verifyReceipt';

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

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

/**
* Validate receipt for ios.
* Validate receipt for android.
* @param {string} packageName package name of your app.
* @param {string} productId product id for your in app product.
* @param {string} productToken token for your purchase.
Expand Down Expand Up @@ -215,6 +203,40 @@ export const validateReceiptAndroid = async (packageName, productId, productToke
}
};

/**
* deprecagted codes
*/
/*
export const validateReceiptIos = async (receiptBody, isTest) => {
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) {
const json = await res.text();
res = JSON.parse(json);
return res;
}

return false;
} catch (err) {
console.log(err);
return false;
}
}

return response.json();
};
*/

export default {
prepare,
endConnection,
Expand Down