Я пытаюсь интегрировать полосовую оплату в свой проект Flutter с бэкэнд Laravel. По какой-то причине я получаю сообщение об ошибке в json.decode.
E / flutter (10999): [ОШИБКА: flutter / lib / ui / ui_dart_state.cc (157)] Необработанное исключение: FormatException: неожиданный символ (в символе 1) E / flutter (10999): ‹! Doctype html›
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0)),
color: Colors.redAccent,
onPressed: () {
var payment = Payment();
payment.name = _cardHolderName.text;
payment.email = _cardHolderEmail.text;
payment.cardNumber = _cardNumber.text;
payment.expiryMonth = _expiryMonth.text;
payment.expiryYear = _expiryYear.text;
payment.cvcNumber = _cvcNumber.text;
_makePayment(context, payment);
},
child: Text('Make Payment',
style: TextStyle(color: Colors.white)),
),
void _makePayment(BuildContext context, Payment payment) async {
PaymentService _paymentService = PaymentService();
var paymentData = await _paymentService.makePayment(payment);
var result = json.decode(paymentData.body);
if (result['result'] == true) {
CartService _cartService = CartService();
this.widget.cartItems.forEach((cartItem){
_cartService.deleteCartItemById(cartItem.productId);
});
_showPaymentSuccessMessage(context);
Timer(Duration(seconds: 2), () {
Navigator.pop(context);
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
});
}
}
Платежная служба
class PaymentService {
Repository _repository;
PaymentService(){
_repository = Repository();
}
makePayment(Payment payment) async {
return await _repository.httpPost('make-payment', payment.toJson());
}
}
Код Laravel
public function makePayment(Request $request)
{
\Stripe\Stripe::setApiKey('sk_test_7422pw1lMsdRfN2FaPmfFKlt00AtiIJD9O');
$token = Token::create([
'card'=>[
'number'=>$request-input('cardNumber'),
'exp_month'=>$request-input('expiryMonth'),
'exp_year'=>$request-input('expiryYear'),
'cvc'=>$request-input('cvcNumber')
]
]);
\Stripe\PaymentIntent::create([
'amount' => 1000,
'currency' => 'inr',
'source' => $token,
'receipt_email' => $request->input('email'),
]);
}
Ошибка
E/flutter (10999): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (10999): <!doctype html>
E/flutter (10999): ^
E/flutter (10999):
E/flutter (10999): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1394:5)
E/flutter (10999): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1261:9)
E/flutter (10999): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:926:22)
E/flutter (10999): #3 _parseJson (dart:convert-patch/convert_patch.dart:31:10)
E/flutter (10999): #4 JsonDecoder.convert (dart:convert/json.dart:495:36)
E/flutter (10999): #5 JsonCodec.decode (dart:convert/json.dart:153:41)
E/flutter (10999): #6 _PaymentScreenState._makePayment (package:factory2homes/screens/payment_screen.dart:160:23)
E/flutter (10999): <asynchronous suspension>
E/flutter (10999): #7 _PaymentScreenState.build.<anonymous closure> (package:factory2homes/screens/payment_screen.dart:142:23)
E/flutter (10999): #8 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (10999): #9 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (10999): #10 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (10999): #11 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (10999): #12 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (10999): #13 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter (10999): #14 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (10999): #15 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter (10999): #16 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (10999): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (10999): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (10999): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (10999): #20 _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (10999): #21 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (10999): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (10999): #23 _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (10999): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (10999):
Эта ошибка возникает, когда сервер возвращает тело
html
или на стороне сервера произошла ошибка при обработке вашего запроса. Попробуйте использовать отладчик и посмотритеresponse
запроса, чтобы узнать, соответствует ли ответstatus
200
, а также проверьте ответbody
, чтобы узнать, что сервер отправляет на ваш запрос.Также попробуйте обернуть вас
jsonDecode
внутриtry-catch
для обработки будущих исключений.