rakeshr

strileformcodewithoutbutton

Nov 30th, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <?php
  2. require 'lib/Stripe.php';
  3.  
  4. if ($_POST) {
  5.   Stripe::setApiKey("testkey");
  6.   $error = '';
  7.   $success = '';
  8.   try {
  9.     if (!isset($_POST['stripeToken']))
  10.       throw new Exception("The Stripe Token was not generated correctly");
  11.     Stripe_Charge::create(array("amount" => 1000,
  12.                                 "currency" => "usd",
  13.                                 "card" => $_POST['stripeToken'],
  14.                                 'description' => 'Single quote purchase after login'));
  15.     Stripe_Customer::create(array(
  16.     "description" => "Customer for [email protected]"
  17. ));
  18.                                
  19.     $success = 'Your payment was successful.';
  20.   }
  21.   catch (Exception $e) {
  22.     $error = $e->getMessage();
  23.   }
  24. }
  25.  
  26. ?>
  27.  
  28. <!DOCTYPE html>
  29. <html lang="en">
  30.         <head>
  31.         <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  32.         <title>Stripe Getting Started Form</title>
  33.         <script type="text/javascript" src="https://js.stripe.com/v1/"></script>
  34.         <!-- jQuery is used only for this example; it isn't required to use Stripe -->
  35.         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  36.         <script type="text/javascript">
  37.             // this identifies your website in the createToken call below
  38.             Stripe.setPublishableKey('test key');
  39.  
  40.             function stripeResponseHandler(status, response) {
  41.                 if (response.error) {
  42.                     // re-enable the submit button
  43.                     $('.submit-button').removeAttr("disabled");
  44.                     // show the errors on the form
  45.                     $(".payment-errors").html(response.error.message);
  46.                 } else {
  47.                     var form$ = $("#payment-form");
  48.                     // token contains id, last4, and card type
  49.                     var token = response['id'];
  50.                     // insert the token into the form so it gets submitted to the server
  51.                     form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
  52.                     // and submit
  53.                     form$.get(0).submit();
  54.                 }
  55.             }
  56.  
  57.             $(document).ready(function() {
  58.                 $("#payment-form").submit(function(event) {
  59.                     // disable the submit button to prevent repeated clicks
  60.                     $('.submit-button').attr("disabled", "disabled");
  61.  
  62.                     // createToken returns immediately - the supplied callback submits the form if there are no errors
  63.                     Stripe.createToken({
  64.                         number: $('.card-number').val(),
  65.                         cvc: $('.card-cvc').val(),
  66.                         exp_month: $('.card-expiry-month').val(),
  67.                         exp_year: $('.card-expiry-year').val()
  68.                     }, stripeResponseHandler);
  69.                     return false; // submit from callback
  70.                 });
  71.             });
  72.         </script>
  73.         </head>
  74.         <body>
  75. <h1>Charge $10 with Stripe</h1>
  76. <!-- to display errors returned by createToken -->
  77. <span class="payment-errors">
  78.         <?= $error ?>
  79.         </span> <span class="payment-success">
  80.         <?= $success ?>
  81.         </span>
  82. <form action="" method="POST" id="payment-form">
  83.           <div class="form-row">
  84.     <label>Card Number</label>
  85.     <input type="text" size="20" autocomplete="off" class="card-number" />
  86.   </div>
  87.           <div class="form-row">
  88.     <label>CVC</label>
  89.     <input type="text" size="4" autocomplete="off" class="card-cvc" />
  90.   </div>
  91.           <div class="form-row">
  92.     <label>Expiration (MM/YYYY)</label>
  93.     <input type="text" size="2" class="card-expiry-month"/>
  94.     <span> / </span>
  95.     <input type="text" size="4" class="card-expiry-year"/>
  96.   </div>
  97.           <button type="submit" class="submit-button">Submit Payment</button>
  98.         </form>
  99. </body>
  100. </html>
Advertisement
Add Comment
Please, Sign In to add comment