Skip to content

Instantly share code, notes, and snippets.

@kublermdk
Last active November 7, 2022 09:44
Show Gist options
  • Save kublermdk/ba7d2bca49736eef02f94d6780a2344a to your computer and use it in GitHub Desktop.
Save kublermdk/ba7d2bca49736eef02f94d6780a2344a to your computer and use it in GitHub Desktop.
Setting up the Stripe PHP SDK to use the stripe-mock server
<?php
include_once './vendor/autoload.php';
use Stripe\ApiRequestor;
use Stripe\Stripe;
// ---------------------------------------
// Stripe Mock Setup
// ---------------------------------------
// @license http://opensource.org/licenses/BSD-3-Clause
// Ensure you are running the stripe-mock server on your computer before running this e.g
// > stripe-mock -http-port 12111 -https-port 12112
//
// Note that this is just a cut-down example of how to setup the Stripe client so you can make calls to the stripe-mock server
// You'll likely want to wrap this functionality into your unit tests, or whatever
Stripe::$apiBase = 'https://localhost:12112'; // In case you want to use the legacy way
// -- It's a self-signed SSL cert running on localhost, so you need to turn off SSL, hence setting the curl client directly with CURLOPT_SSL_VERIFYPEER off
$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_SSL_VERIFYPEER => 0]);
ApiRequestor::setHttpClient($curl);
// -- We need to set the Stripe Client Config with the 'api_base', that took me a while to figure out
$stripeClientConfig = [
'api_base' => 'https://localhost:12112', // You'll want to set this to whatever HTTPS port the stripe-mock server is running on, 12112 is the default
'api_key' => 'sk_test_***************************************************************************************************', // The stripe-mock just validates that it's a semi-valid looking string, so this works
];
$stripe = new \Stripe\StripeClient($stripeClientConfig);
// -- Now you can run things like
$paymentConfirm = $stripe->paymentIntents->confirm('pi_1DeQ7b2eZvKYlo2C5FUypnEA', ['payment_method' => 'pm_card_visa']);
$paymentConfirmId = $paymentConfirm->id; // e.g 'pi_1DeQ7b2eZvKYlo2C5FUypnEA
$paymentConfirmAmount = $paymentConfirm->amount; // e.g '1099'
$paymentConfirmCurrency = $paymentConfirm->currency; // e.g 'usd'
// Output the contents if you want, or save to DB, whatever...
var_export($paymentConfirm->toArray());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment