Created
March 28, 2023 15:01
-
-
Save gilbitron/4f71d7f9fea4a5178f9016f33fd7f06b to your computer and use it in GitHub Desktop.
Stripe > Lemon Squeezy exporter
This file contains hidden or 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
<?php | |
/** | |
* This script creates an `export.csv` file of Stripe PaymentIntent data that is | |
* in the correct format for importing into Lemon Squeezy. | |
* | |
* @link https://docs.lemonsqueezy.com/help/migrating/migrating-from-stripe | |
* | |
* Usage: | |
* 1. Run `composer require stripe/stripe-php` | |
* 2. Replace `[STRIPE_API_KEY]` with your Stripe API key | |
* 3. Run `php stripe-exporter.php` | |
*/ | |
use Stripe\Stripe; | |
use Stripe\PaymentIntent; | |
require_once __DIR__ . '/vendor/autoload.php'; | |
// Replace this with your Stripe API key | |
$apiKey = '[STRIPE_API_KEY]'; | |
Stripe::setApiKey($apiKey); | |
echo "Exporting Stripe payment intents...\n"; | |
$stripePaymentIntents = PaymentIntent::search([ | |
'query' => 'status:"succeeded"', | |
'limit' => 100, | |
'expand' => ['data.customer'], | |
]); | |
$rows = []; | |
foreach ($stripePaymentIntents->autoPagingIterator() as $stripePaymentIntent) { | |
// Alter this if required | |
$productName = $stripePaymentIntent->description; | |
$charge = $stripePaymentIntent->charges->data[0]; | |
$country = $charge->billing_details->address->country ?? null; | |
$zipCode = $charge->billing_details->address->postal_code ?? null; | |
$refunded = $charge->refunded ?? false; | |
$disputed = $charge->disputed ?? false; | |
$rows[] = [ | |
'order_identifier' => $stripePaymentIntent->id, | |
'product_id' => $stripePaymentIntent->metadata->product_id, | |
'product_name' => $productName, | |
'customer_name' => $stripePaymentIntent->customer->name, | |
'customer_email' => $stripePaymentIntent->customer->email, | |
'purchase_date' => date('Y-m-d H:i:s', $stripePaymentIntent->created), | |
'subtotal' => number_format($stripePaymentIntent->amount / 100, 2), | |
'total' => number_format($stripePaymentIntent->amount / 100, 2), | |
'currency' => strtoupper($stripePaymentIntent->currency), | |
'country' => $country, | |
'zip_code' => $zipCode, | |
'status' => $stripePaymentIntent->status, | |
'refunded' => $refunded, | |
'disputed' => $disputed, | |
]; | |
} | |
echo "Found " . count($rows) . " payment intents.\n"; | |
$fh = fopen('export.csv', 'w'); | |
fputcsv($fh, array_keys($rows[0])); | |
foreach ($rows as $row) { | |
fputcsv($fh, $row); | |
} | |
fclose($fh); | |
echo "Exported " . count($rows) . " payment intents to export.csv.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment