Skip to content

Commit

Permalink
Messy messy
Browse files Browse the repository at this point in the history
  • Loading branch information
flinkgutt committed Jan 24, 2024
1 parent 23f5bfc commit 944d379
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 0 deletions.
103 changes: 103 additions & 0 deletions sample_code/order-management_sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { open } from "https://deno.land/x/open@v0.0.6/index.ts";
import "https://deno.land/std@0.208.0/dotenv/load.ts";
//import { Client } from "https://deno.land/x/vipps_mobilepay_sdk@0.8.0/mod.ts";
import { Client } from "../src/mod.ts"; // TODO Revert to deno.land/x on commit

// Read environment variables
const clientId = Deno.env.get("CLIENT_ID") || "";
const clientSecret = Deno.env.get("CLIENT_SECRET") || "";

const merchantSerialNumber = Deno.env.get("MERCHANT_SERIAL_NUMBER") || "";
const subscriptionKey = Deno.env.get("SUBSCRIPTION_KEY") || "";
console.log(clientId)
// Create a client
const client = Client({
merchantSerialNumber,
subscriptionKey,
useTestMode: false,
retryRequests: false,
});

// Grab a token
const accessToken = await client.auth.getToken({
clientId,
clientSecret,
subscriptionKey,
});

if (!accessToken.ok) {
console.log("Error retrieving token", accessToken.error);
Deno.exit(1);
}
const token = accessToken.data.access_token;

// Creating an ePayment order to put a receipt on
const orderReference = crypto.randomUUID();
const payment = await client.payment.create(token, {
reference: orderReference,
amount: {
currency: "NOK",
value: 100,
},
paymentMethod: {
type: "WALLET",
},
userFlow: "WEB_REDIRECT",
returnUrl: "https://example.com/",
paymentDescription: "This is my BOOMstick!",
});
if (!payment.ok) {
console.log("Error creating payment", payment.error);
Deno.exit(1);
}
console.log("We have a payment, let's open the browser...");
await open(payment.data.redirectUrl);

const shouldProceed = confirm("Complete the order, then press 'y' and hit enter.");
console.log("Aaaaaand we continue to the fun part!");
const receipt = await client.orderManagement.addReceipt(
token,
{
orderLines: [
{
name: "BOOMstick #1",
id: "sku-1",
totalAmount: 100,
totalAmountExcludingTax: 80,
totalTaxAmount: 20,
taxPercentage: 25,
},
],
bottomLine: {
currency: "NOK",
},
},
"ecom",
orderReference,
);
if (receipt.ok) {
console.log("Receipt: ", receipt);
} else {
console.log("Error adding receipt", receipt.error);
}

const fetchedReceipt = await client.orderManagement.getOrderWithCategoryAndReceipt(token, "ecom", orderReference);
console.log(fetchedReceipt);
// const addCategoryToOrderResult = await client.orderManagement
// .addCategoryToOrder(token, "ecom", orderReference, {
// category: "RECEIPT",
// imageId: null,
// orderDetailsUrl: `https://example.com/portal/receipt/${orderReference}`,
// });

// if (addCategoryToOrderResult.ok) {
// console.log("Successfully added category to order");
// console.log("Data:", addCategoryToOrderResult.data);
// } else {
// console.log(
// "Error adding category to order:",
// addCategoryToOrderResult.error,
// );
// }


75 changes: 75 additions & 0 deletions src/apis/ordermanagement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
OrderManagementAddCategoryOKResponse,
OrderManagementAddImageOKResponse,
OrderManagementAddImageRequest,
OrderManagementAddReceiptOKResponse,
OrderManagementAddReceiptRequest,
OrderManagementErrorResponse,
OrderManagementGetOrderOKResponse,
OrderManagementPaymentType,
TypedOrderManagementAddCategoryToOrderRequest,
} from "./types/ordermanagement_types.ts";
import { RequestData } from "../types.ts";

export const orderManagementRequestFactory = {
addCategoryToOrder(
token: string,
paymentType: OrderManagementPaymentType,
orderId: string,
body: TypedOrderManagementAddCategoryToOrderRequest,
): RequestData<
OrderManagementAddCategoryOKResponse,
OrderManagementErrorResponse
> {
return {
url: `/order-management/v2/${paymentType}/categories/${orderId}`,
method: "PUT",
body: body,
token,
};
},
addImage(
token: string,
body: OrderManagementAddImageRequest,
): RequestData<
OrderManagementAddImageOKResponse,
OrderManagementErrorResponse
> {
return {
url: "/order-management/v1/images",
method: "POST",
body: body,
token,
};
},
getOrderWithCategoryAndReceipt(
token: string,
paymentType: OrderManagementPaymentType,
orderId: string,
): RequestData<
OrderManagementGetOrderOKResponse,
OrderManagementErrorResponse
> {
return {
url: `/order-management/v2/${paymentType}/${orderId}`,
method: "GET",
token,
};
},
addReceipt(
token: string,
body: OrderManagementAddReceiptRequest,
paymentType: OrderManagementPaymentType,
orderId: string,
): RequestData<
OrderManagementAddReceiptOKResponse,
OrderManagementErrorResponse
> {
return {
url: `/order-management/v2/${paymentType}/receipts/${orderId}`,
method: "POST",
body: body,
token,
};
},
} as const;
4 changes: 4 additions & 0 deletions src/apis/types/common_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Currency types, allowed values are NOK, DKK and EUR
*/
export type Currency = "NOK" | "DKK" | "EUR";
Loading

0 comments on commit 944d379

Please sign in to comment.