paypal android app,Android Paypal refund through App

本文详细介绍了如何通过PayPal API在应用程序中实现退款操作,包括获取Access Token、获取交易详情以及发起退款请求三个关键步骤。通过示例代码展示了如何使用HTTPSURLConnection进行API调用,实现客户端的授权、请求发送及响应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Finally i found a solution for paypal refund through app.

It comprises of three very simple steps.

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

Get Transaction details from the Payment Id

Refund Payment using the sale id fetched in the second part.

Following is the working code for the above three steps.

Step1:

public class GetAccessToken extends AsyncTask {

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

}

}

Step 2 :

public class GetTransactionDetail extends AsyncTask {

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

}

}

Step 3:

public class RefundPayment extends AsyncTask {

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值