2016-05-10 3 views
1

私はシナリオを持っています。サーバーがダウンしている場合は、現在行われている取引の「払い戻し」を行う必要がありますentirely through the application。出来ますか ? 次のpaypal払い戻しAPIには、販売者アカウントからのoAuthのアクセストークンが必要ですが、モバイル側で販売者からのoAuthが可能な場合は表示されません。 sdkの例では、サーバ側でこれを主に行っています。 PaypalRefundアプリからのペイパル払い戻し

"PaypalHere SDKは、" これを許可しますが、そのAPIは、通常の ペイパルAPIは異なっています。

PaypalHereApi

答えて

0

最後に、私はアプリを通じてPayPalの返金のための解決策を見つけました。 これは3つの非常に簡単なステップで構成されています。

  1. マーチャントのクライアントIDとシークレットに対するアクセストークンを取得します。

  2. 第二部でフェッチ販売IDを使用してお支払い番号

  3. 返金支払いから取引の詳細を取得します。

以下は、上記の3つのステップの作業コードです。

ステップ1:

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); 
    } 
} 
関連する問題