接入支付宝支付SDK

如题所述

第1个回答  2022-07-09
接入支付宝支付SDK

可以说支付宝支付接入是所有SDK最好接入的,没有之一。

客户端不用签名,也不用管包名,也不用管签名文件,就接口返回订单,把订单交给支付宝SDK调用就行,成功或者失败都在当前界面返回给你。你再去通知接口。

支付流程图

官方文档地址

!支付宝支付官方文档地址

按照文档说明接入SDK和相关配置,在这就不重复了

客户端支付关键代码===》支付接口的调用(调起支付弹框)

记住支付接口的调用必须在独立的非ui线程中执行,即需新开线程里面调用。可以想官方demo一样用new Thread方式。

下面我给出用Observable方式示例代码

在PayUtils中

/**

* desc:支付宝支付

* Created by congge on 2018/8/27 17:20

* @param orderInfo 接口返回的订单

**/

public static void aliPay(final Activity activity, final String orderInfo, final OrderListener orderListener) {

Observable.just(orderInfo)

.map(new Function () {

@Override

public String apply(String orderInfo) throws Exception {

//用户在商户app内部点击付款,是否需要一个loading做为在钱包唤起之前的过渡,这个值设置为true

return new PayTask(activity).pay(orderInfo, true);

}

})

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(new Consumer () {

@Override

public void accept(String payResult) throws Exception {

orderListener.onPayResult(payResult);

}

});

}

支付结果返回处理

返回例子:

resultStatus={9000};memo={};result={{"alipay_trade_app_pay_response":{"code":"10000","msg":"Success","app_id":"2016091300503896","auth_app_id":"2016091300503896","charset":"utf-8","timestamp":"2018-08-28 17:51:11","out_trade_no":"nVElbd74TW6WnEyxQwvX8A","total_amount":"0.01","trade_no":"2018082821001004680500208879","seller_id":"2088102175487650"},"sign":"W0Hg9k4GxL8Oaxymvqk2i65WNDQxYp6HGve32ek6VjSRnymmI3GQTjpQVbZuDzvjcwQ/HIkM97PoBGAVlTmi/wiJcqDgSSDzDY7AFnNN0OcK0ehWGwKQINA4IDGh51A7yY/vYKmR0VW+2OwGhlRPPMMZtQOEqh8a9/aIijzT6ZLwy9Hl4ayG/fVKhdC1VdckF6+C25BFNp3fIxarg5tfEunm7N9iWngKCUsnP+IZz05OHdvynimgYPcBnbBERHG97GVqRT/EdBWTQyIDMc0LemScAYxJixTVgXDkRddQjzWZ7HgLdBfjs0nXY24puHudT76ERxVY+8NkoKle/QI+FA==","sign_type":"RSA2"}}

也可以自己打log看看

处理示例代码:

//支付宝支付

PayUtils.aliPay(this, result.getSignDataStr(), new PayUtils.OrderListener() {

@Override

public void onPayResult(String payResult) {

PayResult pr = new PayResult(payResult);

String rs = pr.getResultStatus();

String r = pr.getResult();

switch (rs) {

case AliPayResultStatus.PAY_SUCCESS:

ToastUtils.show(R.string.pay_success);

//通知接口支付成功

break;

case AliPayResultStatus.PAY_PROCESSING:

case AliPayResultStatus.PAY_UNKNOWN:

ToastUtils.show(R.string.pay_fail);

//支付可能成功,要接口去查询

break;

default:

ToastUtils.show(R.string.pay_fail);

//通知接口支付失败,取消订单

}

}

});

上面方法中:

//通知接口支付成功 //支付可能成功,要接口去查询 //通知接口支付失败,取消订单。根据你产品需求要不要通知你服务器做的操作。正常是要的,用来改变订单状态

PayResult.class

public class PayResult {

private String resultStatus;

private String result;

private String memo;

public PayResult(String rawResult) {

if (TextUtils.isEmpty(rawResult))

return;

String[] resultParams = rawResult.split(";");

for (String resultParam : resultParams) {

if (resultParam.startsWith("resultStatus")) {

resultStatus = gatValue(resultParam, "resultStatus");

}

if (resultParam.startsWith("result")) {

result = gatValue(resultParam, "result");

}

if (resultParam.startsWith("memo")) {

memo = gatValue(resultParam, "memo");

}

}

}

@Override

public String toString() {

return "resultStatus={" + resultStatus + "};memo={" + memo

+ "};result={" + result + "}";

}

private String gatValue(String content, String key) {

String prefix = key + "={";

return content.substring(content.indexOf(prefix) + prefix.length(),

content.lastIndexOf("}"));

}

public String outOrder() {

String order = ""out_trade_no"";

if (result.contains(order)) {

String begin = result.substring(result.indexOf(order));

String ss = begin.split(",")[0];

String newS = ss.replace(""", "")

.replace("}", "")

.replace(":", "")

.replace("out_trade_no", "");

try {

return newS;

} catch (Exception e) {

e.printStackTrace();

}

}

return "";

}

/**

* @return the resultStatus

*/

public String getResultStatus() {

return resultStatus;

}

/**

* @return the memo

*/

public String getMemo() {

return memo;

}

/**

* @return the result

*/

public String getResult() {

return result;

}}

最后给下支付返回码表

AliPayResultStatus.class

public class AliPayResultStatus {

/**

* 订单支付成功,唯一肯定是支付成功的

*/

public static final String PAY_SUCCESS = "9000";

/**

* 正在处理中,支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态

*/

public static final String PAY_PROCESSING = "8000";

/**

* 订单支付失败

*/

public static final String PAY_FAIL = "4000";

/**

* 重复请求

*/

public static final String PAY_REPEAT = "5000";

/**

* 用户中途取消

*/

public static final String PAY_PROCESS_CANCEL = "6001";

/**

* 网络连接出错

*/

public static final String PAY_NET_ERROR = "6002";

/**

* 支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态

*/

public static final String PAY_UNKNOWN = "6004";}

还有一个直接弃用沙箱调试模式,否则提示支付失败也有可能不知道错在那,怕金额大,和接口商量,测试服务器就用0.01测试。
相似回答