Skip to content

Instantly share code, notes, and snippets.

@adamjstevenson
Last active September 21, 2016 21:52
Show Gist options
  • Select an option

  • Save adamjstevenson/41942b25829f5fb45e56c68adfe3bdbd to your computer and use it in GitHub Desktop.

Select an option

Save adamjstevenson/41942b25829f5fb45e56c68adfe3bdbd to your computer and use it in GitHub Desktop.
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;
// Check for a submitted token
if (isset($_POST['stripeToken'])){
try {
$charge = \Stripe\Charge::create(array(
"amount" => $db->amount, // The total amount to charge the customer, in cents
"currency" => "usd",
"source" => $_POST['stripeToken'], // The token submitted from Checkout or Stripe.js
"application_fee" => $application_fee,
"destination" => $db->connected_account_id, // The account ID, retrieved from your database
));
$success = "Thanks! Your payment has been made";
}
catch(\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$error = $body['error']['message']; // Display the $error message to the user in your app
} catch (\Stripe\Error\RateLimit $e) {
// Rate limits are pretty rare. Maybe have the user try again?
} catch (\Stripe\Error\InvalidRequest $e) {
// You made an invalid request to Stripe. Maybe log or send yourself an email?
} catch (\Stripe\Error\Authentication $e) {
// You used a bad API key
} catch (\Stripe\Error\ApiConnection $e) {
// There was a connectivity problem. Maybe log and have the user try again?
} catch (\Stripe\Error\Base $e) {
// Some other API error. Maybe log or send yourself an email?
} catch (Exception $e) {
// Something unrelated to Stripe happened. Log or send yourself an email, etc
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment