Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion examples/3-minimal-with-verify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@code-wallet/client": "^1.0.0",
"@code-wallet/client": "^1.3.1",
"body-parser": "^1.20.2",
"ejs": "^3.1.9",
"express": "^4.18.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/4-minimal-with-webhook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@code-wallet/client": "^1.0.0",
"@code-wallet/client": "^1.3.1",
"body-parser": "^1.20.2",
"ejs": "^3.1.9",
"express": "^4.18.2"
Expand Down
4 changes: 2 additions & 2 deletions examples/5-example-purchase-flow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@code-wallet/client": "^1.0.0",
"@code-wallet/library": "^1.0.0",
"@code-wallet/client": "^2.0.2",
"@code-wallet/library": "^1.3.1",
"@markdoc/markdoc": "^0.3.2",
"body-parser": "^1.20.2",
"dotenv": "^16.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@code-wallet/client": "^1.1.2",
"@code-wallet/client": "^1.3.1",
"body-parser": "^1.20.2",
"ejs": "^3.1.9",
"express": "^4.18.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ app.get('/success/:id', async (req, res) => {
// Get the payment intent id from the URL
const intent = req.params.id;

// Get the status of the payment intent and the user's public key
const status = await code.paymentIntents.getStatus({ intent });
try {
// Get the status of the payment intent
const status = await code.getStatus({ intent });
const user = await code.getUserId({ intent, verifier });

// Render the success page with the intent id and status
res.render('success', { intent, status });
// Render the success page with the intent id and status
res.render('success', { intent, status, user });
} catch (e) {
console.log('error', e);
res.json({ error: e.message });
}
});

// This is a JSON file that Code will look for when verifying your domain. It
Expand Down Expand Up @@ -64,6 +70,7 @@ app.post('/create-intent', async (req, res) => {
res.send({ clientSecret });
});


app.listen(port, () => {
console.log(`🚀 Minimal example listening on port ${port}`)
console.log(`http://localhost:${port}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code SDK - Example/6-minimal-with-login</title>
<title>Code SDK - Example/6-minimal-purchase-with-login</title>
</head>

<body>
Expand All @@ -16,6 +16,7 @@
const hostname = window.location.protocol + "//" + window.location.host;

const { button } = code.elements.create('button', {
mode: 'payment',
currency: 'usd',
destination: 'E8otxw1CVX9bfyddKu3ZB3BVLa4VVF9J7CTPdnUwT9jR',
amount: 0.05,
Expand All @@ -29,7 +30,7 @@
// Wait for the button to be clicked
button.on('invoke', async () => {

// Get a payment intent from our own server
// Get a payment intent clientSecret value from server.js
const res = await fetch('/create-intent', { method: 'POST' });
const { clientSecret } = await res.json();

Expand Down
17 changes: 17 additions & 0 deletions examples/6-minimal-purchase-with-login/views/success.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code SDK - Example/6-minimal-purchase-with-login</title>
</head>

<body>
<div>Intent Id: <%= intent %></div>
<div>Status: <pre><%= JSON.stringify(status, null, 2) %></pre></div>
<div>User: <pre><%= JSON.stringify(user, null, 2) %></pre></div>
</body>

</html>
22 changes: 22 additions & 0 deletions examples/7-minimal-login/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "7-minimal-with-login",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@code-wallet/client": "^2.0.6",
"body-parser": "^1.20.2",
"ejs": "^3.1.9",
"express": "^4.18.2"
},
"devDependencies": {
"@types/express": "^4.17.17"
}
}
73 changes: 73 additions & 0 deletions examples/7-minimal-login/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as code from "@code-wallet/client";
import { Keypair } from "@code-wallet/library";
import express from "express";

const port = process.env.PORT || 3080;
const hostname = process.env.HOSTNAME || 'example.com';
const app = express();
const verifier = Keypair.generate();

// Set the view engine to ejs
app.set('view engine', 'ejs');

// index page (with the login button)
app.get('/', async function(req, res) {
res.render('index', { domain: hostname, verifier: verifier.getPublicKey().toBase58() });
});

// This is a page that the user will be redirected to once the login is made.
app.get('/success/:id', async (req, res) => {
// Get the login intent id from the URL
const intent = req.params.id;

try {
// Get the status of the login intent
const status = await code.getStatus({ intent });
const user = await code.getUserId({ intent, verifier });

// Render the success page with the intent id and status
res.render('success', { intent, status, user });
} catch (e) {
console.log('error', e);
res.json({ error: e.message });
}
});

// This is a JSON file that Code will look for when verifying your domain. It
// should be publicly accessible at the root of your domain. For example:
// https://example.com/.well-known/code-payments.json
app.get('/.well-known/code-payments.json', (req, res) => {
res.json({ "public_keys": [verifier.getPublicKey().toBase58()] });
});

// Create a login intent. We're letting Code know that a login is coming
// soon and we want to be notified once it's made.
app.post('/create-intent', async (req, res) => {

const { clientSecret, id } = await code.loginIntents.create({
login: {
verifier: verifier.getPublicKey().toBase58(),

// Cannot be localhost or a subdomain. It must be a domain that you own
// and have access to. Code will verify that this domain is owned by you
// by looking for the .well-known/code-payments.json file.
domain: hostname,
},

signers: [ verifier ],
});

console.log('Created intent', id);

// The clientSecret value needs to be sent to the browser so that the browser
// can use it to setup a login with this intent instance. The client will
// use the login details along with this value to derive the same login
// intent id on its end.
res.send({ clientSecret });
});


app.listen(port, () => {
console.log(`🚀 Minimal example listening on port ${port}`)
console.log(`http://localhost:${port}`)
});
50 changes: 50 additions & 0 deletions examples/7-minimal-login/views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code SDK - Example/7-minimal-login</title>
</head>

<body>
<div id="button-container"></div>

<script type="module">
import code from 'https://js.getcode.com/v1';

const verifier = '<%= verifier %>'; // value provided by server.js
const domain = '<%= domain %>'; // value provided by server.js

const hostname = window.location.protocol + "//" + window.location.host;
const { button } = code.elements.create('button', {
mode: 'login',

login: {
verifier,
domain,
},

confirmParams: {
success: { url: `${hostname}/success/{{INTENT_ID}}`, },
cancel: { url: `${hostname}/`, },
},
});

// Wait for the button to be clicked
button.on('invoke', async () => {

// Get a payment intent clientSecret value from server.js
const res = await fetch('/create-intent', { method: 'POST' });
const { clientSecret } = await res.json();

// Update the button with the new client secret so that our server
// can be notified once the payment is complete.
button.update({ clientSecret });
})

button.mount('#button-container');
</script>
</body>

</html>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code SDK - Example/6-minimal-with-login</title>
<title>Code SDK - Example/7-minimal-login</title>
</head>

<body>
<h1>Payment Intent</h1>
<div>Intent Id: <%= intent %></div>
<div>Status: <pre><%= JSON.stringify(status, null, 2) %></pre></div>
<div>User: <pre><%= JSON.stringify(user, null, 2) %></pre></div>
</body>

</html>
Loading