Your own website
Checkout API
Create the checkout from your own server, send the buyer to CraftPay to pay, and get them back on your site when the payment is done.
How it works
- 1The buyer picks packages on your website.
- 2Your server calls POST
/v1/checkoutswith the packages, the Minecraft name and yoursuccessUrl. - 3You redirect the buyer to the
urlyou get back. They pay with card or PayPal on your store's checkout. - 4When the payment is confirmed, CraftPay sends the buyer to your
successUrlwithorder_idandcheckout_idadded. - 5The plugin delivers the packages on your server – exactly like a normal purchase.
1. Create the checkout
Do this on your server – never in the browser, because it needs your secret API key.
Node.js
// POST /buy on your own server
app.post("/buy", async (req, res) => {
const r = await fetch("https://api.craftpay.net/v1/checkouts", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CRAFTPAY_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
items: [{ packageId: req.body.packageId, quantity: 1 }],
player: req.body.player,
successUrl: "https://myserver.com/thanks",
cancelUrl: "https://myserver.com/shop",
}),
});
const { data, error } = await r.json();
if (error) return res.status(400).send(error.message);
res.redirect(303, data.url); // the buyer pays on CraftPay
});PHP
$ch = curl_init("https://api.craftpay.net/v1/checkouts");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("CRAFTPAY_KEY"),
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => json_encode([
"items" => [["packageId" => $_POST["packageId"], "quantity" => 1]],
"player" => $_POST["player"],
"successUrl" => "https://myserver.com/thanks",
]),
]);
$checkout = json_decode(curl_exec($ch), true)["data"];
header("Location: " . $checkout["url"], true, 303);2. When the buyer comes back
The buyer lands on your successUrl like this:
https://myserver.com/thanks?order_id=cm3kb1z7e0009&checkout_id=cs_Qm8v2kX4pLr9TzW1nBc7HdYe0aFg3. Check that it's paid
Anyone can type an address, so don't trust the link alone. Ask the API: status is complete when the order is paid.
Node.js
app.get("/thanks", async (req, res) => {
const r = await fetch(`https://api.craftpay.net/v1/checkouts/${req.query.checkout_id}`, {
headers: { Authorization: `Bearer ${process.env.CRAFTPAY_KEY}` },
});
const { data } = await r.json();
if (data?.status !== "complete") return res.send("Waiting for the payment…");
res.send(`Thanks! Order ${data.orderId} is on its way to ${data.player}.`);
});What the buyer can't change
- The packages and quantities you sent.
- The Minecraft name, if you sent player.
- The discount code, if you sent coupon.
Prices always come from your packages in CraftPay. The buyer can still add a gift card, and must fill in what your checkout asks for (e-mail, accept terms).
Expiry
A checkout link works for 24 hours, and stops working once it's paid. Make a new one if the buyer comes back later.
Stuck? Write to mail@craftpay.net