Search code examples
androidtransactionspaypalpaypal-ipnnotify

Set notify url to paypal payment in my android apps


Normally on websites, we can set a notify URL to check payment with paypal server. However how do we set it in an app? I am super new to paypal app payment.

I am using MEP.PayPal

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(resultCode) {
    case Activity.RESULT_OK:
        String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
        data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
        Toast.makeText(this,"Paymnet Successful : " + payKey,Toast.LENGTH_LONG).show();
        //finish();
        break;
    case Activity.RESULT_CANCELED:
        Toast.makeText(this,"Paymnet Cancel",Toast.LENGTH_LONG).show();
        //finish();
        break;
    case PayPalActivity.RESULT_FAILURE:
        Toast.makeText(this,"Paymnet Failed",Toast.LENGTH_LONG).show();
        String errorID = 
        data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
        String errorMessage = 
        data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
        //Toast.makeText(getApplicationContext(),errorMessage, Toast.LENGTH_LONG).show();
        //finish();
        break;
    }
}

So under Activity.RESULT_OK, I would like to send a post to my notify URL so that the codes in the URL can validate payments.

Also, I get that the paykey can return the transaction details. If so, then I will use it to do the verification instead of using notify url. But after doing much reading, I still could not find the answer to return the transaction details, like maybe data.getStringExtra('transaction_id'), data.getStringExtra('merchant_name'), etc.


Solution

  • After reading this post here Transaction details from paypal using Pay-key in android

    I am able to come up with my solution. But I will have to create ASYNC class to make it work.

    So it is something like this

    public class MyPaypalActivity extends Activity implements OnClickListener {
    String resultExtra;
    
    //some methods you can fill in yourself to integrate with paypal
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(resultCode) {
        case Activity.RESULT_OK:
            String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
            data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
            resultExtra = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
            new RetreivePaypalData().execute(resultExtra);
    
            break;
        case Activity.RESULT_CANCELED:
    
            break;
        case PayPalActivity.RESULT_FAILURE:
            Toast.makeText(this,"Paymnet Failed",Toast.LENGTH_LONG).show();
            String errorID = 
            data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
            String errorMessage = 
            data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
    
            break;
        }
    }
    
        class RetreivePaypalData extends AsyncTask<String, Void, String> {
    
               private Exception exception;
    
               protected String doInBackground(String... args) {
                   try{
    
                       HttpPost httppost = null;
                       httppost = new HttpPost("https://svcs.sandbox.paypal.com/AdaptivePayments/PaymentDetails");
    
    
    
                       httppost.setHeader("X-PAYPAL-SECURITY-USERID", "mybusiness_blalbal.fff.cm");
                       httppost.setHeader("X-PAYPAL-SECURITY-PASSWORD", "3243535554");
                       httppost.setHeader("X-PAYPAL-SECURITY-SIGNATURE", "AR908E09RG7H0G0GREJ3GH9GHGHDFKGHGIHE99GFDIGHIH34ERGHI");
                       httppost.setHeader("X-PAYPAL-APPLICATION-ID", "APP-80W284485P519543T");
                       httppost.setHeader("X-PAYPAL-REQUEST-DATA-FORMAT", "NV");
                       httppost.setHeader("X-PAYPAL-RESPONSE-DATA-FORMAT", "NV");
    
    
    
    
    
    
    
    
                           // Add your data
                           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
                           nameValuePairs.add(new BasicNameValuePair("payKey", resultExtra));
                           nameValuePairs.add(new BasicNameValuePair("requestEnvelope.errorLanguage", "en_US"));
    
                           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                       HttpResponse response = Client.getInstance().execute(httppost);
                       HttpEntity entity = response.getEntity();
                       InputStream is = entity.getContent();
                       byte[] data1;
                       data1 = new byte[256];
                       StringBuffer buffer = new StringBuffer();
                       int len = 0;
                       while (-1 != (len = is.read(data1)) )
                         {
                            buffer.append(new String(data1, 0, len));
                         }
                       Log.e("log_tag",""+buffer.toString());
    
                       //Make the comparison case-insensitive.
                       is.close();
                       }catch(Exception e)
                       {
                           Log.e("Got Error","error "+ e );
                       }
                       Log.e("Paykey","resultExtra "+ resultExtra );
                       return null;
               }
    
               protected void onPostExecute(String d) {
                   // TODO: check this.exception 
                   // TODO: do something with the data
               }
            }
    }
    

    Just a rough solution. Need to tweak.