-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
// ); | ||
// } | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Oops, something went wrong.