// Include the Stripe library: #require_once('lib/Stripe.php'); // set your secret key: remember to change this to your live secret key in production // see your keys here https://manage.stripe.com/account Stripe::setApiKey(STRIPE_PRIVATE_KEY); $charge = array(); if (CHARGE_LATER) { # We will charge the customer later so we will create the customer at first and will charge the customer later. # We will check whether there are any customer already available in database or not. $customerId = getStripeCustomerId($_SESSION['FBID']); error_log("Got Customer ID [" . $customerId . "]"); if ($customerId == "") { # Create a Customer if the customer is already not in database. $customer = Stripe_Customer::create(array("card" => $token, "description" => $_SESSION['FBID'])); // Save the customer ID in your database so you can use it later saveStripeCustomerId($_SESSION['FBID'], $customer->id); error_log("Saving Customer [" . $_SESSION['FBID'] . "] [" . $customer->id . "]"); $customerId = $customer->id; } if ($customerId != "") { // Save the Transaction details in your database so you can use it later ## The default card is the 1st card list($fullName, $email) = getFullName($_SESSION['FBID']); $sid = $_SESSION['FBID']; #autoSubscribe($email); ## Get current Card ID from Token $retrieve_token = Stripe_Token::retrieve($token); $card_id = $retrieve_token->card->id; error_log("token :: [" . $token . "]"); error_log("card_id :: [" . $card_id . "]"); ## To check whether the Card is already in Customer Base if not we will add this now.
function createStripeCustomer($token, $user) { try { $customer = \Stripe\Customer::create(array("source" => $token, "email" => $user["email"], "description" => "user_id=" . $user["user_id"] . ": " . $user["fname"] . " " . $user["lname"])); saveStripeCustomerId($user["user_id"], $customer->id); } catch (\Stripe\Error\ApiConnection $e) { // Network error. Try again? $e_json = $e->getJsonBody(); $err = $e_json['error']; return $err['message']; } catch (\Stripe\Error\InvalidRequest $e) { // You screwed up in your programming. This shouldn't happen! $e_json = $e->getJsonBody(); $err = $e_json['error']; return $err['message']; } catch (\Stripe\Error\Api $e) { // Stripe's servers are down! $e_json = $e->getJsonBody(); $err = $e_json['error']; return $err['message']; } catch (\Stripe\Error\Base $e) { // Something else that's not the customer's fault. $e_json = $e->getJsonBody(); $err = $e_json['error']; return $err['message']; } return $customer->id; }