效果图
实现方案
- jcenter 集成方式
implementation 'com.xgr.easypay:EasyPay:2.0.5' // 基类库,必选 implementation 'com.xgr.easypay:wechatpay:2.0.5' // 微信支付,可选 implementation 'com.xgr.easypay:alipay:2.0.5' // 支付宝支付,可选
2. 服务器端提前准备好接口,能保证正常,这个项目php,参考
public function pay(){ ***************************略Db::startTrans();try{ Db::name('libu_order')->insert($param); // 提交事务Db::commit(); } catch (\Exception $e) {// 回滚事务Db::rollback();$this->error("提交失败!"); }$payname= $paydata['nickname'];$request = Request::instance();$epay = get_addon_info('epay');if ($epay && $epay['state']) { $notifyurl = $request->root(true) . '/api/spay/epay/type/notify/paytype/' .$payname;$returnurl = $request->root(true) . '/api/spay/epay/type/return/paytype/' .$payname. '/orderid/' . $param['orderNo'];$params = ['amount' => $param['amount'],'orderid' => $param['orderNo'],'type' => $payname,'title' => "支付元",'notifyurl' => $notifyurl,'returnurl' => $returnurl,'method' => "app", ];return \addons\epay\library\Service::submitOrder($params);}else {//$result = \think\Hook::listen('cms_order_submit', $order);if (!$result) {throw new OrderException("请在后台安装配置微信支付宝整合插件");}}**************************略 }
3. 支付宝和微信支付返回的数据比较特殊,不能进行封装处理,否则一定报错;而且返回的格式气人,所以android端重新封装了请求。
final static String payurl="改你自己的";public static Callback pay(String payType,Callback callback){String timeString = UserManager.tokeToken(MyRealApplication.getApplication());//1.创建OkHttpClient对象OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new TokenInterceptor(timeString)).build();//2.创建一个RequestBody,可以用add添加键值对RequestBody requestBody = new FormBody.Builder().add("paytype",payType).add("amount","6").add("extro","VIP充值").build();//3.创建Request对象,设置URL地址,将RequestBody作为post方法的参数传入Request request = new Request.Builder().url(payurl).post(requestBody).build();//4.创建一个call对象,参数就是Request请求对象okHttpClient.newCall(request).enqueue(callback);return callback;}
4. 支付宝返回的是字符串,可直接使用;微信返回的是个对象,所以需要创建个对象,注意创建对象的时候不能用package这个字段,但是微信支付返回的信息中有这个字段,需要特殊处理下。
@SerializedName("package")
public class WeChatBean {public String appid;public String partnerid;public String prepayid;public String timestamp;public String noncestr;public String sign;@SerializedName("package")public String packagevalue; }
5. 按钮支付相应事件pay(),注意使用中,在处理业务过程中,需要放在
Looper.prepare()和
Looper.loop()中间,否则报错。
public void pay(){Map<String, String> params = new HashMap<>();***************************OkHttpTool.pay(String.valueOf(checkedIds),new Callback() {@Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {var responsedata=response.body().string();if(responsedata!=null){Log.d("支付返回", responsedata);Looper.prepare();if(checkedIds==18){alipay(responsedata);}else {Gson gson = new Gson();WeChatBean one = gson.fromJson(responsedata, WeChatBean.class);wxpay(one);Log.d("微信支付返回", one.packagevalue);}Looper.loop();}Log.d("onResponse", responsedata);}@Override public void onFailure(@NonNull Call call, @NonNull IOException e) {}});}
欢迎大家点赞、收藏、关注、评论、批评啦