首页 > 解决方案 > laravel POST 在邮递员中工作,但不适用于 react-native 中的 axios 实例

问题描述

我正在尝试使用 stripe-php 和 laravel 5.8 使用以下代码在服务器中创建一个 PaymentIntent 对象:

在路线/api.php 中:

Route::post('/create-payment-intent', 'Api\v1\PaymentController@createPaymentIntent');

在 PaymentController.php 中:

public function createPaymentIntent(Request $request) {
    $validator = Validator::make($request->all(), [
      'amount' => 'required',
      'currency' => 'required',
      'voucherId' => 'required',
    ]);

    $user = Auth::user();
    $voucher = AppVoucher::where('id', $request->input('voucherId'))->first();
    $voucherOwner = User::where('id', $voucher['business_id'])->first();

    try {
      $paymentIntent = \Stripe\PaymentIntent::create([
        'amount' => $request->input('amount') * 100 ,
        'currency' => $request->input('currency'),
        'customer' => $user->client_stripe_id,
        'description' => $voucher->description,
        'on_behalf_of' => $voucherOwner->stripe_account_id,
        'payment_method_types' => ['card'],
        'receipt_email' => $user->email,
        'transfer_data' => ['destination' => $voucherOwner->stripe_account_id],
      ]);

      return response()->json(['paymentIntent' => $paymentIntent], 200);
    }

    catch (\CardErrorException $e) {
      return response()->json([
        'information' => 'Error. Something went wrong. Please try again',
        'error_on' => 'creating a Payment Intent object in Stripe',
      ], 400);
    }
  }

在我的客户端(React-Native 应用程序)上,我创建了一个 axios 实例和一个 apiCall 辅助函数来向我的 api 发出所有请求:

axiosInstance & apiCall 辅助函数:

const axiosInstance = axios.create({
  baseURL: config.apiUrl.local, // LOCAL ENV
  // baseURL: config.apiUrl.staging, // STAGING ENV
  cancelToken: source.token,
});

const apiCall = async ({ url, method, data, options, onSuccess, onError }) => {
  const token = await getToken();
  const headers = {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json',
  };

  if (options.withPhoto) {
    headers['Content-Type'] = 'multipart/form-data';
  }

  if (options.withToken) {
    headers.Authorization = `Bearer ${token}`;
  }

  axiosInstance({ url, method, data, headers })
    .then((res) => {
      if (res.status >= 200 || res.status < 300) {
        onSuccess(res.data);
      }
    })
    .catch(err => onError(err));
};

export default apiCall;

在 CheckoutContainer 的 componentDidMount 中:

componentDidMount() {
    const { navigation, onPaymentIntentStart, onPaymentIntentFail } = this.props;
    const voucher = navigation.getParam('voucher');
    onPaymentIntentStart();

    apiCall({
      url: 'create-payment-intent',
      data: {
        amount: Number(voucher.amount),
        currency: 'gbp',
        voucherId: voucher.id,
      },
      method: 'post',
      options: { withToken: true, withPhoto: false },
      onSuccess: res => console.log('onSuccess res: ', res),
      // onSuccess: res => this.onPaymentIntentSuccess(res),
      onError: err => console.log('onError err: ', err),
      // onError: err => onPaymentIntentFail(err.message),
    });
  }

此设置适用于我在应用程序中创建的每个 apiCall,但相关方法除外,该方法适用于 Postman,但不适用于 react-native 中的 axios。我还尝试通过添加超时键来增加 axiosInstance 中的超时,但仍然给我错误状态代码 500。

如果我删除服务器中与相关的代码$paymentIntent并返回$user$voucher$voucherOwner使用 axios,我会得到响应。

我已经被困了好几天了。我在这里能错过什么?

标签: phpreact-nativeaxiosstripe-paymentslaravel-5.8

解决方案


推荐阅读