Code Explanation

Note

Your App must use the latest security standards to prevent your code being compromised.

Initiate the “com.test.pg.secure.pgsdkv4.PaymentParams” CLASS to set the payment parameters:

PaymentParams pgPaymentParams = new PaymentParams();
pgPaymentParams.setAPiKey(SampleAppConstants.PG_API_KEY);
pgPaymentParams.setAmount(SampleAppConstants.PG_AMOUNT);
pgPaymentParams.setEmail(SampleAppConstants.PG_EMAIL);
pgPaymentParams.setName(SampleAppConstants.PG_NAME);
pgPaymentParams.setPhone(SampleAppConstants.PG_PHONE);
pgPaymentParams.setOrderId(SampleAppConstants.PG_ORDER_ID);
pgPaymentParams.setCurrency(SampleAppConstants.PG_CURRENCY);
pgPaymentParams.setDescription(SampleAppConstants.PG_DESCRIPTION);
pgPaymentParams.setCity(SampleAppConstants.PG_CITY);
pgPaymentParams.setState(SampleAppConstants.PG_STATE);
pgPaymentParams.setAddressLine1(SampleAppConstants.PG_ADD_1);
pgPaymentParams.setAddressLine2(SampleAppConstants.PG_ADD_2);
pgPaymentParams.setZipCode(SampleAppConstants.PG_ZIPCODE);
pgPaymentParams.setCountry(SampleAppConstants.PG_COUNTRY); 
pgPaymentParams.setReturnUrl(SampleAppConstants.PG_RETURN_URL);
pgPaymentParams.setMode(SampleAppConstants.PG_MODE);
pgPaymentParams.setUdf1(SampleAppConstants.PG_UDF1);
pgPaymentParams.setUdf2(SampleAppConstants.PG_UDF2);
pgPaymentParams.setUdf3(SampleAppConstants.PG_UDF3);
pgPaymentParams.setUdf4(SampleAppConstants.PG_UDF4);
pgPaymentParams.setUdf5(SampleAppConstants.PG_UDF5);
pgPaymentParams.setPaymentHostname(SampleAppConstants.PG_PAYMENT_HOSTNAME);

Initailize the com.test.pg.secure.pgsdkv4.PaymentGatewayPaymentInitializer class with payment parameters and initiate the payment:

PaymentGatewayPaymentInitializer pgPaymentInitialzer = new PaymentGatewayPaymentInitializer(pgPaymentParams,MainActivity.this);
pgPaymentInitialzer.initiatePaymentProcess();

To receive the json response, override the onActivityResult() using the REQUEST_CODE and PAYMENT_RESPONSE variables from com.test.pg.secure.pgsdkv4.PaymentParams class

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PGConstants.REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                try {
                    String sdkResponse = data.getStringExtra(PGConstants.SDK_PAYMENT_RESPONSE);
                    if (sdkResponse == null) {
                        System.out.println("Transaction Error!");
                        transactionIdView.setText("Transaction ID: NIL");
                        transactionStatusView.setText("Transaction Status: Transaction Error!");
                    } else {
                        JSONObject response = new JSONObject(sdkResponse);
                        if (response.has("payment_response") && response.getString("status").equals("success")) {
                            JSONObject paymentResponse = new JSONObject(response.getString("payment_response"));
                            System.out.println("payment_response" + paymentResponse.toString());
                            transactionIdView.setText("Transaction ID: " + paymentResponse.getString("transaction_id"));
                            transactionStatusView.setText("Transaction Status: " + paymentResponse.getString("response_message"));
                        } else {
                            transactionIdView.setText("Transaction Status: " + response.getString("status"));
                            transactionStatusView.setText("Transaction Message: " + response.getString("error_message"));
                        }
                    }

                } catch (Exception ep) {
                    ep.printStackTrace();
                }
            }
        }
    }