Last active
September 3, 2016 14:59
-
-
Save adamjstevenson/47468847b3057b1f3ef9c245a8679c63 to your computer and use it in GitHub Desktop.
Update default payment method for a customer with PHP
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 | |
// 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(); | |
$success = "Your payment method has been updated."; | |
} | |
catch(\Stripe\Error\Card $e) { | |
// Since it's a decline, \Stripe\Error\Card will be caught | |
$body = $e->getJsonBody(); | |
$error = $body['error']['message']; | |
} | |
// Probably want to log all of these for later or send yourself a notification | |
catch (\Stripe\Error\RateLimit $e) { | |
$error = "Sorry, we weren't able tp update your payment method. Please try again."; | |
} catch (\Stripe\Error\InvalidRequest $e) { | |
$error = "Sorry, we weren't able tp update your payment method. We're looking into this now."; | |
// Send yourself a notification, etc. | |
} catch (\Stripe\Error\Authentication $e) { | |
$error = "Sorry, we weren't able tp update your payment method. We're looking into this now."; | |
// Send yourself a notification, etc. | |
} catch (\Stripe\Error\ApiConnection $e) { | |
$error = "Sorry, we weren't able tp update your payment method. Please try again."; | |
// Send yourself a notification, etc. | |
} catch (\Stripe\Error\Base $e) { | |
$error = "Sorry, we weren't able tp update your payment method. We're looking into this now."; | |
// Send yourself a notification, etc. | |
} catch (Exception $e) { | |
$error = "Sorry, we weren't able tp update your payment method. We're looking into this now."; | |
// Send yourself a notification, etc. | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment