本文介绍了通过应用程序的Android Paypal退款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,如果服务器关闭,我们需要对当前进行的交易entirely through the application进行退款".是否有可能 ?以下Paypal Refund API需要从商家帐户访问oAuth的访问令牌,但我看不到任何示例是否可以在移动端从商家获取oAuth? sdk示例主要在服务器端执行此操作. PaypalRefund

I have a scenario , where if the server is down , we need to do the "Refund" of currently made transaction entirely through the application. Is it possible ?The following paypal Refund API requires Access token of oAuth from the merchant account , but i dont see any example if oAuth from merchant is possible on mobile end ? the sdk examples are doing this at server end mostly.PaypalRefund

"PaypalHere SDK"允许这样做,但该API与常规API不同贝宝(Paypal)API.

"PaypalHere SDK" allows this , but that API is different from regularPaypal API.

PaypalHereApi

推荐答案

最后,我找到了通过应用程序贝宝退款的解决方案.它包含三个非常简单的步骤.

Finally i found a solution for paypal refund through app.It comprises of three very simple steps.

  1. 针对商家的客户ID和机密获取访问令牌.

  1. Get Access Token against merchant's Client id and secret.

从付款ID获取交易明细

Get Transaction details from the Payment Id

以下是以上三个步骤的工作代码.

Following is the working code for the above three steps.

第一步:

 public class GetAccessToken extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        materialProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(String... strings) {
        StringBuffer stringBuffer = new StringBuffer("");
        try {
            URL url = new URL("https://api.sandbox.paypal.com/v1/oauth2/token");
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setRequestMethod("POST");
            httpsURLConnection.addRequestProperty("Accept", "application/json");
            httpsURLConnection.addRequestProperty("Accept-Language", "en_US");
            httpsURLConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            String basicAuth = "Basic " + base64;
            httpsURLConnection.setRequestProperty("Authorization", basicAuth);

            String data = "grant_type=client_credentials";

            OutputStreamWriter outputWriter = new OutputStreamWriter(httpsURLConnection.getOutputStream());
            outputWriter.write(data);
            outputWriter.flush();
            outputWriter.close();

            Log.d(TAG, "Response Code; " + httpsURLConnection.getResponseCode());

            InputStream is;

            int status = httpsURLConnection.getResponseCode();

            if (status >= 400)
                is = httpsURLConnection.getErrorStream();
            else
                is = httpsURLConnection.getInputStream();

            int read = -1;
            byte[] buffer = new byte[512];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            while ((read = is.read(buffer)) > 0) {
                baos.write(buffer, 0, read);
                baos.flush();
            }

            stringBuffer.append(new String(baos.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        materialProgressBar.setVisibility(View.GONE);
        onGettingAccessToken(s);
    }
}

第2步:

public class GetTransactionDetail extends AsyncTask<String, Void, String> {

    private static final String URL = " https://api.sandbox.paypal.com/v1/payments/payment/%s";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        materialProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(String... strings) {
        String address = String.format(URL, strings[0]);
        StringBuffer stringBuffer = new StringBuffer("");
        try {
            URL url = new URL(address);
            Log.d(TAG, address);
            showLog(" Payment Id =" + strings[0] + " TOken = " + strings[1]);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setRequestMethod("GET");
            httpsURLConnection.addRequestProperty("Content-Type", "application/json");
            String basicAuth = "Bearer " + strings[1];
            Log.d(TAG, basicAuth);
            httpsURLConnection.setRequestProperty("Authorization", basicAuth);
            Log.d(TAG, "Response Code; " + httpsURLConnection.getResponseCode());
            Log.i(TAG, "************GETTING TRANSACTIN  DETAILS ASYNC a********");

            Log.i(TAG, "Payment ID =" + strings[0] + " Access Token = " + strings[1]);


            InputStream is;

            int status = httpsURLConnection.getResponseCode();

            if (status >= 400)
                is = httpsURLConnection.getErrorStream();
            else
                is = httpsURLConnection.getInputStream();

            int read = -1;
            byte[] buffer = new byte[512];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            while ((read = is.read(buffer)) > 0) {
                baos.write(buffer, 0, read);
                baos.flush();
            }

            stringBuffer.append(new String(baos.toByteArray()));
            showLog("Transaction Detail =" + stringBuffer.toString());
        } catch (Exception e) {
            e.printStackTrace();
            showLog("Exception " + e.toString());
        }
        return stringBuffer.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        materialProgressBar.setVisibility(View.GONE);
        // parse the json
        onTransactionDetails(s);
    }
}

第3步:

public class RefundPayment extends AsyncTask<String, Void, String> {

    private static final String URL = "https://api.sandbox.paypal.com/v1/payments/sale/%s/refund";
    private static final String DATA = "{\"amount\":{\"total\": %s,\"currency\": \"%s\"}}";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        materialProgressBar.setVisibility(View.VISIBLE);
        showToastAlpha("Starting Payment Refund...");
       /* progressDialog.setMessage("Please wait...");
        progressDialog.show();*/
    }

    @Override
    protected String doInBackground(String... strings) {
        String address = String.format(URL, strings[0]);
        String data;
        if (strings[1] == null || strings[2] == null) {
            data = "{}";
        } else {
            data = String.format(DATA, strings[1], strings[2]);
        }

        StringBuffer stringBuffer = new StringBuffer("");
        try {
            java.net.URL url = new URL(address);
            Log.d(TAG, address);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setRequestMethod("POST");
            httpsURLConnection.addRequestProperty("Accept", "application/json");
            httpsURLConnection.addRequestProperty("Accept-Language", "en_US");
            httpsURLConnection.addRequestProperty("Content-Type", "application/json");
            String basicAuth = "Bearer " + strings[3];
            Log.d(TAG, basicAuth);
            httpsURLConnection.setRequestProperty("Authorization", basicAuth);
            Log.i(TAG, "************GETTING REFUND PAYMENT a********");

            Log.i(TAG, "SAle id =" + strings[0] + " Amount to Refund = " + strings[1] + " Currency =" + strings[2] + " Access token  = " + strings[3]);


            OutputStreamWriter outputWriter = new OutputStreamWriter(httpsURLConnection.getOutputStream());
            Log.d(TAG, "Sending: " + data);
            outputWriter.write(data);
            outputWriter.flush();
            outputWriter.close();

            Log.d(TAG, "Response Code; " + httpsURLConnection.getResponseCode());

            InputStream is;

            int status = httpsURLConnection.getResponseCode();

            if (status >= 400)
                is = httpsURLConnection.getErrorStream();
            else
                is = httpsURLConnection.getInputStream();

            int read = -1;
            byte[] buffer = new byte[512];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            while ((read = is.read(buffer)) > 0) {
                baos.write(buffer, 0, read);
                baos.flush();
            }

            stringBuffer.append(new String(baos.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        materialProgressBar.setVisibility(View.GONE);
        onRefundPayment(s);
    }
}

这篇关于通过应用程序的Android Paypal退款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:11
查看更多