getAccessToken method

Future<String?> getAccessToken(
  1. String userName,
  2. String amount,
  3. String apiKey,
  4. String merchantId,
  5. String sandboxMode,
  6. String email,
  7. String phone,
)

Implementation

Future<String?> getAccessToken(String userName, String amount, String apiKey, String merchantId, String sandboxMode,
    String email, String phone) async {
  // https://secure.payu.com/
  // https://secure.snd.payu.com/
  // var url = "https://secure.payu.ru/api/v4/payments/authorize";
  var url = "https://secure.payu.ru/api/v4/payments/authorize";
  try {
    Map<String, String> requestHeaders = {
      // 'Content-type': 'application/json',
      "Content-Type": "application/x-www-form-urlencoded",
      "X-Header-Signature": apiKey,
      "X-Header-Merchant": merchantId,  // MERCHANT	8 латинских букв. Можно посмотреть в Личном кабинете PayU
      "X-Header-Date": DateTime.now().toIso8601String(),
    };
    Map<String, dynamic> map = <String, dynamic>{};
    map['amount'] = amount;
    map['purpose'] = "Advertising";
    map['buyer_name'] = userName;
    map['email'] = email;
    map['phone'] = phone;
    map['allow_repeated_payments'] = "true";
    map['send_email'] = "false";
    map['send_sms'] = "false";
    map['redirect_url'] = "https://www.example.com/redirect/";
    map['webhook'] = "http://www.example.com/webhook/";

    // Map<String, String> body = {
    //   "amount": amount, //amount to be paid
    //   "purpose": "Advertising",
    //   "buyer_name": userName,
    //   "email": email,
    //   "phone": phone,
    //   "allow_repeated_payments": "true",
    //   "send_email": "false",
    //   "send_sms": "false",
    //   "redirect_url": "https://www.example.com/redirect/",
    //   // Where to redirect after a successful payment.
    //   "webhook": "http://www.example.com/webhook/",
    // };
    // dprint('Response body: $body');

    var response = await http.post(Uri.parse(url), headers: requestHeaders, body: map).timeout(const Duration(seconds: 10));
    dprint('Response status: ${response.statusCode}');
    dprint('Response body: ${response.body}');
    var jsonResult = json.decode(response.body);
    if (response.statusCode == 201) {
      if (jsonResult['success'] == true)
        return jsonResult["payment_request"]['longurl'];
    }
    messageError(context, jsonResult['message'].toString());
    return null;
  } catch (e) {
    rethrow;
  }
}