Skip to content

Instantly share code, notes, and snippets.

@bkrausz
Forked from maccman/charge.php
Last active September 24, 2021 17:13
Show Gist options
  • Save bkrausz/a76f9d7a9b446337d470 to your computer and use it in GitHub Desktop.
Save bkrausz/a76f9d7a9b446337d470 to your computer and use it in GitHub Desktop.
Please see https://stripe.com/docs/checkout/php for updated code examples
// Please see https://stripe.com/docs/checkout/php for updated code examples
@sradhaa
Copy link

sradhaa commented Dec 8, 2016

I am getting this error {"error":"No such token: card_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(invalid_request_error)"}

@openconcept
Copy link

openconcept commented Jan 1, 2017

5000 cents is $50. The commenter mspivak pointed out that on the last line in charge.php it says $5, not $50. This was apparently misunderstood by subsequent commenters, who got it turned around. And the incorrect amount is still there as "Successfully charged $5" in the charge.php example.

@jcaruso001
Copy link

jcaruso001 commented Jan 19, 2017

Why is this all hard coded? Why isn't there any error handling for declined cards? This is very misleading...

For those of you looking for something with some error handling for declined cards just surround it in a try catch...

		try {
		
			\Stripe\Stripe::setApiKey("....");

			// Charge the user's card:
			$charge = \Stripe\Charge::create(array(
			"amount" => $Request->Get('amount'),
			"currency" => "usd",
			"description" => $Request->Get('description'),
			"source" => $Request->Get('charge_id'),
			));

		} catch (Exception $e) {
			//TODO: Error Handeling logic.
		}

@Bhlowe
Copy link

Bhlowe commented Apr 24, 2017

Someone at Stripe should update the charge.php source code and then reply to the comments.

  1. Add Error handling
  2. Change $5 to $50
  3. Use 'email' => $_POST['stripeEmail'], instead of 'email' => 'customer@example.com',

I added the following to charge.php to help show what was returned:

echo "post: <pre>"; print_r($_POST) ;  echo "</pre>";
echo "customer: <pre>"; print_r($customer) ;  echo "</pre>";
echo "charge: <pre>"; print_r($charge) ;  echo "</pre>";

The documentation for config.php should be changed to show that you can use init.php instead of autoload.php for those that don't use or know how to use composer.

@hemant-tivlabs
Copy link

The piece of code:

$customer = \Stripe\Customer::create(array(
      'email' => 'customer@example.com',

should actually be:

$customer = \Stripe\Customer::create(array(
      'email' => $_POST['stripeEmail'],

as that is the same email as submitted by the customer, and gets posted back with "stripeToken" and "stripeTokenType".

@EricKing1
Copy link

Someone needs to update the example. Having problems with the charge amount this does NOT work. I have tried both, what is the element name to capture?
$charge = \Stripe\Charge::create(array( 'customer' => $customer->id, 'amount' => $_POST['stripeAmount'] or $_POST['data-amount'], 'currency' => 'usd' ));

@emiled
Copy link

emiled commented May 29, 2017

This example does not work! 'card' is the wrong parameter, it should be 'source'

inside charge.php:

this:

  $customer = \Stripe\Customer::create(array(
      'email' => 'customer@example.com',
      'card'  => $token
  ));

should be:

  $customer = \Stripe\Customer::create(array(
      'email' => 'customer@example.com',
      'source'  => $token
  ));

@tegarkurniawan
Copy link

I am getting this error {"error":"No such token: card_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(invalid_request_error)"}

@JoysonAnto
Copy link

Hi Stripe Bitcoin team,
I need to charge the amount that varies dynamically each time how to manage that data-amount="5000"

 <script
			src="https://checkout.stripe.com/checkout.js" class="stripe-button"
			data-key="<?php echo $stripe_code['publishable_key']; ?>"
			data-amount="<?php echo $price; ?>"
			data-name="OfficeBnb"
			data-description="2 widgets (<?php echo  $this->session->userdata('currency_s').($price); ?>) Cents"
			data-image="images/logo/<?php echo $this->config->item('logo_image');?>"
			data-label="Pay with Card or Bitcoin"
			data-locale="auto"
			data-currency="<?php echo $this->session->userdata('currency_type'); ?>"
			data-bitcoin="true">
		</script>

@terrimorgan
Copy link

Same comment as @Bhlowe back in April ... updates to the examples are overdue.

Same comment as @JoysonAnto needs to be an option to get a variable amount. Can't use this with a store -- unless attached directly to products that can only be purchased individually at a set price.

Same for the Requirement to add "Composer" ..."assumes you’ve installed the Stripe PHP library via Composer. ..." which also assumes quite a bit.

For what is touted as "super simple" the implementation seems to be much more complicated than it should need to be. Granted, I'm not an expert, my skills are ok. For those of us who might want to use Stripe but don't live and breathe code every day the claim of "as little as one line of client side code" is seriously misleading. There's quite a bit more required to implement than "one line of client side code."

@ovvval
Copy link

ovvval commented Aug 10, 2017

This is really awesome. Straight to the point. I love it.

@aldcar2002
Copy link

How can I get the confirmation number or transaction number of the payment ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment