From 378b6dae63048e1137626408e6f06ade5b0ca42a Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Sat, 8 Nov 2025 13:06:54 -0700 Subject: [PATCH] Send Stripe more info to make customer service easier, cap margin at $3 per label --- source/static/payshipment.php | 59 ++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/source/static/payshipment.php b/source/static/payshipment.php index 678ce78..3f9e0bc 100644 --- a/source/static/payshipment.php +++ b/source/static/payshipment.php @@ -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; + } } } @@ -26,14 +33,14 @@ try { // make the payment intent but don't capture it yet, in case something goes wrong. $intent = \Stripe\PaymentIntent::create([ - 'amount' => $price * 100, - 'currency' => 'usd', - 'payment_method' => $_REQUEST["stripeid"], - 'description' => "CertifiedFromHome.com", - 'statement_descriptor' => "CrtfdFrmHome", - 'payment_method_types' => ['card', 'link'], - 'capture_method' => 'manual', - 'confirm' => true + 'amount' => $price * 100, + 'currency' => 'usd', + 'payment_method' => $_REQUEST["stripeid"], + 'description' => "CertifiedFromHome.com", + 'statement_descriptor' => "CrtfdFrmHome", + 'payment_method_types' => ['card', 'link'], + 'capture_method' => 'manual', + 'confirm' => true ]); if ($intent->status == 'requires_capture') { @@ -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