-
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
1 parent
1766bfc
commit 7a4b30d
Showing
2 changed files
with
180 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,53 @@ | ||
import "https://deno.land/std@0.224.0/dotenv/load.ts"; | ||
import { Client, InitiatePaymentSessionRequest } from "../src/mod.ts"; | ||
|
||
// First, get your API keys from https://portal.vipps.no/ | ||
// Here we assume they are stored in a .env file, see .env.example | ||
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") || ""; | ||
|
||
// Create a client | ||
const client = Client({ | ||
merchantSerialNumber, | ||
subscriptionKey, | ||
systemName: "My cool e-commerce system", | ||
systemVersion: "1.2.3", | ||
pluginVersion: "1.2.3", | ||
pluginName: "My cool plugin", | ||
useTestMode: true, | ||
retryRequests: false, | ||
}); | ||
|
||
//Create a checkout session | ||
const request: InitiatePaymentSessionRequest = { | ||
type: "PAYMENT", | ||
merchantInfo: { | ||
callbackUrl: "https://example.com/callbackUrl", | ||
returnUrl: "https://example.com/fallbackPage", | ||
callbackAuthorizationToken: "1234", | ||
}, | ||
transaction: { | ||
amount: { | ||
currency: "NOK", | ||
value: 1000, // This value equals 10 NOK | ||
}, | ||
paymentDescription: "One pair of socks.", | ||
}, | ||
}; | ||
|
||
const checkout = await client.checkout.create(clientId, clientSecret, request); | ||
|
||
// Check if the checkout session was created successfully | ||
if (!checkout.ok) { | ||
console.error("😟 Error creating checkout session 😟"); | ||
console.log(checkout.error); | ||
Deno.exit(1); | ||
} | ||
|
||
console.log("🎉 Checkout Session created successfully!"); | ||
|
||
const pollingUrl = checkout.data.pollingUrl; | ||
console.log("🔗 URL to poll for session information:", pollingUrl); |
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,127 @@ | ||
import { checkoutRequestFactory } from "../src/apis/checkout.ts"; | ||
import type { InitiatePaymentSessionRequest } from "../src/apis/external_types.ts"; | ||
import { InitiateSubscriptionSessionRequest } from "../src/mod.ts"; | ||
import { | ||
assert, | ||
assertEquals, | ||
assertExists, | ||
assertNotEquals, | ||
} from "./test_deps.ts"; | ||
|
||
Deno.test("create - should return the correct request data", () => { | ||
const client_id = "your_client_id"; | ||
const client_secret = "your_client_secret"; | ||
const body: InitiatePaymentSessionRequest = { | ||
type: "PAYMENT", | ||
merchantInfo: { | ||
callbackUrl: "https://example.com/vipps/callbacks-for-checkout", | ||
returnUrl: | ||
"https://example.com/vipps/fallback-result-page-for-both-success-and-failure", | ||
callbackAuthorizationToken: "1234", | ||
}, | ||
transaction: { | ||
amount: { | ||
currency: "NOK", | ||
value: 1000, | ||
}, | ||
reference: "foobar", | ||
paymentDescription: "One pair of socks.", | ||
}, | ||
}; | ||
|
||
const requestData = checkoutRequestFactory.create( | ||
client_id, | ||
client_secret, | ||
body, | ||
); | ||
|
||
assertEquals(requestData.url, "/checkout/v3/session"); | ||
assertEquals(requestData.method, "POST"); | ||
assertEquals(requestData.body, body); | ||
assertNotEquals(requestData.additionalHeaders, undefined); | ||
const headers = requestData.additionalHeaders as Record<string, string>; | ||
assertEquals("client_id" in headers, true); | ||
}); | ||
|
||
Deno.test("create - should fill in missing properties for payment requests", () => { | ||
const client_id = "your_client_id"; | ||
const client_secret = "your_client_secret"; | ||
const body: InitiatePaymentSessionRequest = { | ||
type: "PAYMENT", | ||
merchantInfo: { | ||
callbackUrl: "https://example.com/vipps/callbacks-for-checkout", | ||
returnUrl: | ||
"https://example.com/vipps/fallback-result-page-for-both-success-and-failure", | ||
callbackAuthorizationToken: "1234", | ||
}, | ||
transaction: { | ||
amount: { | ||
currency: "NOK", | ||
value: 1000, | ||
}, | ||
paymentDescription: "One pair of socks.", | ||
}, | ||
}; | ||
|
||
const requestData = checkoutRequestFactory.create( | ||
client_id, | ||
client_secret, | ||
body, | ||
); | ||
|
||
assertExists(requestData.body); | ||
assert("transaction" in requestData.body); | ||
assert( | ||
"reference" in (requestData.body.transaction as Record<string, unknown>), | ||
); | ||
}); | ||
|
||
Deno.test("create - should fill in missing properties for subscription requests", () => { | ||
const client_id = "your_client_id"; | ||
const client_secret = "your_client_secret"; | ||
const body: InitiateSubscriptionSessionRequest = { | ||
type: "SUBSCRIPTION", | ||
merchantInfo: { | ||
callbackUrl: "https://example.com/vipps/callbacks-for-checkout", | ||
returnUrl: | ||
"https://example.com/vipps/fallback-result-page-for-both-success-and-failure", | ||
callbackAuthorizationToken: "1234", | ||
}, | ||
subscription: { | ||
amount: { | ||
currency: "NOK", | ||
value: 1000, | ||
}, | ||
interval: { | ||
count: 1, | ||
unit: "MONTH", | ||
}, | ||
productName: "Socks", | ||
merchantAgreementUrl: "https://example.com/vipps/merchant-agreement", | ||
}, | ||
}; | ||
|
||
const requestData = checkoutRequestFactory.create( | ||
client_id, | ||
client_secret, | ||
body, | ||
); | ||
|
||
assertExists(requestData.body); | ||
assert("reference" in (requestData.body as Record<string, unknown>)); | ||
}); | ||
|
||
Deno.test("info - should return the correct request data", () => { | ||
const client_id = "your_client_id"; | ||
const client_secret = "your_client_secret"; | ||
const reference = "your_reference"; | ||
|
||
const requestData = checkoutRequestFactory.info( | ||
client_id, | ||
client_secret, | ||
reference, | ||
); | ||
|
||
assertEquals(requestData.url, `/checkout/v3/session/${reference}`); | ||
assertEquals(requestData.method, "GET"); | ||
}); |