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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料