上次集成支付宝支付已经很久了,今天写东西用到了支付宝支付,就大致写一下流程;
去蚂蚁金服下载最新版的Android&IOS端SDK
全部文档 -- 资源下载 -- App支付客户端
下载后解压,分为Android和IOS端,里面有demo和SDK,使用Android端
把SDK中的jar导入到工程中:
build.gradle中添加
compile files('libs/alipaySdk-20170922.jar')
AndroidManifest.xml中添加:
权限:
android:name="android.permission.READ_EXTERNAL_STORAGE" /> android:name="android.permission.CAMERA" /> android:name="android.permission.INTERNET" /> android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> android:name="android.permission.ACCESS_NETWORK_STATE" /> android:name="android.permission.ACCESS_WIFI_STATE" /> android:name="android.permission.READ_PHONE_STATE" /> android:name="android.permission.ACCESS_COARSE_LOCATION" />
配置:
android:name="com.alipay.sdk.app.H5PayActivity" android:configChanges="orientation|keyboardHidden|navigation|screenSize" android:exported="false" android:screenOrientation="behind" android:windowSoftInputMode="adjustResize|stateHidden" > android:name="com.alipay.sdk.app.H5AuthActivity" android:configChanges="orientation|keyboardHidden|navigation" android:exported="false" android:screenOrientation="behind" android:windowSoftInputMode="adjustResize|stateHidden" >
现在需要的文件已经集成,拿着你的生成的订单号去请求服务器(我们的是这样的),服务器返回给你支付宝支付的orderInfo;
使用这个orderInfo调起支付:
private final int SDK_PAY_FLAG = 1; private final int SDK_AUTH_FLAG = 2;
findViewById(R.id.tvPay).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String orderInfo = "服务器返回的OrderInfo"; // 订单信息 Runnable payRunnable = new Runnable() { @Override public void run() { PayTask alipay = new PayTask(GoodsDatailsActivity.this); Map, String> result = alipay.payV2(orderInfo,true); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; // 必须异步调用 Thread payThread = new Thread(payRunnable); payThread.start(); } });
接受返回信息,这个以服务器的为准,前端只是展示:
private static Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SDK_PAY_FLAG: { @SuppressWarnings("unchecked") PayResult payResult = new PayResult((Map, String>) msg.obj); String resultInfo = payResult.getResult();// 同步返回需要验证的信息 String resultStatus = payResult.getResultStatus(); LogUtils.i(payResult.getMemo()+"______"+payResult.getResult()+"_____"+payResult.getResultStatus()); // 判断resultStatus 为9000则代表支付成功 if (TextUtils.equals(resultStatus, "9000")) { // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。 Toast.makeText(context, "支付成功", Toast.LENGTH_SHORT).show(); Intent BuySucceed = new Intent(context ,Order_BuySucceed_Activity.class); context.startActivity(BuySucceed); } else { // 该笔订单真实的支付结果,需要依赖服务端的异步通知。 Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show(); } break; } case SDK_AUTH_FLAG: break; default: break; } } };
OK完成了;其中PayResult这个类,用支付宝的那个老是报错;
public class PayResult { private String resultStatus; private String result; private String memo; public PayResult(Map, 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); } } } public PayResult(String obj) { } @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; } }