getIdPayResponse static method

Future<IdPayResponse> getIdPayResponse({
  1. required PaymentRequest request,
  2. required String apiKey,
  3. bool isSandbox = false,
})

Implementation

static Future<IdPayResponse> getIdPayResponse(
    {required PaymentRequest request,
    required String apiKey,
    bool isSandbox = false}) async {
  try {
    String endPoint = 'https://api.idpay.ir/v1.1/payment';
    print(endPoint);
    var client = new Client();

    print(request.toJson().toString());
    Uri finalUri = Uri.parse(endPoint);
    final response = await client.post(finalUri,
        headers: {
          "X-API-KEY": apiKey,
          "X-SANDBOX": isSandbox ? '1' : '0',
          "Content-type": "application/json",
        },
        body: json.encode(request.toJson()));
    if (response.statusCode == 201) {
      var jsonResponse = json.decode(response.body);
      var idPayResponse = IdPayResponse(jsonResponse['link'], '', 201);
      return idPayResponse;
    } else {
      var jsonResponse = json.decode(response.body);
      return IdPayResponse(
          '', jsonResponse['error_message'], response.statusCode);
    }
  } catch (e) {
    print(e.toString());
    return IdPayResponse('', 'خطای نا مشخص', 400);
  }
}