Created
May 29, 2020 19:14
-
-
Save gixxerblade/260ed68a751532565af140333f233da4 to your computer and use it in GitHub Desktop.
EasyPost Stripe lambda function written for Node
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
// Function to purchase shipping from Easypost | |
// API doc here => https://www.easypost.com/stripe-relay | |
// Originally written in Ruby but converted to node | |
// and https://www.easypost.com/docs/api/node#shipments | |
const EasyPost = require("@easypost/api"); | |
const apiKey = process.env.GATSBY_EASYPOST_APIKEY; | |
const api = new EasyPost(apiKey); | |
const stripe = require("stripe")(process.env.GATSBY_STRIPE_SECRET_KEY); | |
// Error handling | |
const errorResponse = (err, callback) => { | |
const response = { | |
headers: { | |
"Access-Control-Allow-Origin": "*", | |
}, | |
statusCode: 500, | |
body: JSON.stringify({ | |
error: err, | |
message: err.message, | |
}), | |
}; | |
if (typeof callback === "function") { | |
callback(null, response); | |
} | |
}; | |
module.exports.handler = async (event, context, callback) => { | |
// Send over the order id & selected_shipping_method in the event query string from Ship.js | |
const id = event.queryStringParameters.id; | |
// The Stripe Order id is automatically sent to EasyPost as a Shipment reference | |
// according to Easypost => https://www.easypost.com/stripe-relay#easypost_stripe_relay-rb-1 | |
try { | |
//Retrieve Stripe order data | |
const order = await stripe.orders.retrieve(id); | |
//The Stripe Order.id is automatically sent to EasyPost as a Shipment reference | |
// According to https://www.easypost.com/stripe-relay | |
const shipments = await api.Shipment.retrieve(id); | |
//Stripe order should already have a "selected_shipping_method" otherwise default to lowest rate | |
let selectedRate; | |
order.selected_shipping_method && | |
order.selected_shipping_method.startsWith("rate_") | |
? (selectedRate = await order.selected_shipping_method) | |
: (selectedRate = await shipments.lowest_rate(["USPS"])); | |
// Buy this rate | |
await shipments.buy(selectedRate); | |
// Convert label to PDF | |
await shipments.convertLabelFormat("PDF"); | |
//Update the Stripe order status and tracking information | |
await stripe.orders.update(id, { | |
status: "fulfilled", | |
metadata: { | |
shipping_id: shipments.id, | |
shipping_tracking_code: shipments.tracking_code, | |
shipping_postage_label: shipments.postage_label.label_pdf_url, | |
}, | |
}); | |
// Send reponse to fetch function on the client | |
const response = { | |
headers: { | |
"Access-Control-Allow-Origin": "*", | |
}, | |
statusCode: 200, | |
body: JSON.stringify({ | |
data: order, | |
tracking_code: shipments.tracking_code, | |
shipping_label: shipments.postage_label.label_pdf_url, | |
message: "Postage successfully purchased", | |
}), | |
}; | |
// If reponse is 200... | |
response.ok && callback(null, response); | |
return response; | |
} catch (e) { | |
console.log(`Unable to fulfill Order ${id}: `, e.message); | |
errorResponse(e, callback); | |
// Runs no matter what | |
} finally { | |
console.log("Completed transaction"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment