1.支付宝,微信基础api请去官网查看;
下面是实现的code,android端签名:
重要数据在cpp层,用native获取;
接口
package org.fdcch.iotclient.view.fragment;
import android.view.View;
import java.util.Map;
/**
* 充值面板fragment接口.
* <p>由充值面板fragment类实现</p>
* @author Mr Huang 2018/6/27
* @version 1.0
*/
public interface RechargePanelView {
/**
* 初始化充值面板视图.
* <p>{@code findViewById(int)}{@link View#findViewById(int)}</p>
* @param view {@link View}
*/
void initView(View view);
/**
* 绑定数据到ui.
* <p>{@code View}{@link View}</p>
*/
void bindDataToAdpter();
/**
* 支付宝支付.
* <p>通过控制器进行处理</p>
* @param map 支付宝支付需要的参数
* @see org.fdcch.iotclient.persenter.RechargePanelPersenter#alipay(String)
* <b>支付宝协议参数文档</b> {@link <i>https://docs.open.alipay.com/204/105465/}
*/
void aliPay(Map<String,String>map);
/**
* 微信支付.
* <p>这个sdk支付有巨坑,刚开始登录和支付用同一个sdk,导致直接返回
* activity不是同一个东西,但是就不报错,在找原因,最终找到了,发现
* api不是支付的,是登录的api,填一个坑,第二个坑也是api的原因,由于
* 用的是登录的sdk,api名字一样,直接导致找不到方法,这坑也填了,签名验证
* 和官方的文档写的不一样,商户密匙不对就直接返回错误,文档上说的是不验证
* 商户密匙,这个坑留着之后的一直返回-1来填的,文档和实际的不一样,坑太多了
* 说不清多少坑.
*
* </p>
* @see com.tencent.mm.opensdk.openapi.IWXAPI {@code 对比一下下面的}
* @see com.tencent.mm.sdk.openapi.IWXAPI {@code 这个是登录的api}
* @param map 统一下单需要的参数
*/
void wechatPay(Map<String,String>map);
/**
* 二次签名.
* <p>由于微信官方文档写得很变态,基本上就故意引人入坑得,文档写得模模糊糊,直接导致签名的
* 时候跳进了大坑,是要进行二次签名的
* </p>
* @param map 二次签名需要签的参数
*/
String wechatPaySign(Map<String,String>map);
/**
* 支付信息数据上传到服务器.
* <p>通过控制器进行处理</p>
* @see org.fdcch.iotclient.persenter.RechargePanelPersenter#submitOrder(Map)
* @param userId 用户编号
* @param price 充值金额
* @param type 充值类型
*/
void aliPay(String userId,String price,String type);
/**
* @see AddGardenView#showMessage(String, String)
*
*/
void showMessage(String msg, String title,int code);
}
实现类:
package org.fdcch.iotclient.view.fragment;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.huangxudong.pay.alipay.OrderInfoUtil2_0;
import com.huangxudong.pay.wechatpay.OrderInfoUtil;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import org.fdcch.iotclient.R;
import org.fdcch.iotclient.model.AlipayBean;
import org.fdcch.iotclient.model.Constant;
import org.fdcch.iotclient.model.UploadBasketBean;
import org.fdcch.iotclient.persenter.RechargePanelPersenterImp;
import org.fdcch.iotclient.view.activity.MyApplication;
import org.fdcch.iotclient.view.activity.MyWalletActivity;
import org.fdcch.iotclient.view.adpter.RechargePanelAdpter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link RechargePanelFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link RechargePanelFragment#newInstance} factory method to
* create an instance of this fragment.
* 充值面板fragment.
* <p>主要用来显示充值面板界面</p>
* {@inheritDoc}
* @see RechargePanelView
*/
public class RechargePanelFragment extends DialogFragment implements RechargePanelView ,RechargePanelAdpter.CallBackPrice,View.OnClickListener{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private ImageView mImageViewBack;
private TextView mTextViewBack;
private GridView mGridView;
private LinearLayout mLinearLayoutPrice;
private EditText mEditTextPrice;
private Button mButtonPriceOk;
private RechargePanelAdpter mRechargePanelAdpter;
private String ALIPAY_RSA="";
private String ALIPAY_APP_ID="";
private String WECHAT_APPID="";
private String WECHAT_MCH_ID="";
private String WECHAT_KEY="";
private RechargePanelPersenterImp mRechargePanelPersenterImp;
private Handler mHandler=new Handler(new Handler.Callback() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public boolean handleMessage(Message msg) {
switch (msg.what){
case Constant.RechargePanelActivityHandleAlipay:
Map<String,String>map= (Map<String, String>) msg.obj;
String result= map.get(getString(R.string.result));
AlipayBean alipayBean=mRechargePanelPersenterImp.getAlipayToJson(result);
if (alipayBean!=null&&alipayBean.getAlipay_trade_app_pay_response()!=null){
if (alipayBean.getAlipay_trade_app_pay_response().getMsg()!=null){
if (alipayBean.getAlipay_trade_app_pay_response().getMsg().equals("Success")){
aliPay(mParam2,alipayBean.getAlipay_trade_app_pay_response().getTotal_amount(),"1");
}
}
}
break;
case Constant.RechargePanelActivityHandleWechat:
Map<String,Object> map1= OrderInfoUtil.getOrderInfoUtil().parseXml(msg.obj.toString());
Date date=new Date();
String l=date.getTime()/1000+"";
PayReq payReq=new PayReq();
payReq.appId = map1.get(getString(R.string.appid)).toString();
payReq.partnerId = map1.get(getString(R.string.mch_id)).toString();
payReq.prepayId= map1.get(getString(R.string.prepay_id)).toString();
payReq.packageValue = getString(R.string.signwxpay);
payReq.nonceStr= map1.get(getString(R.string.nonce_str)).toString();
payReq.timeStamp= l;
Map<String,String>map11=new HashMap<>();
map11.put(getString(R.string.appid),payReq.appId);
map11.put(getString(R.string.partnerid),payReq.partnerId);
map11.put(getString(R.string.prepayid),payReq.prepayId);
map11.put(getString(R.string.noncestr),payReq.nonceStr);
map11.put(getString(R.string.packagea),payReq.packageValue);
map11.put(getString(R.string.timestamp),payReq.timeStamp);
String sign= wechatPaySign(map11);
payReq.sign=sign;
iwxapi.sendReq(payReq);
break;
case Constant.RechargePanelActivityHandleSubmitOrder:
if (msg.obj==null){
mHandler.sendEmptyMessage(Constant.RechargePanelActivityHandleFaild);
return false;
}
UploadBasketBean uploadBasketBean=mRechargePanelPersenterImp.getAlipayMsgToJson(msg.obj.toString());
if (uploadBasketBean==null){
mHandler.sendEmptyMessage(Constant.RechargePanelActivityHandleFaild);
return false;
}
if (uploadBasketBean.getStatusCode()==200){
boolean b=((MyApplication)getActivity().getApplication()).isMain();
if (b){
showMessage("充值成功,请到我的钱包查看","提示信息",0);
}else {
showMessage(getString(R.string.czcg),getString(R.string.tsxx),1);
}
}else {
mHandler.sendEmptyMessage(Constant.RechargePanelActivityHandleFaild);
}
break;
case Constant.RechargePanelActivityHandleFaild:
showMessage(getString(R.string.fwqgznxdd),getString(R.string.tsxx),0);
break;
}
return false;
}
});
private String mParam1;
private String mParam2;
private IWXAPI iwxapi;
private OnFragmentInteractionListener mListener;
public RechargePanelFragment() {
// Required empty public constructor
}
static {
System.loadLibrary("native-lib");
}
private native String getALIPAY_RSA();
private native String getALIPAY_APP_ID();
private native String getWECHAT_APPID();
private native String getWECHAT_MCH_ID();
private native String getWECHAT_KEY();
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment RechargePanelFragment.
*/
public static RechargePanelFragment newInstance(String param1, String param2) {
RechargePanelFragment fragment = new RechargePanelFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
View view=inflater.inflate(R.layout.fragment_recharge_panel, container, false);
ALIPAY_APP_ID=getALIPAY_APP_ID();
ALIPAY_RSA=getALIPAY_RSA();
WECHAT_APPID=getWECHAT_APPID();
WECHAT_MCH_ID=getWECHAT_MCH_ID();
WECHAT_KEY=getWECHAT_KEY();
initView(view);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
private boolean codes=false;
public boolean isCodes() {
return codes;
}
public void setCodes(boolean codes) {
this.codes = codes;
}
@Override
public void onStart() {
super.onStart();
if (codes){
dismiss();
return;
}
DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
WindowManager.LayoutParams layoutParams=getDialog().getWindow().getAttributes();
layoutParams.width=displayMetrics.widthPixels;
layoutParams.gravity= Gravity.BOTTOM;
layoutParams.height=WindowManager.LayoutParams.WRAP_CONTENT;
getDialog().getWindow().setAttributes(layoutParams);
}
/**
* @see RechargePanelView#showMessage(String, String,int)
* @param msg
* @param title
*/
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void showMessage(String msg, String title, final int code) {
if (mContext==null){
return;
}
new QMUIDialog.MessageDialogBuilder(getContext())
.setTitle(title)
.setMessage(msg)
.addAction("确定",new QMUIDialogAction.ActionListener(){
@Override
public void onClick(QMUIDialog dialog, int index) {
if (code==1){
((MyWalletActivity)getActivity()).onFragmentInteraction(Uri.parse("update"));
dismiss();
}
}
})
.setCancelable(false)
.show();
}
/**
* @see RechargePanelView#aliPay(Map)
* @param maps
*/
@Override
public void aliPay(Map<String,String>maps) {
Map<String,String>map= OrderInfoUtil2_0.buildOrderParamMap(ALIPAY_APP_ID,true,maps);
String orderParam= OrderInfoUtil2_0.buildOrderParam(map);
String sign=OrderInfoUtil2_0.getSign(map,ALIPAY_RSA,true);
final String orderInfo=orderParam+"&"+sign;
mRechargePanelPersenterImp.alipay(orderInfo);
}
@Override
public void wechatPay(Map<String, String> map) {
OrderInfoUtil orderInfoUtil=OrderInfoUtil.getOrderInfoUtil();
String xml=orderInfoUtil.oderInfoFromMap(map);
orderInfoUtil.doHttpXml(xml,Constant.RechargePanelActivityHandleWechat,mHandler);
}
@Override
public String wechatPaySign(Map<String, String> map) {
OrderInfoUtil orderInfoUtil=OrderInfoUtil.getOrderInfoUtil();
return orderInfoUtil.sign(map,WECHAT_KEY);
}
/**
* @see RechargePanelView#aliPay(String, String, String)
*/
@Override
public void aliPay(String userId, String price, String type) {
Date date=new Date();
Long date1=date.getTime();
Map<String,String>map=new HashMap<>();
map.put("userNum",userId);
map.put("orderMoney",price);
map.put("chargeDate",date1+"");
map.put("chargeType",type);
mRechargePanelPersenterImp.submitOrder(map);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
onButtonPressed(Uri.parse("show"));
mListener = null;
}
private Context mContext;
/**
* @see RechargePanelView#initView(View)
*
*/
@Override
public void initView(View view) {
((MyApplication)getActivity().getApplication()).setRechargePanelFragment(this);
mContext=getDialog().getContext();
mRechargePanelPersenterImp=new RechargePanelPersenterImp(mContext,mHandler,getActivity());
mImageViewBack=view.findViewById(R.id.iv_recharge_back);
mTextViewBack=view.findViewById(R.id.tv_recharge_back);
mLinearLayoutPrice=view.findViewById(R.id.ll_input_price);
mEditTextPrice=view.findViewById(R.id.et_editor_price);
mButtonPriceOk=view.findViewById(R.id.btn_price_ok);
mButtonPriceOk.setOnClickListener(this);
mImageViewBack.setOnClickListener(this);
mTextViewBack.setOnClickListener(this);
mGridView=view.findViewById(R.id.gv_recharge);
bindDataToAdpter();
}
/**
* @see RechargePanelView#bindDataToAdpter()
*/
@Override
public void bindDataToAdpter() {
List<Map<String,Object>>list=mRechargePanelPersenterImp.getRechargePanelData();
mRechargePanelAdpter=new RechargePanelAdpter(mContext,list,this);
mGridView.setAdapter(mRechargePanelAdpter);
}
@Override
public void callBackPrice(String price) {
if (mParam1.equals(getString(R.string.alipay))){
Date date=new Date();
Map<String,String>map=new HashMap<>();
map.put(getString(R.string.subject),getString(R.string.wlwcz)+price);
map.put(getString(R.string.out_trade_no),date.getTime()+"");
map.put(getString(R.string.total_amount),price.split("元")[0]);
map.put(getString(R.string.product_code),getString(R.string.QUICK_MSECURITY_PAY));
aliPay(map);
}else if (mParam1.equals(getString(R.string.wachepay))){
((MyApplication)getActivity().getApplication()).setPrice(Integer.parseInt(price.split("元")[0])*100+"");
((MyApplication)getActivity().getApplication()).setUserId(mParam2);
iwxapi= WXAPIFactory.createWXAPI(mContext,null);
iwxapi.registerApp(WECHAT_APPID);
OrderInfoUtil orderInfoUtil=OrderInfoUtil.getOrderInfoUtil();
nonceString=orderInfoUtil.crateNonceStr();
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
String order=simpleDateFormat.format(date);
Map<String,String>map=new HashMap<>();
map.put("appid",WECHAT_APPID);
map.put("mch_id",WECHAT_MCH_ID);
map.put("nonce_str",nonceString);
map.put("body","物联网充值"+price);
map.put("out_trade_no",order);
map.put("total_fee",(Integer.parseInt(price.split("元")[0])*100)+"");
map.put("spbill_create_ip","127.0.0.1");
map.put("notify_url","http://www.weixin.qq.com/wxpay/pay.php");
map.put("trade_type","APP");
sgin=orderInfoUtil.sign(map,WECHAT_KEY);
map.put("sign",sgin);
wechatPay(map);
}
}
private String sgin;
private String nonceString;
@Override
public void callBackHideView() {
mLinearLayoutPrice.setVisibility(View.VISIBLE);
List<Map<String,Object>>list=new ArrayList<>();
mRechargePanelAdpter=new RechargePanelAdpter(mContext,list,this);
mGridView.setAdapter(mRechargePanelAdpter);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.iv_recharge_back:
dismiss();
break;
case R.id.tv_recharge_back:
dismiss();
break;
case R.id.btn_price_ok:
if (mEditTextPrice.getText()==null||mEditTextPrice.getText().toString().equals("")){
showMessage("充值金额不能为空!","提示信息",0);
return;
}
if (mParam1.equals("alipay")){
Date date=new Date();
Map<String,String>map=new HashMap<>();
map.put(getString(R.string.subject),"物联网充值"+mEditTextPrice.getText().toString()+"元");
map.put("out_trade_no",date.getTime()+"");
map.put("total_amount",mEditTextPrice.getText().toString());
map.put("product_code","QUICK_MSECURITY_PAY");
aliPay(map);
}else if (mParam1.equals("wachepay")){
((MyApplication)getActivity().getApplication()).setPrice((Integer.parseInt(mEditTextPrice.getText().toString())*100)+"");
((MyApplication)getActivity().getApplication()).setUserId(mParam2);
iwxapi= WXAPIFactory.createWXAPI(mContext,null);
iwxapi.registerApp(WECHAT_APPID);
OrderInfoUtil orderInfoUtil=OrderInfoUtil.getOrderInfoUtil();
nonceString=orderInfoUtil.crateNonceStr();
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
String order=simpleDateFormat.format(date);
Map<String,String>map=new HashMap<>();
map.put("appid",WECHAT_APPID);
map.put("mch_id",WECHAT_MCH_ID);
map.put("nonce_str",nonceString);
map.put("body","物联网充值"+mEditTextPrice.getText().toString()+"元");
map.put("out_trade_no",order);
map.put("total_fee",(Integer.parseInt(mEditTextPrice.getText().toString())*100)+"");
map.put("spbill_create_ip","127.0.0.1");
map.put("notify_url","http://www.weixin.qq.com/wxpay/pay.php");
map.put("trade_type","APP");
sgin=orderInfoUtil.sign(map,WECHAT_KEY);
map.put("sign",sgin);
wechatPay(map);
}
break;
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
* @see AddGardenFragment.OnFragmentInteractionListener
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}