【问题标题】:Android Paypal refund through AppAndroid Paypal 通过 App 退款
【发布时间】:2016-09-05 18:13:10
【问题描述】:

我有一个场景,如果服务器宕机,我们需要对当前进行的交易entirely through the application进行“退款”。可能吗 ? 以下 paypal Refund API 需要来自商家帐户的 oAuth 访问令牌,但我没有看到任何示例,如果来自商家的 oAuth 在移动端是可能的? sdk 示例主要在服务器端执行此操作。 PaypalRefund

“PaypalHere SDK”允许这样做,但该 API 与常规 API 不同 贝宝 API。

PaypalHereApi

【问题讨论】:

    标签: android paypal


    【解决方案1】:

    我终于通过应用找到了paypal 退款的解决方案。 它包括三个非常简单的步骤。

    1. 根据商家的客户端 ID 和密码获取访问令牌。

    2. 从付款 ID 获取交易详情

    3. 使用第二部分中获取的销售 ID 退款。

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

    第一步:

     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);
        }
    }
    

    【讨论】:

    • 第 1 步中的 base64 是什么?
    • 如果有人破解安卓应用他会得到paypal客户端ID和密码?我认为这对安全性来说太糟糕了。
    • 当然不应该把它们这样放在应用程序中,这只是一个关于如何退款的示例。可以进一步改进。
    猜你喜欢
    • 2013-09-26
    • 2015-01-28
    • 2015-12-23
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多