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

Transform receipt on purchase validator #1610

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import API from '../../api';
import { isAndroid } from '../../cordova-util';
import { isAndroid, isIOS } from '../../cordova-util';

import {
updateIsInFreeCountry,
Expand Down Expand Up @@ -41,7 +41,7 @@ export class SubscriptionProvider extends Component {
const isInFreeCountry = updateIsInFreeCountry();
const isOnTrialPeriod = updateIsOnTrialPeriod();
await updatePlans();
if (isAndroid()) this.configInAppPurchasePlugin();
if (isAndroid() || isIOS()) this.configInAppPurchasePlugin();
if (!isInFreeCountry && !isOnTrialPeriod && !isSubscribed && isLogged) {
showPremiumRequired({ showTryPeriodFinishedMessages: true });
}
Expand All @@ -67,9 +67,54 @@ export class SubscriptionProvider extends Component {
};

configPurchaseValidator = () => {
const transformReceipt = receipt => {
const receiptTransaction = receipt.transaction;
const receiptData = JSON.parse(receipt.transaction.receipt);
const {
packageName,
productId,
purchaseTime,
purchaseState,
purchaseToken,
quantity,
autoRenewing,
acknowledged
} = receiptData;
const transaction = {
className: 'Transaction',
transactionId: receiptTransaction.id,
state: 'approved',
products: receipt.products,
platform: receiptTransaction.type,
nativePurchase: {
orderId: receiptTransaction.id,
packageName: packageName,
productId: productId,
purchaseTime: purchaseTime,
purchaseState: purchaseState,
purchaseToken: purchaseToken,
quantity: quantity,
autoRenewing: autoRenewing,
acknowledged: acknowledged,
productIds: receipt.products.map(product => product.id),
signature: receiptTransaction.signature,
receipt: receiptTransaction.receipt
},
purchaseId: purchaseToken,
purchaseDate: new Date(purchaseTime),
isPending: false,
isAcknowledged: acknowledged,
renewalIntent: 'Renew'
};
return transaction;
};

window.CdvPurchase.store.validator = async function(receipt, callback) {
try {
const transaction = receipt.transactions[0];
const transaction = isIOS()
? receipt.transaction
: transformReceipt(receipt);

const res = await API.postTransaction(transaction);
if (!res.ok) throw res;
callback({
Expand Down