Skip to content

Instantly share code, notes, and snippets.

View adamjstevenson's full-sized avatar
🏄‍♂️

Adam Stevenson adamjstevenson

🏄‍♂️
View GitHub Profile
@adamjstevenson
adamjstevenson / update_payment_method.php
Last active September 3, 2016 14:59
Update default payment method for a customer with PHP
<?php
// Check for a POSTed stripeToken
if (isset($_POST['stripeToken'])){
// Retrieve the customer id from your database/app, e.g.
// $customer_id = $user->customer_id;
try {
$customer = \Stripe\Customer::retrieve($customer_id);
$customer->source = $_POST['stripeToken']; // The token submitted from Stripe.js
$customer->save();
@adamjstevenson
adamjstevenson / list_connected_charges.rb
Last active December 15, 2017 13:03
List charges on a connected account
require 'stripe'
Stripe.api_key = "YOUR-API-KEY"
# Authenticate as the connected account and retrieve the first 100 charges
charges = Stripe::Charge.all({limit: 100},{stripe_account: "acct_YourConnectedAccountID"})
# Iterate through charges using auto-pagination
charges.auto_paging_each do |charge|
# Output the charge ID, amount, currency, refund, and dispute status
@adamjstevenson
adamjstevenson / connect_account_volume.rb
Last active November 3, 2023 13:31
Calculate gross and net transaction volume on a connected account
require 'stripe'
Stripe.api_key = "YOUR-API-KEY"
# Authenticate as the connected account and retrieve the first 100 transactions
transactions = Stripe::BalanceTransaction.list({limit: 100},{stripe_account: "acct_YourConnectedAcctID"})
total_volume = 0
net_volume = 0
# Iterate through transactions using auto-pagination
@adamjstevenson
adamjstevenson / list_connected_transfers_balance.rb
Created September 13, 2016 21:30
List Connected Account Transfers and Balance
require 'stripe'
Stripe.api_key = "YOUR-API-KEY"
account = "acct_YourConnectedAccountID"
# Retrieve available balance for the connected account
balance = Stripe::Balance.retrieve(stripe_account: account)
puts "Available balance: #{balance.available[0].amount}"
puts "Pending balance: #{balance.pending[0].amount}"
@adamjstevenson
adamjstevenson / list_connected_upcoming_transfers.rb
Created September 15, 2016 16:52
List upcoming created transfers on a connected account
require 'stripe'
# Your platform's secret key
Stripe.api_key = "YOUR-API-KEY"
# List `transfer.created` events on the connected account
events = Stripe::Event.list({type: "transfer.created", limit: 100},{stripe_account: "acct_YourConnectedAccountID"})
# Iterate through each event
events.each do | event |
@adamjstevenson
adamjstevenson / list_connected_balance_transactions_for_transfer.rb
Created September 15, 2016 18:11
List balance transactions for each transfer on a connected account
require 'stripe'
# Your platform API key
Stripe.api_key = "YOUR-API-KEY"
# The ID of the connected account
account = "acct_YourConnectedAccountID"
# Retrieve the last 10 transfers on the connected account
transfers = Stripe::Transfer.list({limit:10},{stripe_account: account})
@adamjstevenson
adamjstevenson / config.php
Created September 21, 2016 02:58
Example Stripe PHP config file
<?php
// Including the library instead of using Composer
// Read more about installing this library here: https://github.com/stripe/stripe-php
require_once('stripe-php/init.php');
// Replace with your API keys
$stripe = array(
"secret_key" => "YOUR-SECRET-API-KEY",
"publishable_key" => "YOUR-PUBLISHABLE-API-KEY"
@adamjstevenson
adamjstevenson / create_charge_with_custom_app_fee.php
Last active September 21, 2016 21:52
Charge a custom application fee for a connected account
<?php
// Include Stripe's library and set your API key.
require('./config.php');
// Retrieve the amount and application fee details from your own database...
// Ex: $db is an object containing the attributes you retrieve from your database.
// Calculate the application fee to charge, in cents
$application_fee = ($db->amount/100 * $db->percent_fee + $db->flat_fee) * 100;
@adamjstevenson
adamjstevenson / create_charge_with_tiered_app_fee.php
Created September 21, 2016 22:25
Create a charge with a custom tiered application fee
<?php
// Include Stripe's library and set your API key.
require('./config.php');
// Retrieve the account ID and application fee tier details from your own database...
// In this example $db is an object containing the attributes you retrieve from your database.
// Calculate the app fee to charge based on the pricing tier
if ($db->pricing_tier == 1){
// Charge 2.9% + $.30 per charge (passing Stripe's default processing fees)
@adamjstevenson
adamjstevenson / metadata.php
Last active September 21, 2016 22:53
Add Stripe's processing fee and your platform application fee as metadata
<?php
$charge = \Stripe\Charge::create(array(
"amount" => $db->amount, // The total amount to charge the customer, in cents
"currency" => "usd",
"source" => $token, // The token submitted from Checkout or Stripe.js
"application_fee" => $application_fee,
"destination" => $db->connected_account_id, // The account ID, retrieved from your database
"metadata" => array(
// Calculate Stripe's processing fee based on the total charge amount
"stripe fee" => ($db->amount/100 * .029 + .30) * 100,