added redirect to payments page
This commit is contained in:
parent
34380780f6
commit
6d8205febf
|
@ -1,12 +1,12 @@
|
|||
import { getEndpoint } from 'utils/common/apiUtil';
|
||||
import { getEndpoint, getPaymentsUrl } from 'utils/common/apiUtil';
|
||||
import { getStripePublishableKey, getToken } from 'utils/common/key';
|
||||
import { checkConnectivity, runningInBrowser } from 'utils/common/';
|
||||
import { setData, LS_KEYS } from 'utils/storage/localStorage';
|
||||
import { convertToHumanReadable } from 'utils/billingUtil';
|
||||
import { loadStripe, Stripe } from '@stripe/stripe-js';
|
||||
import { SUBSCRIPTION_VERIFICATION_ERROR } from 'utils/common/errorUtil';
|
||||
import HTTPService from './HTTPService';
|
||||
import { logError } from 'utils/sentry';
|
||||
import { getPaymentToken } from './userService';
|
||||
|
||||
const ENDPOINT = getEndpoint();
|
||||
|
||||
|
@ -15,6 +15,10 @@ export enum PAYMENT_INTENT_STATUS {
|
|||
REQUIRE_ACTION = 'requires_action',
|
||||
REQUIRE_PAYMENT_METHOD = 'requires_payment_method',
|
||||
}
|
||||
enum PaymentActionType{
|
||||
Buy='buy',
|
||||
Update='update'
|
||||
}
|
||||
export interface Subscription {
|
||||
id: number;
|
||||
userID: number;
|
||||
|
@ -90,10 +94,12 @@ class billingService {
|
|||
|
||||
public async buyPaidSubscription(productID) {
|
||||
try {
|
||||
const response = await this.createCheckoutSession(productID);
|
||||
await this.stripe.redirectToCheckout({
|
||||
sessionId: response.data.sessionID,
|
||||
});
|
||||
// const response = await this.createCheckoutSession(productID);
|
||||
// await this.stripe.redirectToCheckout({
|
||||
// sessionId: response.data.sessionID,
|
||||
// });
|
||||
const paymentToken =await getPaymentToken();
|
||||
await this.redirectToPayments(paymentToken, productID, PaymentActionType.Buy);
|
||||
} catch (e) {
|
||||
logError(e, 'unable to buy subscription');
|
||||
throw e;
|
||||
|
@ -102,46 +108,48 @@ class billingService {
|
|||
|
||||
public async updateSubscription(productID) {
|
||||
try {
|
||||
const response = await HTTPService.post(
|
||||
`${ENDPOINT}/billing/stripe/update-subscription`,
|
||||
{
|
||||
productID,
|
||||
},
|
||||
null,
|
||||
{
|
||||
'X-Auth-Token': getToken(),
|
||||
},
|
||||
);
|
||||
const { result } = response.data;
|
||||
switch (result.status) {
|
||||
case PAYMENT_INTENT_STATUS.SUCCESS:
|
||||
// subscription updated successfully
|
||||
// no-op required
|
||||
break;
|
||||
case PAYMENT_INTENT_STATUS.REQUIRE_PAYMENT_METHOD:
|
||||
throw new Error(
|
||||
PAYMENT_INTENT_STATUS.REQUIRE_PAYMENT_METHOD,
|
||||
);
|
||||
case PAYMENT_INTENT_STATUS.REQUIRE_ACTION:
|
||||
{
|
||||
const { error } = await this.stripe.confirmCardPayment(
|
||||
result.clientSecret,
|
||||
);
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
// const response = await HTTPService.post(
|
||||
// `${ENDPOINT}/billing/stripe/update-subscription`,
|
||||
// {
|
||||
// productID,
|
||||
// },
|
||||
// null,
|
||||
// {
|
||||
// 'X-Auth-Token': getToken(),
|
||||
// },
|
||||
// );
|
||||
// const { result } = response.data;
|
||||
// switch (result.status) {
|
||||
// case PAYMENT_INTENT_STATUS.SUCCESS:
|
||||
// // subscription updated successfully
|
||||
// // no-op required
|
||||
// break;
|
||||
// case PAYMENT_INTENT_STATUS.REQUIRE_PAYMENT_METHOD:
|
||||
// throw new Error(
|
||||
// PAYMENT_INTENT_STATUS.REQUIRE_PAYMENT_METHOD,
|
||||
// );
|
||||
// case PAYMENT_INTENT_STATUS.REQUIRE_ACTION:
|
||||
// {
|
||||
// const { error } = await this.stripe.confirmCardPayment(
|
||||
// result.clientSecret,
|
||||
// );
|
||||
// if (error) {
|
||||
// throw error;
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
const paymentToken =await getPaymentToken();
|
||||
await this.redirectToPayments(paymentToken, productID, PaymentActionType.Update);
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
logError(e, 'subscription update failed');
|
||||
throw e;
|
||||
}
|
||||
try {
|
||||
await this.verifySubscription();
|
||||
} catch (e) {
|
||||
throw new Error(SUBSCRIPTION_VERIFICATION_ERROR);
|
||||
}
|
||||
// try {
|
||||
// await this.verifySubscription();
|
||||
// } catch (e) {
|
||||
// throw new Error(SUBSCRIPTION_VERIFICATION_ERROR);
|
||||
// }
|
||||
}
|
||||
|
||||
public async cancelSubscription() {
|
||||
|
@ -217,6 +225,15 @@ class billingService {
|
|||
}
|
||||
}
|
||||
|
||||
public async redirectToPayments(paymentToken:string, productID:string, action:string) {
|
||||
try {
|
||||
window.location.href =`${getPaymentsUrl()}?productID=${productID}&payment-token=${paymentToken}&action=${action}`;
|
||||
} catch (e) {
|
||||
logError(e, 'unable to get payments url');
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async redirectToCustomerPortal() {
|
||||
try {
|
||||
const response = await HTTPService.get(
|
||||
|
|
|
@ -72,6 +72,19 @@ export const getPublicKey = async (email: string) => {
|
|||
return resp.data.publicKey;
|
||||
};
|
||||
|
||||
export const getPaymentToken = async () => {
|
||||
const token = getToken();
|
||||
|
||||
const resp = await HTTPService.get(
|
||||
`${ENDPOINT}/users/payment-token`,
|
||||
null,
|
||||
{
|
||||
'X-Auth-Token': token,
|
||||
},
|
||||
);
|
||||
return resp.data['paymentToken'];
|
||||
};
|
||||
|
||||
export const verifyOtt = (email: string, ott: string) => HTTPService.post(`${ENDPOINT}/users/verify-email`, { email, ott });
|
||||
|
||||
export const putAttributes = (token: string, keyAttributes: KeyAttributes) => HTTPService.put(
|
||||
|
|
|
@ -5,14 +5,14 @@ export const getEndpoint = () => {
|
|||
|
||||
export const getFileUrl = (id: number) => {
|
||||
if (process.env.NEXT_PUBLIC_ENTE_ENDPOINT !== undefined) {
|
||||
return `${process.env.NEXT_PUBLIC_ENTE_ENDPOINT}/files/download/${id}` ?? 'https://api.ente.io';
|
||||
return `${process.env.NEXT_PUBLIC_ENTE_ENDPOINT}/files/download/${id}` ?? `https://api.ente.io/files/download/${id}`;
|
||||
}
|
||||
return `https://files.ente.workers.dev/?fileID=${id}`;
|
||||
};
|
||||
|
||||
export const getThumbnailUrl = (id: number) => {
|
||||
if (process.env.NEXT_PUBLIC_ENTE_ENDPOINT !== undefined) {
|
||||
return `${process.env.NEXT_PUBLIC_ENTE_ENDPOINT}/files/preview/${id}` ?? 'https://api.ente.io';
|
||||
return `${process.env.NEXT_PUBLIC_ENTE_ENDPOINT}/files/preview/${id}` ?? `https://api.ente.io/files/preview/${id}`;
|
||||
}
|
||||
return `https://thumbnails.ente.workers.dev/?fileID=${id}`;
|
||||
};
|
||||
|
@ -20,3 +20,10 @@ export const getThumbnailUrl = (id: number) => {
|
|||
export const getSentryTunnelUrl = () => {
|
||||
return `https://sentry-reporter.ente.workers.dev`;
|
||||
};
|
||||
|
||||
export const getPaymentsUrl = () => {
|
||||
if (process.env.NEXT_PUBLIC_ENTE_ENDPOINT !== undefined) {
|
||||
return process.env.NEXT_PUBLIC_ENTE_PAYMENT_ENDPOINT;
|
||||
}
|
||||
return `https://payments.ente.io`;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue