Skip to content

Commit

Permalink
Remove PayPal, as it's integrated inside Stripe now.
Browse files Browse the repository at this point in the history
Current PayPal subscribers will be updated manually.
  • Loading branch information
BrunoBernardino committed Aug 31, 2023
1 parent acb1e29 commit e4f9b5c
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 238 deletions.
3 changes: 0 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ POSTGRESQL_CAFILE=""
POSTMARK_SERVER_API_TOKEN="fake"

STRIPE_API_KEY="fake"

PAYPAL_CLIENT_ID="fake"
PAYPAL_CLIENT_SECRET="fake"
2 changes: 0 additions & 2 deletions .github/workflows/cron-check-subscriptions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ jobs:
POSTGRESQL_PORT: ${{ secrets.POSTGRESQL_PORT }}
POSTGRESQL_CAFILE: ${{ secrets.POSTGRESQL_CAFILE }}
STRIPE_API_KEY: ${{ secrets.STRIPE_API_KEY }}
PAYPAL_CLIENT_ID: ${{ secrets.PAYPAL_CLIENT_ID }}
PAYPAL_CLIENT_SECRET: ${{ secrets.PAYPAL_CLIENT_SECRET }}
run: |
make crons/check-subscriptions
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ It's not compatible with Budget Zen v2 ([end-to-end encrypted via Userbase](http

