Skip to content

Instantly share code, notes, and snippets.

@tlongren
Last active November 25, 2021 04:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlongren/5988259 to your computer and use it in GitHub Desktop.
Save tlongren/5988259 to your computer and use it in GitHub Desktop.
Stripe Setup as of July 12
<?php
require_once("/srv/www/vpsstat.us/public_html/includes/stripe-php/lib/Stripe.php");
Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$token = ($_GET['stripeToken']) ?$_GET['stripeToken'] : $_POST['stripeToken'];
$customerDetails = get_customer_details($customer_id);
if (empty($customerDetails['stripe_id'])) {
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => "standard",
"email" => $customerDetails['email']
)
);
} else {
$customerStripeId = $customerDetails['stripe_id'];
$cu = Stripe_Customer::retrieve($customerStripeId);
$cu->description = $customerDetails['firstname'] . ' ' . $customerDetails['lastname'] . ' for ' .$customerDetails['email'];
$cu->card = $token; // obtained with Stripe.js
$cu->save();
}
if ($customerDetails['stripe_id'] == "") {
$custStripeId = $customer->id;
$setStripeId = set_stripe_id($custStripeId, $customer_id);
}
// the below inserts the order into my database. Orders always make it into my database.
$newOrder = add_order($customer_id, $theplan, $apiurl, $apikey, $apihash, $allowedip, $servicename, $domain);
// the problem happens here, or somewhere after here.
$planCost = get_plan_cost($theplan);
Stripe_Charge::create(array(
"amount" => $planCost,
"currency" => "usd",
"card" => $token, // obtained with Stripe.js
"description" => "New Service")
);
// lots of checks for validity, but eventually i need to echo good back to the js so it knows everything was successful
print "good"
<form action="javascript:" method="POST" id="addservice-form">
<label>Service Name</label>
<input type="text" name="servicename" id="servicename" class="span5">
<label>API URL</label>
<input type="text" name="apiurl" id="apiurl" class="span5">
<label>API Key</label>
<input type="text" name="apikey" id="apikey" class="span5">
<label>API Hash</label>
<input type="text" name="apihash" id="apihash" class="span5">
<label>Allowed IP's (comma separated list)</label>
<input type="text" name="allowedip" id="allowedip" class="span5">
<label>Desired Status URL</label>
<div class="input-append">
<input type="text" name="domain" id="domain" class="span4" id="appendedInput">
<span class="add-on">.bleh.com</span>
</div>
<label>Service (monthly cost in parens)</label>
<select class="span5" id="theplan" name="theplan">
<option value="" disabled selected style="display: none;">Choose One:</option>
<option value="5">Starter ($9.99)</option>
<option value="1">Standard ($14.99)</option>
<option value="2">Advance ($29.99)</option>
</select>
<label>Credit Card #</label>
<input class="span5" type="text" data-stripe="number" id="thecard" placeholder="Card Number (eg: 1111222233334444)">
<label>Security Code</label>
<input class="span5" type="text" data-stripe="cvc" id="thecvc" placeholder="CVC Code/Security Code (eg: 112)">
<label>Expiration Month</label>
<select class="span1" id="themonth" data-stripe="exp-month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<label>Expiration Year</label>
<select class="span2" id="theyear" data-stripe="exp-year">
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
<option value="2026">2026</option>
<option value="2027">2027</option>
<option value="2028">2028</option>
<option value="2029">2029</option>
<option value="2030">2030</option>
<option value="2031">2031</option>
<option value="2032">2032</option>
<option value="2033">2033</option>
<option value="2034">2034</option>
<option value="2035">2035</option>
<option value="2036">2036</option>
<option value="2037">2037</option>
<option value="2038">2038</option>
<option value="2039">2039</option>
<option value="2040">2040</option>
<option value="2041">2041</option>
<option value="2042">2042</option>
<option value="2043">2043</option>
<option value="2044">2044</option>
<option value="2045">2045</option>
<option value="2046">2046</option>
<option value="2047">2047</option>
<option value="2048">2048</option>
<option value="2049">2049</option>
<option value="2050">2050</option>
</select>
<center><button type="submit" class="ladda-button btn btn-primary" data-style="expand-right" id="addservice-submit"><span class="ladda-label"><i class="icon-white icon-plus-sign"></i> Add Service</span><span class="ladda-spinner"></span></button></center>
<div class="clearfix"></div><br />
</form>
Stripe.setPublishableKey('pk_test_xxxxxxxxxxxxxxxxxxxxxxxx');
var stripeResponseHandler = function(status, response) {
if (response.error) {
// show the errors on the form
$("#stripe-errors").html(response.error.message);
} else {
var token = response['id'];
var form$ = $("#addservice-form");
form$.append("<input type='hidden' id='token' name='stripeToken' value='" + token + "' />");
var data = $("#addservice-form").serialize();
var $form = $('form#addservice-form');
$.post("/addService.php", data, function (html) {
//if process.php returned 1/true (send mail success)
alert(html);
if (html=="good") {
//hide the form
$('#serviceNameExists').hide();
$('#notComplete').hide();
$('#serviceAdded').show();
} else if (html=="notComplete") {
$('#serviceAdded').hide();
$('#serviceNameExists').hide();
$('#domainExists').hide();
$('#notComplete').show();
} else if (html=="serviceNameExists") {
$('#serviceAdded').hide();
$('#notComplete').hide();
$('#domainExists').hide();
$('#serviceNameExists').show();
} else if (html=="domainExists") {
$('#serviceAdded').hide();
$('#notComplete').hide();
$('#serviceNameExists').hide();
$('#domainExists').show();
} else alert(html);
});
}
}
$(document).ready(function(){
$('button#addservice-submit').click(function(event) {
Stripe.createToken({
number: $('#thecard').val(),
cvc: $('#thecvc').val(),
exp_month: $('#themonth').val(),
exp_year: $('#theyear').val()
}, stripeResponseHandler);
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment