php - 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,我会得到响应。
我已经被困了好几天了。我在这里能错过什么?
解决方案
推荐阅读
- javascript - 两个按钮中的一个启动滚动,但它们都应该
- c - 如何在 C 运行时获取哪些有问题的源代码和行?
- oracle - 在Oracle中查找某个时期内帐户总和之间的差异
- linux - ubuntu 中的 systemd 文件未按预期运行
- stata - Stata中看似无关的回归后的利润率太慢
- css - JavaFX TabPane:如何制作梯形标签
- javascript - Firebase Firestore - 如何查询两个匹配的“WHERE”子句中的任何一个
- python - 带有循环的 Python 列表 remove() 无法按预期工作
- sqlite - 我有一些列表的类,我想将这两个列表存储到颤振应用程序中的 Sqllite 并将其检索回来
- jquery - 使用 Ajax 时如何删除服务器端 DATATABLE 中的空白?