在android中代表第三方提交PayPal发票的最佳方式是什么?

时间:2015-12-06 08:45:28

标签: android paypal android-volley

我一直在尝试使用Volley来点击Invoice REST服务。想知道我是否还需要使用无数的PayPal SDK之一,除了使这项工作或我应该自己动手。任何例子都将不胜感激。

非常感谢, 大卫

1 个答案:

答案 0 :(得分:0)

好吧,我至少已成功使用Volley对PayPal REST服务进行身份验证。这是一个可以帮助其他Volley用户的片段。有关设置和使用Volley的更多信息:http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

                    String tag_string_req = "string_req";
                    String url = "https://api.sandbox.paypal.com/v1/oauth2/token";

                    final ProgressDialog pDialog = new ProgressDialog(context);
                    pDialog.setMessage("Requesting Authorization...");
                    pDialog.show();

                    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                            url,
                            new Response.Listener<JSONObject>() {

                                @Override
                                public void onResponse(JSONObject response) {

                                    try {
                                        createInvoice(response.get("access_token").toString());
                                    } catch (JSONException e) {
                                        Log.e("MFM", e.getLocalizedMessage());
                                    }
                                    //Toast.makeText(context, response.toString(), Toast.LENGTH_LONG).show();
                                    pDialog.hide();
                                }
                            }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
                            pDialog.hide();
                        }
                    }) {


                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            HashMap<String, String> headers = new HashMap<>();
                            headers.put("Accept", "application/json");
                            headers.put("Accept-Language", "en_US");
                            headers.put("content-type", "application/x-www-form-urlencoded; charset=utf-8");
                            String credentials = "<client-id-from-paypal-sandbox>:<client-secret-from-paypal-sandbox>";
                            String encodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP);
                            headers.put("Authorization", "Basic " + encodedCredentials);
                            return headers;
                        }

                        @Override
                        public byte[] getBody() {
                            String httpPostBody = "grant_type=client_credentials";
                            return httpPostBody.getBytes();
                        }
                    };

                    // Adding request to request queue
                    getInstance().addToRequestQueue(jsonObjReq, tag_string_req);