Send Stripe more info to make customer service easier, cap margin at $3 per label

This commit is contained in:
Skylar Ittner 2025-11-08 13:06:54 -07:00
parent d8feed270d
commit 378b6dae63

View File

@ -9,10 +9,17 @@ try {
$shipment = \EasyPost\Shipment::retrieve($_REQUEST["shipmentid"]);
$price = 0;
$cost = 0;
foreach ($shipment->rates as $rate) {
if ($rate->id == $_REQUEST["rateid"]) {
$cost = $rate->rate;
$retail_rate = $rate->retail_rate ?? ($rate->list_rate ?? $rate->rate);
$price = $retail_rate + 1.00;
if ($retail_rate - $cost > 3) {
// Cap at $3 profit to be nice
$price = $cost + 3.00;
} else {
$price = $cost + 1.00;
}
}
}
@ -45,9 +52,40 @@ try {
$shipment->buy(['rate' => ['id' => $_REQUEST["rateid"]]]);
}
$pdffile = date("Ymd") . "_" . hash("sha256", $shipment->id) . ".pdf";
try {
$stripe = new \Stripe\StripeClient($_SETTINGS["stripe_sk"]);
$stripe->paymentIntents->update($intent->id, ["metadata" => ["tracking_number" => "$shipment->tracking_code", "rate" => $price - 1.00]]);
$stripe->paymentIntents->update($intent->id, [
"metadata" => [
"tracking_number" => "$shipment->tracking_code",
"from_email" => (empty($shipment->from_address->email) ? "None" : $shipment->from_address->email . ""),
"rate" => number_format($cost, 2),
"label" => $pdffile
],
"amount_details" => [
"line_items" => [
[
"product_name" => "Shipping Label",
"quantity" => 1,
"unit_cost" => $price * 100,
"unit_of_measure" => "each",
"product_code" => "$shipment->tracking_code",
"tax" => [
"total_tax_amount" => 0
],
"payment_method_options" => [
"card" => [
"commodity_code" => "43231515" // UNSPSC: Mailing and shipping software / Software that offices can use as an alternative to postage meters to print postage stamps and shipping labels.
]
]
]
]
],
"payment_details" => [
"order_reference" => "$shipment->tracking_code"
]
]);
} catch (Exception $exx) {
}
@ -143,7 +181,6 @@ try {
$pdf->AddPage();
$pdf->Image($tmpfile, 0, 0, -300, -300, "PNG");
$pdffile = date("Ymd") . "_" . hash("sha256", $_REQUEST["shipmentid"]) . ".pdf";
$pdf->Output("F", __DIR__ . "/pdf/" . $pdffile);
// cleanup temp file