我正在尝试使用以下代码创建使用stripe-php和laravel 5.8在服务器中创建的PaymentIntent对象:
在routes / 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 helper函数,以向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
相关的代码,然后使用axios返回$user
,$voucher
和$voucherOwner
,则会得到响应。
我被困了好几天。我在这里想念什么?