dslijian2015 2017-01-17 02:31
浏览 69

如何在React Webpack中使用Stripe Checkout?


I hope you can help. I'll try and provide everything you to help.
I am running my build locally on Xampp. My Code:

import React from "react";
import ReactDOM from "react-dom";

export default class Front extends React.Component {

  constructor(){
    super();
    this.state = {
      account : []
    };
  };

  render(){

    return(
      <div className="shopFront">
      --Content--
      </div>
    );
  }
};

I tried to simply include(in the --Content-- placeholder):

<form action="/your-server-side-code" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" className="stripe-button"
    data-key="TEST--KEY--"
    data-amount="999"
    data-name="Product"
    data-description="Widget"
    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
    data-locale="auto"
    data-currency="aud">
  </script>
</form>

But this does not display an actual "Pay Now" button. No errors in the console. Just no button renders at all.

I've tried to use the react-stripe-checkout package.
https://www.npmjs.com/package/react-stripe-checkout

I added import StripeCheckout from 'react-stripe-checkout'; and amended my build to:

  onToken(token){
    fetch('components/charge.php', {
      method: 'POST',
      body: JSON.stringify(token),
    }).then(token => {
      // alert(`We are in business, ${token.email}`);
      console.log(token);
    });
  }

  render(){
    return(
      <div className="shopFront">
      Hello World

      <StripeCheckout
        token={this.onToken}
        stripeKey="pk_test_GwbZ42OeC9M7KRxd49t21LHC"
      />

      </div>
    );
  }

I've added the charge and config.php files. Composer is implemented with the stripe.php files.
charge.php

<?php
  require_once('./config.php');

  $token  = $_POST['stripeToken'];

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

  $charge = \Stripe\Charge::create(array(
      'customer' => $customer -> id,
      'amount'   => 5000,
      'currency' => 'aud'
  ));

  echo '<h1>Successfully charged $50.00!</h1>';
?>

config.php

<?php
require_once('vendor/autoload.php');

$stripe = array(
  "secret_key"      => "sk_test_r8KuCcmiuJfnFo58UnbV3fG0",
  "publishable_key" => "pk_test_GwbZ42OeC9M7KRxd49t21LHC"
);

\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

It almost works. Displays the "pay now" button but when the checkout form processed I get this error log in the dashboard of Stripe.

enter image description here

The customer gets created ok but the charge fires an error. Does anyone have any insight into this or a better method to including Stripe Checkout into React?
I've uploaded the build to
http://stickermata.com/stripe/
if you would like to run the test.

Any help is much appreciated.
Thanks All,

Moe

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog