<?php
$stripeRand = 'as908fas098';
$my_publishable_key = 'abc123';
$my_secret_key = '123abc';
$stripe_connect_access_token = 'def456';
$stripe_connect_publishable_key = '456def';
$js = "<script src=\"https://js.stripe.com/v2/\"></stripe>
<script>
jQuery(document).ready(function($){
Stripe.setPublishableKey('" . $my_publishable_key . "');
var stripeform = $('#" . $stripeRand . "');
stripeform.bind('validationpassed', function(e){
e.preventDefault();
$('.payment-errors', stripeform).empty();
stripeform.find('button').prop('disabled', true);
Stripe.card.createToken(stripeform, stripeResponseHandler_" . $stripeRand . ");
// Prevent the form from submitting with the default action
return false;
});
});
function stripeResponseHandler_" . $stripeRand . "(status, response) {
//console.log('stripeResponseHandler_" . $stripeRand . " fired; status: ', status, '; response: ', response);
var stfrm = $('#" . $stripeRand . "');
if (!!response['error']) {
// show the errors on the form
$('.payment-errors', stfrm).text(response.error.message);
stfrm.find('button').prop('disabled', false);
return false;
} else {
var token = response['id'];
$('.payment-errors').text('Card accepted');
stfrm.append(\"<input type='hidden' name='stripeToken' value='\" + token + \"'/>\");
stfrm.get(0).submit();
}
}
</script>";
$ast = '<span class="colorRed"> *</span>';
$stripeform = '
<form id="' . $stripeRand . '" class="validateme hijacksubmit" method="POST" action="#">
<input type="hidden" name="stripe_card_form_submitted" value="1" />
<fieldset class="ui-fieldset ui-corner-all">
<legend>Stripe Form</legend>
<span class="payment-errors error"></span>
<table class="tableForm">
<tr>
<th>
Name on Card' . $ast . '
</th>
<td>
<input type="text" name="customer[name]" class="required" maxlength="100" minlength="3" data-stripe="name" />
</td>
</tr>
<tr>
<th>
Email' . $ast . '
</th>
<td>
<input type="text" name="customer[email]" class="required email" maxlength="100" minlength="3" />
</td>
</tr>
<tr>
<th>
Credit/Debit Card Number' . $ast . '
</th>
<td>
<input type="text" name="number" class="required stripe-cardNumber nopost" data-stripe="number" />
</td>
</tr>
<tr>
<th>
CVC Code' . $ast . '
</th>
<td>
<input type="text" name="cvc" class="required stripe-CVC nopost" size="4" style="width: 4em;" data-stripe="cvc" />
</td>
</tr>
<tr>
<th>
Expiration (MM/YYYY)' . $ast . '
</th>
<td>
<input type="text" name="exp-month" class="required nopost stripe-expiry-month" size="2" style="width: 2em;" data-stripe="exp-month" /> / <input type="text" name="exp-year" class="required nopost stripe-expiry-year" size="4" style="width: 4em;" data-stripe="exp-year" />
</td>
</tr>
<tr>
<th>
Address1
</th>
<td>
<input type="text" class="nopost" name="address_line1" maxlength="100" minlength="5" data-stripe="address_line1" />
</td>
</tr>
<tr>
<th>
Address2
</th>
<td>
<input type="text" class="nopost" name="address_line2" maxlength="100" minlength="2" data-stripe="address_line2" />
</td>
</tr>
<tr>
<th>
City
</th>
<td>
<input type="text" class="nopost" name="address_city" maxlength="60" minlength="2" data-stripe="address_city" />
</td>
</tr>
<tr>
<th>
State
</th>
<td>
<input type="text" class="nopost" name="address_state" maxlength="60" minlength="2" data-stripe="address_state" />
</td>
</tr>
<tr>
<th>
Postal Code
</th>
<td>
<input type="text" class="nopost alphanumeric" name="address_zip" maxlength="10" minlength="5" style="width: 10em;" data-stripe="address_zip" />
</td>
</tr>
<tr>
<th>
Country
</th>
<td>
<input type="text" class="nopost" name="address_country" maxlength="3" minlength="3" style="width: 3em;" data-stripe="address_country" />
</td>
</tr>
</table>
<button type="submit">Pay</button>
</fieldset>
</form>
';
echo $stripieform . $js;
//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
$sql = "
INSERT INTO
stripe_customer
SET ...
";
$insert_params = array('customer_id'=>$cust_arr['id'], ...);
$insert_result = $dboc->Execute($sql, $insert_params);
if(!empty($insert_result['success'])){
//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