Android集成PhonePe支付网关遇404错误求助
Android集成PhonePe支付网关遇404错误排查
尝试为Android应用集成PhonePe支付网关,编写代码如下,但调用后未获取预期响应,仅返回错误信息:
something went wrong :404
实现代码
public class PhonePePayActivity extends BaseActivity { String TAG ="PhonePePayActivity"; private static int B2B_PG_REQUEST_CODE = 777; @Override protected int getLayoutId() { return R.layout.activity_phone_pe_pay; } @Override public void setView() { super.setView(); } String apiEndPoint = "/pg/v1/pay"; String SaltKey = "a6334ff7-da0e-4d51-a9ce-76b97d518b1e"; int KeyIndex = 1; @RequiresApi(api = Build.VERSION_CODES.O) @OnClick({R.id.pay_button}) public void onClick(View v) { switch (v.getId()) { case R.id.pay_button: HashMap<String, Object> data = new HashMap(); data.put("merchantTransactionId",createTrnId()); //String. Mandatory data.put("merchantId", "ON24MARTUAT"); //String. Mandatory data.put("merchantUserId", "u123"); //String. Conditional data.put("amount",100); //Long. Mandatory data.put("mobileNumber","8510988965"); //String. Optional data.put("callbackUrl","https://webhook.site/callback-url"); PaymentInstrumentPe mPaymentInstrument = new PaymentInstrumentPe(); mPaymentInstrument.setType("UPI_INTENT"); mPaymentInstrument.setTargetApp("com.phonepe.app"); data.put("paymentInstrument",mPaymentInstrument); DeviceContextPe mDeviceContextPe = new DeviceContextPe(); mDeviceContextPe.setDeviceOS("ANDROID"); // ENUM. Mandatory data.put("deviceContext", mDeviceContextPe); //OBJECT. Mandatory String originalInput = new Gson().toJson(data); String base64Body = Base64.getEncoder().encodeToString(originalInput.getBytes()); Log.e(TAG, "onClick: " + base64Body ); String checksum = sha256(base64Body + apiEndPoint + SaltKey) + "###"+ KeyIndex; Log.e(TAG, "onClick: "+ checksum ); B2BPGRequest b2BPGRequest = new B2BPGRequestBuilder() .setData(base64Body) .setChecksum(checksum) .setUrl(apiEndPoint) .build(); try { startActivityForResult(PhonePe.getImplicitIntent( this, b2BPGRequest, "com.phonepe.app.preprod"),B2B_PG_REQUEST_CODE); } catch(PhonePeInitException e){ Log.e(TAG, "onClick: error-" + e.toString() ); e.printStackTrace(); } break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == B2B_PG_REQUEST_CODE) { // Log.e(TAG, "onActivityResult: " +data.toString() ); /*This callback indicates only about completion of UI flow. Inform your server to make the transaction status call to get the status. Update your app with the success/failure status.*/ } } private String createTrnId() { Date date = new Date(); long createTransactionId = date.getTime(); return "on24m_"+ createTransactionId; } private static String sha256(String input) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(input.getBytes("UTF-8")); StringBuilder hexString = new StringBuilder(); for (byte b : hash) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }
排查建议
- 环境一致性检查:调用预生产环境的PhonePe客户端(
com.phonepe.app.preprod),但支付参数中的targetApp设为了生产环境的com.phonepe.app,改为com.phonepe.app.preprod,确保环境匹配。 - 参数类型修正:
amount字段官方要求为Long类型,当前代码传的100是int,显式改为100L避免类型不兼容问题。 - 校验和逻辑核对:确认校验和拼接顺序是否符合官方要求——当前用的是
base64Body + apiEndPoint + SaltKey,检查官方文档中校验和生成的具体规则,是否存在顺序错误或遗漏内容。 - 商户信息验证:确认
merchantId(ON24MARTUAT)在预生产环境已完成注册,无拼写错误;merchantTransactionId是否保证唯一性。 - 端点与权限配置:确认API端点路径
/pg/v1/pay对预生产环境有效,同时检查AndroidManifest中是否添加了PhonePe SDK所需的所有权限及Scheme配置。
参考文档
- Android PG SDK集成文档
- PAY API详情文档
- 交易状态查询API文档
- 服务器到服务器回调文档
内容的提问来源于stack exchange,提问作者Sudhir Singh
相关产品推荐
相关产品推荐

