简体   繁体   中英

Stripe Simple Checkout Custom Data Field

I am trying to use Stripe's simple checkout system.

I want to include a custom field so that I can match an item_id to an order in my database.

https://stripe.com/docs/checkout#integration-simple

They don't seem to mention it in their documentation but it seems pretty essential for online services. How do I attach a custom id field that will be recorded with the order?

echo '<form action="/charge" method="POST">';
echo '<script ';
  echo 'src="https://checkout.stripe.com/checkout.js" class="stripe-button" ';
  echo 'data-key="pk_test_ksdjhg8dsgsghdsgh" ';
  echo 'data-email="email@example.com" ';
  echo 'data-name="example.com" ';
  //echo 'data-bitcoin="true" ';
  echo 'data-description="Campaign" ';
  echo 'data-currency="usd" ';
  echo 'data-amount="2000" ';
  echo 'data-locale="auto">';
echo '</script>';
echo '</form>';

I hope you require to send your custom data-field with stripe checkout and trying to match with your data base. If I'm right? Follow below detail.

For include custom data you need to use hidden input like below:

echo '<form action="/charge" method="POST">';
echo '<script ';
  echo 'src="https://checkout.stripe.com/checkout.js" class="stripe-button" ';
  echo 'data-key="pk_test_ksdjhg8dsgsghdsgh" ';
  echo 'data-email="email@example.com" ';
  echo 'data-name="example.com" ';
  //echo 'data-bitcoin="true" ';
  echo 'data-description="Campaign" ';
  echo 'data-currency="usd" ';
  echo 'data-amount="2000" ';
  echo 'data-locale="auto">';
echo '</script>';
echo '<input name="item_id" value="SOMEVALUEHERE" type="hidden">';
echo '</form>';

In above code, I include hidden for item_id . You can get the value from that input field in your PHP page like:

$item_id = $_POST["item_id"];

My experience, which is that custom fields are not possible using the basic Stripe checkout, concurs with the accepted answer on this very similar question:

How to add custom fields to popup form of Stripe Checkout

I believe the best route forward would be to get handy with stripe.js and create a custom payment form:

https://stripe.com/docs/custom-form

I am working around that at present by using different endpoint URLs in the form actions to do different things. This works fine, but I may end up using stripe.js in the production code.

you could do something like this to add extra information

echo '<form action="/charge?item_id=123" method="POST">';

and use $_REQUEST['item_id'] in stead of $_POST['item_id'] in the charge page to get the value.

Not my preferred way of working, but in this case it solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM