支付宝支付应该是大多数app中所需的功能,之前做的app 没有集成过,现在的app 中需要支付功能,所以研究了下支付宝支付,很简单,没有想象中的那么难,不说废话,说说集成过程
1下载支付宝支付sdk 下在下来,放到libs目录下
2在build.gradle 中添加依赖
compile files('libs/alipaySdk-20170922.jar')
3将支付宝文档中的权限和所需的activity复制到清单文件中
4将后台所需要的数据传给后台,这样后天会返给你商户privatekey和订单的加签数据等,然后我们用后天返回的这些数据去掉支付宝,代码如下
private void goPay(final String orderInfo) { // final String orderInfo = "alipay_sdk=alipay-sdk-php-20161101&app_id=2017080308017822&biz_content=%7B%22body%22%3A%22%5Cu901b%5Cu8857%5Cu7f51%5Cu652f%5Cu4ed8%22%2C%22subject%22%3A%22%5Cu901b%5Cu8857%5Cu7f51%5Cu652f%5Cu4ed8%22%2C%22out_trade_no%22%3A%22GJW20171012000032%22%2C%22total_amount%22%3A0.01%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2Fwww.app.guangjiew.com%2Fchi-pay%2Fpay-order-notifiy%2F&sign_type=RSA2×tamp=2017-10-12+16%3A39%3A08&version=1.0&sign=iadkEGYXzzNh9SiaVXXC8Ou0PrtiwslvftNWviUlXrEOIsQKcs1WW5%2Ba5in0WLB0RXDltIzKJijpqxm9VVB6H0D7ARDNIQOVGV0dXYLrdjBKEG%2BtVVbzrBP8PbBN45Nvg0LyhZvbTAH6a5EP4xjN8ucIHhWsdmkNlh%2FyLi7Twal6EkAP2pVHbqkNHmsldoyiCs902BKiImZ9pQt6m42ia1aTSPtoiswP2bv4zRBMWREZJei8nTrLxjVpWUsplqKtBBfZU3EyBilgbNAgKyiBkRuiCm42sLIpEGkEbBxRPmEKpsyGW9y4yCxZfe0VMjCmXl95bosH6GvCfuCO1oXbPw%3D%3D"; // 订单信息 Runnable payRunnable = new Runnable() { @Override public void run() { PayTask alipay = new PayTask(SureOrderActivity.this); Map<String, String> stringStringMap = alipay.payV2(orderInfo, true); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = stringStringMap; mHandler.sendMessage(msg); } }; // 必须异步调用 Thread payThread = new Thread(payRunnable); payThread.start(); }5 支付结果的获取和处理@SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @SuppressWarnings("unused") public void handleMessage(Message msg) { switch (msg.what) { case SDK_PAY_FLAG: { @SuppressWarnings("unchecked") PayResult payResult = new PayResult((Map<String, String>) msg.obj); /** 对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。 */ String resultInfo = payResult.getResult();// 同步返回需要验证的信息 String resultStatus = payResult.getResultStatus(); // 判断resultStatus 为9000则代表支付成功 if (TextUtils.equals(resultStatus, "9000")) { // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。 Toast.makeText(SureOrderActivity.this, "支付成功", Toast.LENGTH_SHORT).show(); } else if (TextUtils.equals(resultStatus,"6001")){ Toast.makeText(SureOrderActivity.this, "用户中途取消", Toast.LENGTH_SHORT).show(); }else if (TextUtils.equals(resultStatus,"8000")){ Toast.makeText(SureOrderActivity.this, "正在处理中,支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态", Toast.LENGTH_SHORT).show(); }else if (TextUtils.equals(resultStatus,"4000")){ Toast.makeText(SureOrderActivity.this, "订单支付失败", Toast.LENGTH_SHORT).show(); Bundle bundle = new Bundle(); bundle.putString("username","daifukuan"); JumperUtils.JumpTo(SureOrderActivity.this, AllOrder.class,bundle); }else if (TextUtils.equals(resultStatus,"5000")){ Toast.makeText(SureOrderActivity.this, "重复请求", Toast.LENGTH_SHORT).show(); }else if (TextUtils.equals(resultStatus,"6002")){ Toast.makeText(SureOrderActivity.this, "网络连接出错", Toast.LENGTH_SHORT).show(); }else if (TextUtils.equals(resultStatus,"6004")){ Toast.makeText(SureOrderActivity.this, "支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态", Toast.LENGTH_SHORT).show(); } else { // 该笔订单真实的支付结果,需要依赖服务端的异步通知。 Toast.makeText(SureOrderActivity.this, "其它支付错误", Toast.LENGTH_SHORT).show(); } break; } default: break; } }; };还有就是PayResult这个类sdk中并未有,需要到demo中找,先在我把这个类直接粘在这import java.util.Map; public class PayResult { private String resultStatus; private String result; private String memo; public PayResult(Map<String, String> rawResult) { if (rawResult == null) { return; } for (String key : rawResult.keySet()) { if (TextUtils.equals(key, "resultStatus")) { resultStatus = rawResult.get(key); } else if (TextUtils.equals(key, "result")) { result = rawResult.get(key); } else if (TextUtils.equals(key, "memo")) { memo = rawResult.get(key); } } } @Override public String toString() { return "resultStatus={" + resultStatus + "};memo={" + memo + "};result={" + result + "}"; } /** * @return the resultStatus */ public String getResultStatus() { return resultStatus; } /** * @return the memo */ public String getMemo() { return memo; } /** * @return the result */ public String getResult() { return result; } }这样基本支付宝就集成了,如果还有什么问题可以去官网查看文档,希望这篇文章能帮到需要的人