Reputation:
This is my function:
private void payUsingPhonePe() throws PhonePeInitException {
String MERCHANT_ID = "<hidden>";
String salt = "<hidden>";
String apiEndPoint = "/pg/v1/pay";
PhonePe.init(Checkout.this, PhonePeEnvironment.RELEASE, MERCHANT_ID, "<hidden>");
HashMap<String, Object> data = new HashMap<>();
data.put("merchantId", MERCHANT_ID);
data.put("merchantTransactionId", orderId);
data.put("merchantUserId", custID);
data.put("amount", total * 100);
data.put("mobileNumber", phone);
data.put("callbackUrl", "https://webhook.site/callback-url");
HashMap<String, Object> paymentInstrument = new HashMap<>();
paymentInstrument.put("type", "UPI_INTENT");
paymentInstrument.put("targetApp", "com.phonepe.app");
data.put("paymentInstrument", paymentInstrument);
HashMap<String, Object> deviceContext = new HashMap<>();
deviceContext.put("deviceOS", "ANDROID");
data.put("deviceContext", deviceContext);
String payloadBase64 = convertDataToBase64(data);
String checksum = generateChecksum(payloadBase64, apiEndPoint, salt);
B2BPGRequest b2BPGRequest = new B2BPGRequestBuilder()
.setData(Objects.requireNonNull(payloadBase64))
.setChecksum(Objects.requireNonNull(checksum))
.setUrl(apiEndPoint)
.build();
List<UPIApplicationInfo> upiApps = PhonePe.getUpiApps();
startActivityForResult(PhonePe.getImplicitIntent(this, b2BPGRequest, upiApps.toString()), 777);
}
This is another function:
public static String convertDataToBase64(HashMap<String, Object> data) {
try {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(data);
byte[] base64Bytes = Base64.encode(jsonString.getBytes(), Base64.DEFAULT);
return new String(base64Bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
This is another function:
public static String generateChecksum(String payloadBase64, String apiEndPoint, String salt) {
try {
String dataToHash = payloadBase64 + apiEndPoint + salt;
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(dataToHash.getBytes());
byte[] byteData = md.digest();
StringBuilder checksumBuilder = new StringBuilder();
for (byte b : byteData) {
checksumBuilder.append(String.format("%02x", b));
}
return checksumBuilder + "###1";
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
This is onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String result = null;
if (data != null) {
result = data.getStringExtra("key_txn_result");
if (data.getExtras() != null && data.getExtras().keySet().size() > 0) {
Log.i(TAG, "result has extras");
for (String key : data.getExtras().keySet()) {
Log.i(TAG, key + " = " + data.getExtras().get(key));
}
} else {
Log.i(TAG, "result has 0 extras");
}
}
if (resultCode != Activity.RESULT_CANCELED) {
Toast.makeText(this, "Result: " + result, Toast.LENGTH_SHORT).show();
} else {
if (requestCode == 777 && data != null && data.getExtras().containsKey("key_error_result"))
result = data.getStringExtra("key_error_result");
Toast.makeText(this, "Failed: " + result, Toast.LENGTH_SHORT).show();
}
}
I am not able to make payment using PhonePe gateway. I tried all the things and now I am fed up with all this. Do anyone have a solution to this issue. Please let me know then. I am getting the below error in toast:
Failed:{"success":false,"code":"INTERNAL_SECURITY_BLOCK",message":"Internal Security Block","data":{}}
Upvotes: 0
Views: 1859
Reputation: 21
You are encountering this issue because PhonePE has some security policies.
The solution is:
Set=> Referrer-policy="strict-origin-when-cross-origin"
Upvotes: 2