Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean #1

Merged
merged 10 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
API_KEY=
# API key of your environment
PRIMER_API_KEY=

61 changes: 48 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
# Server Deno Hono
![Primer Banner](./images/primer-banner.png)

In this example we build a server to integrate with Primer's API.
# 🦄 Primer Example Backend

It uses [Deno](https://deno.land) as the runtime and [Hono](https://hono.dev) as the HTTP server framework, but feel free to build your server however you'd like.
This project is a very simple companion backend to the example apps of [Primer](https://primer.io) Universal Checkout:

## Running on your browser
- [Example Web apps](#)
- [Example iOS apps](https://github.com/primer-io/checkout-examples-ios)
- [Example Android apps](https://github.com/primer-io/checkout-examples-android)

[Click here to immediately launch it on your browser](https://stackblitz.com/github/primer-io/checkout-web/tree/main/examples/server-deno-hono).
---

Once it's open, make sure to:
_This server uses [Deno](https://deno.land) as the runtime and [Hono](https://hono.dev) as the HTTP server framework._

1. Create a new file `.env` and copy contents from `.env.example` into it
2. Get an `API_KEY` from your Primer Dashboard and paste it there
- Example: `API_KEY=1234-foo-bar-4321`
# 🚀 Get started

## Running locally
## Pre-requisites

- A Primer sandbox account 👤

- An API key for your Sandbox 🔑 <br /> You can grab your API key or create a new one from the [Primer Dashboard](https://sandbox-dashboard.primer.io/developers/apiKeys).

rabelloo marked this conversation as resolved.
Show resolved Hide resolved
## Deploy on Glitch ⚡️

_We recommend using Glitch to quickly spin up a new instance of your server for free._

1. First, click on this button to open the project in Glitch and start the server.

[![remix With Glitch](https://cdn.glitch.com/2703baf2-b643-4da7-ab91-7ee2a2d00b5b%2Fremix-button-v2.svg?v=1622676640618)](https://glitch.com/edit/#!/import/github/primer-io/example-web-checkout)

2. On Glitch, open the file `.env`. <br /> Set the environment variable `PRIMER_API_KEY` to your Primer sandbox API key.

rabelloo marked this conversation as resolved.
Show resolved Hide resolved
3. On Glitch, grab the URL of your Glitch instance. <br /> It should look like `https://xxx-yyy-zzz.glitch.me`.

4. Paste your Glitch instance URL in the example project.

## Run it locally

1. First, make sure [Deno](https://deno.land) is installed on your machine.
2. Clone this repository:

1. Install [Deno](https://deno.land)
2. Follow the same instructions described on the section above about the `API_KEY`
3. Execute the following script on a terminal window:
```sh
git clone https://github.com/primer-io/example-backend.git

cd ./example-backend
```

3. Clone the file `.env.example` and call it `.env`. Set the environment variable `PRIMER_API_KEY` to your Primer sandbox API key.

rabelloo marked this conversation as resolved.
Show resolved Hide resolved
4. Execute the following script on a terminal window:
```sh
deno task start
```

# 🤖 Capabilities

- `GET /` <br /> Health check

- `POST /client-session` <br /> Create a client session
Binary file removed bun.lockb
Binary file not shown.
25 changes: 25 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added images/primer-banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/api/const.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const primerHeaders = {
accept: "application/json",
"content-type": "application/json",
"X-API-KEY": Deno.env.get("API_KEY"),
"X-API-KEY": Deno.env.get("PRIMER_API_KEY"),
"X-API-VERSION": "2.2",
};

Expand Down
22 changes: 0 additions & 22 deletions src/api/createClientSession.ts

This file was deleted.

72 changes: 62 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,77 @@
import { Hono } from "hono/mod.ts";
import { cors } from "hono/middleware.ts";

import "std/dotenv/load.ts";
import { createClientSession } from "./api/createClientSession.ts";
import { post } from "./utils/post.ts";
import { primerApiUrl, primerHeaders } from "./api/const.ts";

const app = new Hono();

app.use("/*", cors());
rabelloo marked this conversation as resolved.
Show resolved Hide resolved

app.get("/", (c) =>
c.text(["Available endpoints:", "", " POST /client-session"].join("\n")),
c.text(["Available endpoints:", "", " POST /client-session"].join("\n"))
);

///////////////////////////////////////////
// Create a client session and send back the client token
///////////////////////////////////////////
rabelloo marked this conversation as resolved.
Show resolved Hide resolved

app.post("/client-session", async (c) => {
const { amount, currencyCode } = (await c.req.json()) as {
amount: number;
currencyCode: string;
};
const res = await post<ClientSession>(
`${primerApiUrl}/client-session`,

/* ✨ Feel free to update this 👇 */
rabelloo marked this conversation as resolved.
Show resolved Hide resolved
{
orderId: crypto.randomUUID(),

order: {
// Line items for this session
// If your checkout does not have line items:
// > Pass a single line item with the total amount!
lineItems: [
{
itemId: "shoes-123",
description: "Some nice shoes!",
amount: 2500, // Amount should be in minor units!
rabelloo marked this conversation as resolved.
Show resolved Hide resolved
quantity: 1,
},
],
},

const res = await createClientSession({
amount,
currencyCode,
});
currencyCode: "GBP",

// emailAddress and billingAddress are required for 3DS
customer: {
emailAddress: "[email protected]",
mobileNumber: "+6588889999",
firstName: "John",
lastName: "Smith",
billingAddress: {
firstName: "John",
lastName: "Smith",
postalCode: "CB94BQ",
addressLine1: "47A",
countryCode: "CL",
city: "Cambridge",
state: "Cambridgeshire",
},
},
},
/* */
rabelloo marked this conversation as resolved.
Show resolved Hide resolved

primerHeaders
);

return c.json(res);
});

type ClientSession = {
clientToken: string;
rabelloo marked this conversation as resolved.
Show resolved Hide resolved
};

///////////////////////////////////////////
// Serve
///////////////////////////////////////////

await Deno.serve(app.fetch);
Loading