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

Subscription For the Web version using PayPal #1469

Merged
merged 6 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@material-ui/icons": "^4.11.3",
"@material-ui/lab": "4.0.0-alpha.57",
"@microsoft/applicationinsights-web": "^2.8.11",
"@paypal/react-paypal-js": "^7.8.3",
"@redux-beacon/google-analytics-gtag": "^1.1.0",
"@redux-beacon/logger": "^1.0.0",
"@redux-beacon/offline-web": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Settings/Settings.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Settings extends PureComponent {
}
];

if (isAndroid() && !isInFreeCountry) {
if (!isInFreeCountry) {
martinbedouret marked this conversation as resolved.
Show resolved Hide resolved
const subscribeSection = {
icon: <MonetizationOnIcon />,
text: messages.subscribe,
Expand Down
16 changes: 13 additions & 3 deletions src/components/Settings/Subscribe/Subscribe.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const propTypes = {
/**
* Handle refresh subscription
*/
onRefreshSubscription: PropTypes.func
onRefreshSubscription: PropTypes.func,
onSubscribeCancel: PropTypes.func.isRequired,
onCancelSubscription: PropTypes.func.isRequired
};

const defaultProps = {
Expand All @@ -39,7 +41,10 @@ const Subscribe = ({
onSubscribe,
location: { country, countryCode },
subscription,
onRefreshSubscription
onRefreshSubscription,
onSubscribeCancel,
onPaypalApprove,
onCancelSubscription
}) => {
return (
<div className="Subscribe">
Expand All @@ -55,9 +60,14 @@ const Subscribe = ({
onRefreshSubscription={onRefreshSubscription}
isLogged={isLogged}
onSubscribe={onSubscribe}
onSubscribeCancel={onSubscribeCancel}
onPaypalApprove={onPaypalApprove}
/>
) : (
<SubscriptionInfo onRefreshSubscription={onRefreshSubscription} />
<SubscriptionInfo
onRefreshSubscription={onRefreshSubscription}
onCancelSubscription={onCancelSubscription}
/>
)}
</FullScreenDialog>
</div>
Expand Down
210 changes: 133 additions & 77 deletions src/components/Settings/Subscribe/Subscribe.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ export class SubscribeContainer extends PureComponent {

handleRefreshSubscription = () => {
const { updateIsSubscribed, updatePlans } = this.props;
//window.CdvPurchase.store.restorePurchases();
updateIsSubscribed();
updatePlans();
};

handleCancelSubscription = () => {};
martinbedouret marked this conversation as resolved.
Show resolved Hide resolved

handleError = e => {
const { updateSubscriptionError, updateSubscription } = this.props;

Expand All @@ -64,7 +65,7 @@ export class SubscribeContainer extends PureComponent {
updateSubscription({
isSubscribed: false,
expiryDate: null,
androidSubscriptionState: NOT_SUBSCRIBED
status: NOT_SUBSCRIBED
});

setTimeout(() => {
Expand All @@ -76,7 +77,58 @@ export class SubscribeContainer extends PureComponent {
}, 3000);
};

handleSubscribe = product => async event => {
handleSubscribeCancel = () => {
const { updateSubscription } = this.props;
updateSubscription({
isSubscribed: false,
expiryDate: null,
status: NOT_SUBSCRIBED,
ownedProduct: ''
});
};

handlePaypalApprove = async (product, data) => {
const { updateSubscription } = this.props;
const {
facilitatorAccessToken,
orderID,
paymentSource,
subscriptionID
} = data;
const transaction = {
className: 'Transaction',
subscriptionId: subscriptionID,
transactionId: subscriptionID,
state: 'approved',
products: [product],
platform: paymentSource,
nativePurchase: '',
purchaseId: orderID,
purchaseDate: '',
isPending: false,
subscriptionState: ACTIVE,
expiryDate: '',
facilitatorAccessToken
};
try {
const res = await API.postTransaction(transaction);
if (!res.ok) throw res;
const subscriber = await API.getSubscriber();
updateSubscription({
ownedProduct: product,
status: ACTIVE,
isInFreeCountry: false,
isOnTrialPeriod: false,
isSubscribed: true,
expiryDate: subscriber.transaction.expiryDate
});
} catch (err) {
console.error('Cannot subscribe product. Error: ', err.message);
this.handleError(err);
}
};

handleSubscribe = async product => {
const {
intl,
user,
Expand All @@ -86,40 +138,37 @@ export class SubscribeContainer extends PureComponent {
updateSubscription,
subscription
} = this.props;
if (isAndroid()) {
if (
(isLogged &&
product &&
subscription.androidSubscriptionState === NOT_SUBSCRIBED) ||
subscription.androidSubscriptionState === EXPIRED
) {
const newProduct = {
title: formatTitle(product.title),
billingPeriod: product.billingPeriod,
price: product.price,
tag: product.tag,
subscriptionId: product.subscriptionId
};
const apiProduct = {
product: {
...newProduct
}
};
if (
(isLogged && product && subscription.status === NOT_SUBSCRIBED) ||
subscription.status === EXPIRED
) {
const newProduct = {
title: formatTitle(product.title),
billingPeriod: product.billingPeriod,
price: product.price,
tag: product.tag,
subscriptionId: product.subscriptionId
};
const apiProduct = {
product: {
...newProduct
}
};

updateSubscription({
isSubscribed: false,
expiryDate: null,
androidSubscriptionState: PROCCESING,
ownedProduct: ''
});
updateSubscription({
isSubscribed: false,
expiryDate: null,
status: PROCCESING,
ownedProduct: ''
});

let localReceipts = '';
let offers, offer;
if (isAndroid()) {
const prod = await window.CdvPurchase.store.products[0];
const localReceipts = window.CdvPurchase.store.findInLocalReceipts(
prod
);
localReceipts = window.CdvPurchase.store.findInLocalReceipts(prod);

// get offer from the plugin
let offers, offer;
try {
await window.CdvPurchase.store.update();
offers = prod.offers;
Expand All @@ -129,73 +178,77 @@ export class SubscribeContainer extends PureComponent {
this.handleError(err);
return;
}
}

try {
// update the api
const subscriber = await API.getSubscriber(user.id);
updateSubscriberId(subscriber._id);
try {
// update the api
const subscriber = await API.getSubscriber(user.id);
updateSubscriberId(subscriber._id);

// check if current subscriber already bought in this device
if (
localReceipts &&
localReceipts.nativePurchase?.orderId !==
subscriber.transaction?.transactionId
) {
this.handleError({
code: '0001',
message: intl.formatMessage(messages.googleAccountAlreadyOwns)
});
return;
}
await API.updateSubscriber(apiProduct);
// check if current subscriber already bought in this device
if (
localReceipts &&
localReceipts.nativePurchase?.orderId !==
subscriber.transaction?.transactionId
) {
this.handleError({
code: '0001',
message: intl.formatMessage(messages.googleAccountAlreadyOwns)
});
return;
}
await API.updateSubscriber(apiProduct);

// proceed with the purchase
// proceed with the purchase
if (isAndroid()) {
const order = await window.CdvPurchase.store.order(offer);
if (order && order.isError) throw order;
updateSubscription({
ownedProduct: product,
androidSubscriptionState: ACTIVE,
status: ACTIVE,
isInFreeCountry: false,
isOnTrialPeriod: false,
isSubscribed: true
});
} catch (err) {
if (err.response?.data.error === 'subscriber not found') {
// check if current subscriber already bought in this device
if (localReceipts) {
this.handleError({
code: '0001',
message: intl.formatMessage(messages.googleAccountAlreadyOwns)
});
return;
}
try {
const newSubscriber = {
userId: user.id,
country: location.countryCode || 'Not localized',
status: NOT_SUBSCRIBED,
...apiProduct
};
const res = await API.createSubscriber(newSubscriber);
updateSubscriberId(res._id);
}
} catch (err) {
if (err.response?.data.error === 'subscriber not found') {
// check if current subscriber already bought in this device
if (localReceipts) {
this.handleError({
code: '0001',
message: intl.formatMessage(messages.googleAccountAlreadyOwns)
});
return;
}
try {
const newSubscriber = {
userId: user.id,
country: location.countryCode || 'Not localized',
status: NOT_SUBSCRIBED,
...apiProduct
};
const res = await API.createSubscriber(newSubscriber);
updateSubscriberId(res._id);
if (isAndroid()) {
const order = await window.CdvPurchase.store.order(offer);
if (order && order.isError) throw order;
updateSubscription({
ownedProduct: product,
androidSubscriptionState: ACTIVE,
status: ACTIVE,
isInFreeCountry: false,
isOnTrialPeriod: false,
isSubscribed: true
});
} catch (err) {
console.error('Cannot subscribe product. Error: ', err.message);
this.handleError(err);
}
return;
} catch (err) {
console.error('Cannot subscribe product. Error: ', err.message);
this.handleError(err);
}
console.error('Cannot subscribe product. Error: ', err.message);
this.handleError(err);
return;
}
console.error('Cannot subscribe product. Error: ', err.message);
this.handleError(err);
}
}
};
Expand All @@ -212,6 +265,9 @@ export class SubscribeContainer extends PureComponent {
subscription={this.props.subscription}
updateSubscriberId={this.props.updateSubscriberId}
onRefreshSubscription={this.handleRefreshSubscription}
onSubscribeCancel={this.handleSubscribeCancel}
onPaypalApprove={this.handlePaypalApprove}
onCancelSubscription={this.handleCancelSubscription}
/>
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/Settings/Subscribe/Subscribe.messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export default defineMessages({
id: 'cboard.components.Settings.Subscribe.manageSubscription',
defaultMessage: 'Manage Subscription'
},
cancelSubscription: {
id: 'cboard.components.Settings.Subscribe.cancelSubscription',
defaultMessage: 'Cancel Subscription'
},
planAmount: {
id: 'cboard.components.Settings.Subscribe.planAmount',
defaultMessage: 'Plan amount:'
Expand Down
Loading