Or check the [Development section below](#development).

> **NOTE:** You don't need to have emails (Postmark) and subscriptions (Stripe/PayPal) setup to have the app work. Those are only used for allowing others to automatically manage their account. You can simply make any `user.status = 'active'` and `user.subscription.expires_at = new Date('2100-01-01')` to "never" expire, in the database, directly.
> **NOTE:** You don't need to have emails (Postmark) and subscriptions (Stripe) setup to have the app work. Those are only used for allowing others to automatically manage their account. You can simply make any `user.status = 'active'` and `user.subscription.expires_at = new Date('2100-01-01')` to "never" expire, in the database, directly.
## Framework-less

Expand Down
43 changes: 0 additions & 43 deletions crons/check-subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Database, { sql } from '/lib/interfaces/database.ts';
import { getSubscriptions as getStripeSubscriptions } from '/lib/providers/stripe.ts';
import { getPayments as getPaypalPayments } from '/lib/providers/paypal.ts';
import { sendSubscriptionExpiredEmail } from '/lib/providers/postmark.ts';
import { updateUser } from '/lib/data-utils.ts';
import { User } from '/lib/types.ts';
Expand All @@ -15,8 +14,6 @@ async function checkSubscriptions() {

let updatedUsers = 0;

const now = new Date();

const stripeSubscriptions = await getStripeSubscriptions();

for (const subscription of stripeSubscriptions) {
Expand Down Expand Up @@ -57,46 +54,6 @@ async function checkSubscriptions() {
}
}

const paypalPayments = await getPaypalPayments();

for (const payment of paypalPayments) {
const matchingUser = users.find((user) =>
user.email === payment.payer.payer_info.email &&
// Skip payments that aren't related to Budget Zen
payment.transactions.find((transaction) =>
transaction.soft_descriptor.toLocaleLowerCase().includes('budget zen')
)
);

if (matchingUser) {
if (!matchingUser.subscription.external.paypal) {
matchingUser.subscription.external.paypal = {
user_id: payment.payer.payer_info.payer_id,
subscription_id: payment.id,
};
}

matchingUser.subscription.expires_at = new Date(
new Date(payment.update_time).setUTCMonth(new Date(payment.update_time).getUTCMonth() + 1),
).toISOString();
matchingUser.subscription.updated_at = new Date().toISOString();

if (new Date(matchingUser.subscription.expires_at) > now) {
matchingUser.status = 'active';
} else {
if (matchingUser.status === 'active') {
await sendSubscriptionExpiredEmail(matchingUser.email);
}

matchingUser.status = 'inactive';
}

await updateUser(matchingUser);

++updatedUsers;
}
}

console.log('Updated', updatedUsers, 'users');
} catch (error) {
console.log(error);
Expand Down
101 changes: 0 additions & 101 deletions lib/providers/paypal.ts

This file was deleted.

10 changes: 0 additions & 10 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ export const PORT = Deno.env.get('PORT') || 8000;
export const STRIPE_MONTHLY_URL = 'https://buy.stripe.com/eVa01H57C3MB6CQ14s';
export const STRIPE_YEARLY_URL = 'https://buy.stripe.com/28o5m1dE896V0es8wV';
export const STRIPE_CUSTOMER_URL = 'https://billing.stripe.com/p/login/4gw15w3G9bDyfWU6oo';
export const PAYPAL_MONTHLY_URL =
`https://www.paypal.com/webapps/billing/plans/subscribe?plan_id=P-41N48210MJ2770038MQDVBLI&return_url=${
encodeURI(`${baseUrl}/pricing?paypalCheckoutId=true`)
}`;
export const PAYPAL_YEARLY_URL =
`https://www.paypal.com/webapps/billing/plans/subscribe?plan_id=P-20P504881F952811BMQDVA4Q&return_url=${
encodeURI(`${baseUrl}/pricing?paypalCheckoutId=true`)
}`;
export const PAYPAL_CUSTOMER_URL = 'https://www.paypal.com';

export interface PageContentResult {
Expand Down Expand Up @@ -81,8 +73,6 @@ function basicLayout(htmlContent: string, { currentPath, titlePrefix, descriptio
STRIPE_MONTHLY_URL: '${STRIPE_MONTHLY_URL}',
STRIPE_YEARLY_URL: '${STRIPE_YEARLY_URL}',
STRIPE_CUSTOMER_URL: '${STRIPE_CUSTOMER_URL}',
PAYPAL_MONTHLY_URL: '${PAYPAL_MONTHLY_URL}',
PAYPAL_YEARLY_URL: '${PAYPAL_YEARLY_URL}',
PAYPAL_CUSTOMER_URL: '${PAYPAL_CUSTOMER_URL}',
};
</script>
Expand Down
27 changes: 2 additions & 25 deletions pages/api/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { updateUser, validateUserAndSession } from '/lib/data-utils.ts';
import { getSubscriptions as getStripeSubscriptions } from '/lib/providers/stripe.ts';
import { getPayments as getPaypalPayments } from '/lib/providers/paypal.ts';

export async function pageAction(request: Request) {
if (request.method !== 'POST') {
return new Response('Not Implemented', { status: 501 });
}

const { session_id, user_id, provider }: { session_id: string; user_id: string; provider: 'paypal' | 'stripe' } =
await request.json();
const { session_id, user_id, provider }: { session_id: string; user_id: string; provider: 'stripe' } = await request
.json();

if (!session_id || !user_id) {
return new Response('Bad Request', { status: 400 });
Expand All @@ -34,28 +33,6 @@ export async function pageAction(request: Request) {
};
user.status = 'active';

await updateUser(user);
}
} else if (provider === 'paypal') {
const payments = await getPaypalPayments();

const payment = payments.find((payment) =>
payment.payer.payer_info.email === user.email &&
payment.transactions.find((transaction) => transaction.soft_descriptor.toLocaleLowerCase().includes('budget zen'))
);

if (payment) {
user.subscription.isMonthly = parseInt(payment.transactions[0].amount.total, 10) < 10;
user.subscription.updated_at = new Date().toISOString();
user.subscription.expires_at = new Date(
new Date(payment.update_time).setUTCMonth(new Date(payment.update_time).getUTCMonth() + 1),
).toISOString();
user.subscription.external.paypal = {
user_id: payment.payer.payer_info.payer_id,
subscription_id: payment.id,
};
user.status = 'active';

await updateUser(user);
}
}
Expand Down
56 changes: 5 additions & 51 deletions public/ts/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ document.addEventListener('app-loaded', async () => {

const headers = commonRequestHeaders;

const provider = urlSearchParams.get('paypalCheckoutId') ? 'paypal' : 'stripe';
const provider = 'stripe';

const body: { user_id: string; session_id: string; provider: 'stripe' | 'paypal' } = {
const body: { user_id: string; session_id: string; provider: 'stripe' } = {
user_id: session.userId,
session_id: session.sessionId,
provider,
Expand Down Expand Up @@ -45,30 +45,7 @@ document.addEventListener('app-loaded', async () => {
return;
}

const { Swal } = window;

const stripeOrPayPalDialogResult = await Swal.fire({
icon: 'question',
title: 'Stripe or PayPal?',
text: 'Do you prefer paying via Stripe or PayPal?',
focusConfirm: false,
showCancelButton: true,
showDenyButton: true,
confirmButtonText: 'Stripe',
denyButtonText: 'PayPal',
cancelButtonText: 'Wait, cancel.',
});

if (
stripeOrPayPalDialogResult.isConfirmed ||
stripeOrPayPalDialogResult.isDenied
) {
if (stripeOrPayPalDialogResult.isDenied) {
window.location.href = window.app.PAYPAL_MONTHLY_URL;
} else {
window.location.href = window.app.STRIPE_MONTHLY_URL;
}
}
window.location.href = window.app.STRIPE_MONTHLY_URL;
}

async function subscribeYearly(event: Event) {
Expand All @@ -82,30 +59,7 @@ document.addEventListener('app-loaded', async () => {
return;
}

const { Swal } = window;

const stripeOrPayPalDialogResult = await Swal.fire({
icon: 'question',
title: 'Stripe or PayPal?',
text: 'Do you prefer paying via Stripe or PayPal?',
focusConfirm: false,
showCancelButton: true,
showDenyButton: true,
confirmButtonText: 'Stripe',
denyButtonText: 'PayPal',
cancelButtonText: 'Wait, cancel.',
});

if (
stripeOrPayPalDialogResult.isConfirmed ||
stripeOrPayPalDialogResult.isDenied
) {
if (stripeOrPayPalDialogResult.isDenied) {
window.location.href = window.app.PAYPAL_YEARLY_URL;
} else {
window.location.href = window.app.STRIPE_YEARLY_URL;
}
}
window.location.href = window.app.STRIPE_YEARLY_URL;
}

function getValidSubscriptionHtmlElement() {
Expand Down Expand Up @@ -136,7 +90,7 @@ document.addEventListener('app-loaded', async () => {
}

async function updateUI() {
if (urlSearchParams.get('stripeCheckoutId') || urlSearchParams.get('paypalCheckoutId')) {
if (urlSearchParams.get('stripeCheckoutId')) {
await subscriptionSuccessfull();
}

Expand Down
2 changes: 0 additions & 2 deletions public/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export interface App {
STRIPE_MONTHLY_URL: string;
STRIPE_YEARLY_URL: string;
STRIPE_CUSTOMER_URL: string;
PAYPAL_MONTHLY_URL: string;
PAYPAL_YEARLY_URL: string;
PAYPAL_CUSTOMER_URL: string;
isLoggedIn: boolean;
showLoading: () => void;
Expand Down

0 comments on commit e4f9b5c

Please sign in to comment.