Skip to content

Instantly share code, notes, and snippets.

@webgurus
Created June 2, 2016 13:09
Show Gist options
  • Save webgurus/99dda29f1dbcbbdcd097260391e681a6 to your computer and use it in GitHub Desktop.
Save webgurus/99dda29f1dbcbbdcd097260391e681a6 to your computer and use it in GitHub Desktop.
<?php
//BEGIN PROCESS THE FORM - NOTE $attributes global var is a merge of $_GET and $_POST - so the fields submitted in the form :);
if(empty($stripe_errors) && !empty($attributes['stripe_card_form_submitted'])){
$customer_args = array(
'card' => $attributes['stripeToken']
, 'description'=>trim($attributes['customer']['name']) . ';' . trim($attributes['customer']['email'])
, 'email'=>$attributes['customer']['email']
);
//create a new customer
//ALWAYS create the customer record in the root application - NOT using the stripe connect access_token
//according to docs, this is how you create a shared customer
$new_customer = Stripe_Customer::create($customer_args, $my_secret_key);
$cust_arr = $new_customer->__toArray();
if(!empty($cust_arr['id'])){
//save the new customer to the db so we can ::charge them
//insert was successful
//now we have a saved customer. lets try a charge
//I HAVE NOT EVEN TESTED CHARGES ON BEHALF OF 3RD PARTIES YET, BUT THIS IS HOW I ASSUME IT WOULD WORK
//$cust_token = Stripe_Token::create(array('customer'=>$cust_arr['id']), $stripe_connect_access_token); //get a token representing shared customer; returns a token and a 'card' object
//$charge_params = array('customer'=>$cust_token, 'amount'=>1000, 'currency'=>'USD', 'description'=>'Test Charge1'); //make a test charge of $10.00 USD
//$charge = Stripe_Charge::create($charge_params, $stripe_connect_access_token);
//$charge_arr = $charge->__toArray();
//NOW LETS TRY TO CREATE A PLAN FOR 3rd PARTY AND SUBSCRIBE THIS SHARED USER TO THE PLAN
$plan = Stripe_Plan::create(array('amount'=>100, 'interval'=>'month', 'interval_count'=>1, 'name'=>'3rd party plan', 'id'=>'3rdPartyPlan', 'currency'=>'USD'), $stripe_connect_access_token); //THIS WORKS
$new_plan_id = $plan_result->id; //this is the plan_id of the new plan just created in 3rd party's stripe account on their behalf
$cust_token = Stripe_Token::create(array('customer'=>$cust_arr['id']), $stripe_connect_access_token);
//get a token representing shared customer; returns a token and a 'card' object which has a null customer
$shared_cust = Stripe_Customer::retrieve($cust_token, $stripe_connect_access_token); //fails; returns 'no such customer' error.
$subscribe_result = $shared_cust->updateSubscription(array("plan" => $new_plan_id, "prorate" => true, "quantity" => 1 ), $stripe_connect_access_token );
}
}
//END PROCESS THE FORM
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment