So, the cool kids these days seem to be programming in Node instead of PHP. As of this writing, when I was reading through the Stripe Terminal documentation (https://stripe.com/docs/terminal/js/payment), you can get through the beginning fine with PHP or curl. I personally don’t prefer a heavy wrapper around simple requests. So, I use PHP’s curl library to talk directly to the Stripe API, and then parse the results with PHP’s json_decode function. But at the bottom of the page is the part about capturing the payment. Suddenly, PHP and curl disappear from the documentation. (Perhaps this is resolved by the time you’re reading this, but perhaps not.)
So, here’s the workflow (which is quite robust – it eliminates a lot of potential pain points that could lead to double charges or missed charges, but it has a handful of steps):
- You send an API request to Stripe creating a payment_intent (using your Stripe secret with your back-end application). You send the payment amount along as a parameter, as well as the currency and card source.
- Using the JavaScript library, you send the payment_intent ID off to the P400.
- The P400 processes and does its stuff. It authorizes, but does not capture the charge. If you fail to capture the charge, it won’t settle with the day’s batch, and will be eliminated.
- When the payment is authorized, you tell your backend to capture the payment_intent
- Your backend captures the payment_intent and finishes the transaction
(There are actually a couple warm-up steps to connect to the reader.) But once you get down to the page about capturing the payment intent in the documents on the Stripe Terminal page, curl and PHP fall off the list. So, after a short amount of digging, here is some PHP code (using PHP’s curl library) to capture a payment intent. So, first, after the payment is authorized, the JavaScript library returns an object that has the payment_intent’s ID. It has the format pi_[24-numbers-a-letters]. Pass that payment intent ID along to your backend, and here’s how to capture:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/payment_intents/$paymentintent/capture");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$stripesecret" . ":" . "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Hope that saves you a bit of time. Here’s the direct link to Stripe’s deeper full documentation that includes curl and PHP: https://stripe.com/docs/api/payment_intents/capture?lang=curl