initTransaction method
dynamic
initTransaction({
- String? merchantTransactionId,
- required String customerMobile,
- required double amount,
- required RedirectType redirectType,
Implementation
initTransaction({
String? merchantTransactionId,
required String customerMobile,
required double amount,
required RedirectType redirectType,
}) async {
Map<String, dynamic> body = {
"merchantId": merchantId,
"merchantTransactionId": merchantTransactionId ?? uuid.v1(),
"merchantUserId": merchantUserId,
"amount": amount,
"redirectUrl": redirectUrl,
"redirectMode":
redirectType == RedirectType.REDIRECT ? "REDIRECT" : "POST",
"callbackUrl": callbackUrl,
"mobileNumber": customerMobile,
"paymentInstrument": {"type": "PAY_PAGE"}
};
String base64String = base64.encode(utf8.encode(jsonEncode(body)));
String toEncodeToSha256 = '$base64String/pg/v1/pay$salt';
final String sha256String =
crypto.sha256.convert(utf8.encode(toEncodeToSha256)).toString();
final String finalString = '$sha256String###$saltIndex';
Uri url = Uri.parse(debug
? "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay"
: "https://api.phonepe.com/apis/hermes/pg/v1/pay");
http.Response response = await http
.post(url, body: json.encode({"request": base64String}), headers: {
"Content-Type": "application/json",
"X-VERIFY": finalString,
"Access-Control-Allow-Credentials": "true",
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Methods": 'GET, DELETE, HEAD, OPTIONS',
"Origin": 'api-preprod.phonepe.com',
});
print(response.body);
PaymentResponse paymentResponse =
PaymentResponse.fromJson(json.decode(response.body));
await launchUrl(
Uri.parse(paymentResponse.data.instrumentResponse.redirectInfo.url));
return paymentResponse;
}