【问题标题】:Not connecting to Stripe API未连接到 Stripe API
【发布时间】:2020-08-19 08:43:21
【问题描述】:

我正在编写条带 React Native 网关,并且正在编写与 API 的条带连接。我使用 API 连接到 Stripe,但它说我没有连接到 API。这主要在两个文件中:ShopScreen.js 和 index.js。

这里是 ShopScreen.js:

import {PureComponent} from 'react';
import stripe from 'tipsi-stripe';
import Button from '../components/components/Button';
import {
  ActivityIndicator,
  TouchableOpacity,
  TextInput,
  Image,
  ImageBackground,
} from 'react-native';
import * as React from 'react';
import axios from 'axios';
import {WebView} from 'react-native-webview';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
  FlatList,
} from 'react-native';
stripe.setOptions({
  publisherKey: 'pk_test_HWcOeGStIfoP98VZkHRIJUmO00E1eZyuQG',
});

class ShopScreen extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      search: '',
      data: [],
    };
  }
  handleSearch = (text) => {
    this.setState({search: text});
  };

  componentDidMount() {
    axios
      .get('http://localhost:3000')
      .then((response) => {
        this.setState({
          data: response.data,
        });
      })
      .catch(function (err) {
        alert(err);
      });
  }

  static title = 'Card Form';

  state = {
    loading: false,
    token: null,
  };

  handleCardPayPress = async () => {
    try {
      this.setState({loading: true, token: null});
      const token = await stripe.paymentRequestWithCardForm({
        // Only iOS support this options
        smsAutofillDisabled: true,
        requiredBillingAddressFields: 'full',
        prefilledInformation: {
          billingAddress: {
            name: 'Vaibhav Herugu',
            line1: '2 Darryl Drive',
            line2: '',
            city: 'Morganville',
            state: 'NJ',
            country: 'USA',
            postalCode: '07751',
            email: 'vherugu@gmail.com',
          },
        },
      });

      this.setState({loading: false, token: token});
    } catch (error) {
      this.setState({loading: false});
    }
  };

  makePayment = async () => {
    this.setState({loading: true});
    axios({
      method: 'POST',
      url:
        'https://us-central1-localmainstreet-b0144.cloudfunctions.net/completePaymentWithStripe',
      data: {
        amount: 0,
        currency: 'usd',
        token: this.state.token,
      },
    }).then((response) => {
      console.log(response);
      this.setState({loading: false});
    });
  };

  render() {
    const {loading, token} = this.state;
    console.log('##data', this.state.data);
    const Item = ({user}) => {
      console.log('##item', user);
      return (
        <View>
          <View style={styles.viewforFlatList}>
            <View style={styles.viewforButton}>
              <Text style={styles.businessNameStyle}>{user.item.bname}</Text>
              <View style={styles.container}>
                <Button
                  style={styles.buttons1}
                  text="Buy Now"
                  loading={loading}
                  onPress={this.handleCardPayPress}
                />
                <View style={styles.token}>
                  {token && (
                    <>
                      <Text style={styles.instruction}>
                        Token: {this.state.token}
                      </Text>
                      {this.makePayment}
                    </>
                  )}
                </View>
              </View>
            </View>
          </View>
          <Text style={styles.businessDescandEmailStyle}>
            {user.item.bdesc}
          </Text>
          <Text style={styles.businessDescandEmailStyle}>
            Email: {user.item.email}
          </Text>
          <Text style={styles.phoneNumberStyle}>
            Phone Number: {user.item.phonenum}
          </Text>
        </View>
      );
    };

    const {navigate} = this.props.navigation;
    return (
      <View style={styles.viewForSearch}>
        <StatusBar barStyle="dark-content" />
        <View style={styles.viewforButton}>
          <TextInput
            style={styles.input2}
            underlineColorAndroid="transparent"
            placeholder="Business Category/Name"
            placeholderTextColor="#000000"
            autoCapitalize="none"
            onChangeText={this.handleSearch}
          />
          <TouchableOpacity style={styles.buttons}>
            <Text style={styles.buttonText}>Search</Text>
          </TouchableOpacity>
        </View>
        <TouchableOpacity
          style={styles.buttonsUnderLogin}
          onPress={() => {
            navigate('Help');
          }}>
          <Text style={styles.buttonTextForSignUp}>Help</Text>
        </TouchableOpacity>
        {/* <WebView
        source={{ uri: 'https://reactnative.dev' }}
        style={{ marginTop: 20 }}
      />   */}
        <View style={styles.FlatList}>
          <FlatList
            data={this.state.data}
            renderItem={(user) => <Item user={user} />}
            keyExtractor={(user) => user.id}
          />
        </View>
      </View>
    );
  }
}

export default ShopScreen;

(ShopScreen.js 未显示样式)

这里是 index.js:

const functions = require('firebase-functions');
const stripe = require('stripe')('SECRET-KEY');

exports.completePaymentWithStripe = functions.https.onRequest(
  (request, response) => {
    stripe.charges
      .create({
        amount: request.body.amount,
        currency: request.body.currency,
        source: 'tok_mastercard',
      })
      .then((charge) => {
        response.send(charge);
        return charge;
      })
      .catch((error) => {
        console.log(error);
      });
  },
);

(上面写着 SECRET-KEY 我添加了我的密钥)

这应该连接到 Stripe 并处理我的付款。相反,它显示,

我去了文档并尝试使用它说要使用的东西,但它不起作用,所以我删除了它。

这是我的问题。谢谢。

【问题讨论】:

  • 您好 Vaibhav,此问题似乎是由于未提供您的 可发布 密钥而不是您的秘密(后端)密钥造成的。如果您查看在 ShopScreen.js 中初始化tipsi-stripe 的方式,您会注意到您将可发布密钥设置为“publisherKey”。它应该是“publishableKey”而不是:tipsi.github.io/tipsi-stripe/docs/usage.html。简而言之,在 ShopScreen.js 中将 publisherKey 更改为 publishableKey 应该可以解决您的问题。
  • 谢谢!它奏效了

标签: javascript react-native stripe-payments


【解决方案1】:

嘿,问题是您没有在请求中添加令牌

exports.completePaymentWithStripe = functions.https.onRequest(
      (request, response) => {
    stripe.customers.create(req.body.token).then(customer => {
        stripe.charges
          .create({
            amount: request.body.amount,
            currency: request.body.currency,
            source: 'tok_mastercard',
            customer: customer.id
          })
         })
          .then((charge) => {
            response.send(charge);
            return charge;
          })
          .catch((error) => {
            console.log(error);
          });
      },
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 2017-08-06
    • 2020-02-24
    • 2016-08-20
    • 2017-10-18
    相关资源
    最近更新 更多