Error while integrate phonepe payment gateway in Android

3.2k views Asked by At

I am integrating all steps based on the link https://developer.phonepe.com/v1/docs/android-pg-sdk-integration.Now I am getting "Something went wrong:400"

Expect:trying to open the upi apps,And after payment Success Response Required.Here my inegrated codes

data.put("merchantTransactionId", getTransactionId);
data.put("merchantId", R.string.merchantid);
data.put("merchantUserId","ABC12345"));
data.put("amount", 2);                           
data.put("mobileNumber", "123456789");           
data.put("callbackUrl", "https://webhook.site/callback-url");    
data.put("redirectMode", "POST");    
data.put("redirectUrl", "https://webhook.site/redirect-url");    

 PaymentInstrument mPaymentInstrument = new PaymentInstrument();
 mPaymentInstrument.setType("UPI_INTENT");            
 mPaymentInstrument.setTargetApp("com.phonepe.app");   
 data.put("paymentInstrument", mPaymentInstrument);    

  DeviceContext mDeviceContext = new DeviceContext();
  mDeviceContext.setDeviceOs("ANDROID");           
  data.put("deviceContext", mDeviceContext);  

String json = gson.toJson(data);
String getdatas = new String(Base64.encode(json.getBytes(),Base64.DEFAULT), Base64.DEFAULT); 

       try {
        md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        String input = (getdatas + apiEndPoint + salt) ;
        // compute the hash of the input string
        byte[] hash = md.digest(input.getBytes());
        // convert the hash to a hexadecimal string
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            hexString.append(String.format("%02x", b));
        }
        String checksum = String.valueOf(hexString);
        String checkSumResult=checksum+ "###" + saltIndex;
      
        B2BPGRequest b2BPGRequest = new B2BPGRequestBuilder().setData(getdatas).setChecksum(checkSumResult).setUrl(apiEndPoint)
                .build();
        try {
            startActivityForResult(PhonePe.getImplicitIntent(this, b2BPGRequest, "com.phonepe.app"), B2B_PG_REQUEST_CODE);
        } catch (PhonePeInitException e) {
            
        }

Here my Manifest File

   <meta-data
            android:name="com.phonepe.android.sdk.Debuggable"
            android:value="true"/>
        <!-- Keep it true in UAT environment for both the flags-->
        <meta-data
            android:name="com.phonepe.android.sdk.isUAT"
            android:value="true"/>
        <meta-data
            android:name="com.phonepe.android.sdk.isSimulator"
            android:value="true"/>
        <!-- The value will be the MID-->
        <meta-data
            android:name="com.phonepe.android.sdk.MerchantId"
            android:value="@string/merchantid"/>
1

There are 1 answers

1
Shubham Pandey On

You are using testing integration for that you have to use simulator app package name. For access of simualtor mail phonepe team they will provide you. Once u will use it with that it will work.

    val data = JSONObject()
        data.put("merchantTransactionId", MERCHANT_TID)//String. Mandatory

        data.put("merchantId" , MERCHANT_ID) //String. Mandatory

         data.put("amount", 200 )//Long. Mandatory

         data.put("mobileNumber", "7908834635") //String. Optional

         data.put("callbackUrl", "https://webhook.site/callback-url") //String. Mandatory

        val paymentInstrument = JSONObject()
        paymentInstrument.put("type", "UPI_INTENT")
        paymentInstrument.put("targetApp", "com.phonepe.simulator")

         data.put("paymentInstrument", paymentInstrument )//OBJECT. Mandatory


        val deviceContext = JSONObject()
        deviceContext.put("deviceOS", "ANDROID")
         data.put("deviceContext", deviceContext)


//        val base64Body = android.util.Base64(Gson().toJson(data))

        val payloadBase64 = android.util.Base64.encodeToString(
            data.toString().toByteArray(Charset.defaultCharset()), android.util.Base64.NO_WRAP
        )

        val checksum = sha256(payloadBase64 + apiEndPoint + salt) + "###1";

        Log.d("PAPAYACODERS", "onCreate: $payloadBase64")
        Log.d("PAPAYACODERS", "onCreate: $checksum")

        val b2BPGRequest = B2BPGRequestBuilder()
            .setData(payloadBase64)
            .setChecksum(checksum)
            .setUrl(apiEndPoint)
            .build()


        val button = findViewById<Button>(R.id.button)
        button.setOnClickListener {
            //For SDK call below function


            Log.d("PAPAYACODERS", "onCreate: $payloadBase64")
            Log.d("PAPAYACODERS", "onCreate: $checksum")

            try {
                PhonePe.getImplicitIntent(this, b2BPGRequest, "com.phonepe.simulator")
                    ?.let { startActivityForResult(it, 1) };
            } catch (e: PhonePeInitException) {
            }

        }

Also here is an youtube video you can check it help https://youtu.be/NJsmpk42QKs?si=HMWkyk7aORBt0kXO

Here is github repo https://github.com/1902shubh/PhonepeSdkAndroid

Also from android app don't try to use api hitting directly of phonepe it will not work. For that you have to user server side integration. IN Android you have to only use SDK integration. If you have any confusion let me know.