FIX: Vipps agreement and DB agreement out of sync #42

Closed
olaven wants to merge 4 commits from revert-41-fix-vipps-crash into main
3 changed files with 37 additions and 70 deletions
Showing only changes of commit 89ad059938 - Show all commits

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
test-script.ts
tmp tmp
# Created by https://www.toptal.com/developers/gitignore/api/node,deno,macos,emacs # Created by https://www.toptal.com/developers/gitignore/api/node,deno,macos,emacs

View File

@ -7,12 +7,8 @@ import { STATUS_CODE } from "$fresh/server.ts";
const config = { const config = {
clientId: Deno.env.get("VIPPS_CLIENT_ID"), clientId: Deno.env.get("VIPPS_CLIENT_ID"),
clientSecret: Deno.env.get("VIPPS_CLIENT_SECRET"), clientSecret: Deno.env.get("VIPPS_CLIENT_SECRET"),
ocpApimSubscriptionKeyPrimary: Deno.env.get( ocpApimSubscriptionKeyPrimary: Deno.env.get("VIPPS_OCP_APIM_SUBSCRIPTION_KEY_PRIMARY"),
"VIPPS_OCP_APIM_SUBSCRIPTION_KEY_PRIMARY", ocpApimSubscriptionKeySecondary: Deno.env.get("VIPPS_OCM_APIM_SUBSCRIPTION_KEY_SECONDARY"),
),
ocpApimSubscriptionKeySecondary: Deno.env.get(
"VIPPS_OCM_APIM_SUBSCRIPTION_KEY_SECONDARY",
),
msn: Deno.env.get("VIPPS_MSN"), msn: Deno.env.get("VIPPS_MSN"),
baseUrl: Deno.env.get("VIPPS_API_BASE_URL"), baseUrl: Deno.env.get("VIPPS_API_BASE_URL"),
}; };
@ -37,8 +33,8 @@ const getAccessToken = async function () {
method: "POST", method: "POST",
// @ts-ignore // @ts-ignore
headers: { headers: {
client_id: config.clientId, "client_id": config.clientId,
client_secret: config.clientSecret, "client_secret": config.clientSecret,
...standardVippsHeaders, ...standardVippsHeaders,
}, },
}); });
@ -60,23 +56,23 @@ export const createAgreement = async function (email: string) {
...standardVippsHeaders, ...standardVippsHeaders,
}, },
body: JSON.stringify({ body: JSON.stringify({
interval: { "interval": {
unit: "MONTH", "unit": "MONTH",
count: 1, "count": 1,
}, },
initialCharge: { "initialCharge": {
amount: amount, "amount": amount,
description: "Initial charge", "description": "Initial charge",
transactionType: "DIRECT_CAPTURE", "transactionType": "DIRECT_CAPTURE",
}, },
pricing: { "pricing": {
amount: amount, "amount": amount,
currency: "NOK", "currency": "NOK",
}, },
// email is used to identify the user in the success page // email is used to identify the user in the success page
merchantRedirectUrl: `${getHostUrl()}/donations-success?email=${email}`, "merchantRedirectUrl": `${getHostUrl()}/donations-success?email=${email}`,
merchantAgreementUrl: `${getHostUrl()}`, "merchantAgreementUrl": `${getHostUrl()}`,
productName: "Månedlig støtte til NRSS", "productName": "Månedlig støtte til NRSS",
}), }),
}); });
@ -93,23 +89,15 @@ export const createAgreement = async function (email: string) {
} }
}; };
export const getAgreement = async function (agreementId: string): Promise< export const getAgreement = async function (agreementId: string) {
| {
status: "PENDING" | "ACTIVE" | "STOPPED" | "EXPIRED";
}
| Error
> {
const token = await getAccessToken(); const token = await getAccessToken();
const response = await fetch( const response = await fetch(`${config.baseUrl}/recurring/v3/agreements/${agreementId}`, {
`${config.baseUrl}/recurring/v3/agreements/${agreementId}`,
{
// @ts-ignore // @ts-ignore
headers: { headers: {
authorization: `Bearer ${token}`, authorization: `Bearer ${token}`,
...standardVippsHeaders, ...standardVippsHeaders,
}, },
}, });
);
if (response.status === STATUS_CODE.OK) { if (response.status === STATUS_CODE.OK) {
return response.json(); return response.json();
@ -122,9 +110,7 @@ export const getAgreement = async function (agreementId: string): Promise<
export const cancelAgreement = async function (agreementId: string) { export const cancelAgreement = async function (agreementId: string) {
const token = await getAccessToken(); const token = await getAccessToken();
const response = await fetch( const response = await fetch(`${config.baseUrl}/recurring/v3/agreements/${agreementId}`, {
`${config.baseUrl}/recurring/v3/agreements/${agreementId}`,
{
method: "PATCH", method: "PATCH",
// @ts-ignore // @ts-ignore
headers: { headers: {
@ -133,10 +119,9 @@ export const cancelAgreement = async function (agreementId: string) {
...standardVippsHeaders, ...standardVippsHeaders,
}, },
body: JSON.stringify({ body: JSON.stringify({
status: "STOPPED", "status": "STOPPED",
}), }),
}, });
);
if (response.status === STATUS_CODE.NoContent) { if (response.status === STATUS_CODE.NoContent) {
return true; return true;

View File

@ -1,7 +1,6 @@
import { getHostUrl, validateEmail } from "../../../lib/utils.ts"; import { getHostUrl, validateEmail } from "../../../lib/utils.ts";
import { storage } from "../../../lib/storage.ts"; import { storage } from "../../../lib/storage.ts";
import { createAgreement } from "../../../lib/vipps/vipps.ts"; import { createAgreement } from "../../../lib/vipps/vipps.ts";
import { getAgreement } from "../../../lib/vipps/vipps.ts";
export const handler = async function (req: Request): Promise<Response> { export const handler = async function (req: Request): Promise<Response> {
const email = new URLSearchParams(req.url.split("?")[1] || "").get("email"); const email = new URLSearchParams(req.url.split("?")[1] || "").get("email");
@ -22,28 +21,12 @@ export const handler = async function (req: Request): Promise<Response> {
const hasActiveAgreement = existingAgreement && const hasActiveAgreement = existingAgreement &&
existingAgreement.validAt !== null && existingAgreement.validAt !== null &&
existingAgreement.revokedAt === null; existingAgreement.revokedAt === null;
const agreementInVipps = existingAgreement && (await getAgreement(existingAgreement.agreementId));
if ( if (
agreementInVipps &&
!(agreementInVipps instanceof Error) &&
agreementInVipps.status !== "ACTIVE" &&
hasActiveAgreement hasActiveAgreement
) { ) {
// Somehow the Vipps agreement is not active in our system anymore.
// We should reflect the new status in our system.
await storage.writeVippsAgreement({
...existingAgreement,
revokedAt: new Date(),
});
} else {
// i.e. the agreement in our system was in sync with Vipps.
if (hasActiveAgreement) {
console.error("Agreement already exists", email); console.error("Agreement already exists", email);
return Response.redirect(errorPage); return Response.redirect(errorPage);
} }
}
const agreement = await createAgreement(email); const agreement = await createAgreement(email);
if (agreement instanceof Error) { if (agreement instanceof Error) {