简体   繁体   English

条纹未来付款设置显示您必须在与 laravel 集成的本机反应中提供付款方式类型

[英]stripe future payment setup showing you must provide payment method type in react native with laravel integration

I integrated stripe payment in react native with laravel.我在 laravel 的 react native 中集成了条带支付。 My goal is to setup future payment and just collecting card details and charge them later.我的目标是设置未来的付款方式,只收集卡详细信息并在以后收取费用。

Here's my react native code:这是我的反应本机代码:

// Start App.js
import React, {useEffect, useState} from 'react';
import { AuthProvider } from './context/AuthProvider';
import Screens from "./navigation/Screens";
import { StripeProvider } from '@stripe/stripe-react-native';
import { fetchPublishableKey } from "./helper";

export default function App() {
const [publishableKey, setPublishableKey] = useState('');

const fetchKey = async () => {
    const key = await fetchPublishableKey()
    if (key) {
        setPublishableKey(key)
    }
};

useEffect(() => {
    fetchKey();
}, []);

return (
    <StripeProvider publishableKey={publishableKey}>
        <AuthProvider>
            <Screens />
        </AuthProvider>
    </StripeProvider>
);}
// End App.js

// Start AccountNoBilling.js
import {CardField,useConfirmSetupIntent} from '@stripe/stripe-react-native';
import axiosConfig from "../../helpers/axiosConfig";
import {API_URL} from "../../Config";
export default function AccountNoBilling ({ route, navigation}) {

const fetchPaymentIntentClientSecret = async () => {
    axiosConfig.defaults.headers.common['Authorization'] = `Bearer ${user.token}`;
    const response = await axiosConfig.post(`${API_URL}/payment-sheet`)
        .catch(error => {
            console.log('Error: ', error.response.data)
            alert('Error: '+ error.response.data)
        });

    if (typeof (response.data) === undefined) {
        alert('Hello')
    } else {
        return response.data;
    }
};

const handlePayPress = async () => {
        // Gather the customer's billing information (for example, email)
        const billingDetails: BillingDetails = {
            email: 'test.card@example.com',
        };

       // Create a setup intent on the backend
       const clientSecret = await fetchPaymentIntentClientSecret();

       const { setupIntent, error } = await confirmSetupIntent(clientSecret, {
            paymentMethodType: 'Card',
            paymentMethodData: {
                billingDetails,
            }
        });

        if (error) {
            Alert.alert(`Error code: ${error.code}`, error.message);
        } else {
            Alert.alert('Success',setupIntent + ' Your payment method is successfully set up for future payments!');
        }
}

function renderCreditCard() {
           return (
                   <Block>
                        <CardField
                            postalCodeEnabled={true}
                            placeholder={{
                                number: '4242 4242 4242 4242',
                            }}
                            cardStyle={{
                                backgroundColor: '#edeff2',
                                textColor: '#000000',
                                borderWidth: 1,
                                borderColor: '#000000',
                                borderRadius: 8
                            }}
                            style={styles.cardInput}
                            onCardChange={(cardDetails) => {
                                console.log('cardDetails', cardDetails);
                            }}
                            onFocus={(focusedField) => {
                                // console.log('focusField', focusedField);
                            }}
                        />
                    </Block>
                 );
           }

function renderSaveButton() {
                 return (
                             <Block style={{ flex: 12 }} center>
                                <Button
                                    color="success"
                                    style={loading ? styles.buttonDisabled : styles.buttonEnabled}
                                    variant="primary"
                                    disabled={loading}
                                    onPress={handlePayPress}
                                >
                                    Save my billing details
                                </Button>
                                <Text color={argonTheme.COLORS.SUCCESS}>You do not pay now – cancel anytime</Text>
                            </Block>
                      )

return (
    <Block flex style={{ backgroundColor: argonTheme.COLORS.BACKGROUND}}>
        <ScrollView showsVerticalScrollIndicator={false}>
            { renderDescription() }
            { renderBillingAddress() }
            { renderCreditCard() }
            { renderSaveButton() }
        </ScrollView>
    </Block>
)}
// End AccountNoBilling.js

Here's my backend laravel api code:这是我的后端 laravel api 代码:

public function createSetupIntent()
{
    try {
        Stripe::setApiKey(env('STRIPE_SECRET'));

        $customer = User::find($request->userId ?? auth()->id());

        $customer->createAsStripeCustomer();

        $setupIntent = SetupIntent::create([
            'customer' => $customer->stripe_id,
        ]);

        return response()->json($setupIntent->client_secret, 200);
    } catch (Exception $e) {
        return response()->json($e->getMessage(), 422);
    }
}

I was wondering why it says "You must provide paymentMethodType" which I already provided as you can see in handlePayPress function.我想知道为什么它说我已经提供的“您必须提供 paymentMethodType”,正如您在handlePayPress函数中看到的那样。

反应原生“错误代码:失败”

Really matters on version of react native SDK.对 react native SDK 的版本非常重要。 before version 0.8.0 that field used to be named type which has change to paymentMethodType , so the naming difference may be part of why seeing this behavior.在 0.8.0 版本之前,该字段曾经被命名为type ,它已更改为paymentMethodType ,因此命名差异可能是为什么看到这种行为的一部分。 stripe react native SDK version changes here 条纹反应原生 SDK 版本更改在这里

Adding to the latest answer, the migration guide from stripe 0.7.x to 0.8.0 helped me fix my issue.添加到最新答案中, 从 stripe 0.7.x 到 0.8.0 的迁移指南帮助我解决了我的问题。 You seem to have correctly renamed the field indeed and its not clear for me, so I decided the link to the migration guide might help you, or somebody else with the issue.您似乎确实正确地重命名了该字段并且对我来说并不清楚,所以我决定迁移指南的链接可能会帮助您或其他人解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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