网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android中如何集成微信支付

Android中如何集成微信支付,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的凤泉网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

注释: 要继承微信支付和支付宝功能,必须要有以下配置信息,微信支付和支付宝支付申请的秘钥等提供给开发者(当然也可以自己去申请),将配置信息放在静态类中,已被统一使用(安全考虑,微信支付宝推荐这些数据放在服务其中)

public class ParameterConfig {

    public static final String  GANHOST = "http://101.226.197.11"; //服务器地址ip(根据自己替换)

    /**
     * 微信
     */
       //appid
    public static final String WX_APP_ID = "";// 自己填写自己项目的
    // 商户号
    public static final String WX_MCH_ID = "";// 自己填写自己项目的
    // API密钥,在商户平台设置
    public static final String WX_API_KEY = "";// 自己填写自己项目的
    //服务器回调接口
    public static final String WX_notifyUrl = GANHOST+"/service/orderComplete";// 用于微信支付成功的回调(按自己需求填写)

    /**
     * 支付宝
     */
    // 商户PID
    public static final String PARTNER = "";//自己填写自己项目的
    // 商户收款账号
    public static final String SELLER = "";//自己填写自己项目的
    // 商户私钥,pkcs8格式
    public static final String RSA_PRIVATE = "";//自己填写自己项目的

    public static final String aliPay_notifyURL = GANHOST+"/service/alipay/orderComplete";//支付宝支付成功的回调 
}

1.微信支付集成的前提条件 (1)首先要导入微信.jar包,从开发平台上可以下载到,然后放置在libs文件夹上。 (2)需要配置mainfest



    

b.activity配置,这里com.gan.mypay改成自己的包名(如果自己包名与src下的package 名不一样,这里要的是在manifest中配置的名称,同样需要在src建立以自己包 名为路经的package,一定确保有这个activity)这个activity是微信支付结果要回调的activty。

android:name="com.gan.mypay.wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"/>

2.代码继承 (1)首先要有一个商品页MainActivity,用来收集商品信息,这里需要后台交互生成订单,我们这里做的一个假的订单。 MainActivity.java(这里用了xutils的注入)

@ContentView(R.layout.activity_main)
public class MainActivity extends Activity {

    private Goods goods;
    private String username;
    private String mobile;
    private String adress;
    private int count;

    @ViewInject(R.id.product_ordsubmit_username)
    private TextView usernameTV;

    @ViewInject(R.id.product_ordsubmit_phone)
    private TextView phoneTV;

    @ViewInject(R.id.product_ordsubmit_adress)
    private TextView adressTV;

    @ViewInject(R.id.product_ordsubmit_desc)
    private TextView descTV;

    @ViewInject(R.id.product_ordsubmit_price)
    private TextView priceTV;

    @ViewInject(R.id.product_ordsubmit_intg)
    private TextView intgTV;

    @ViewInject(R.id.product_ordsubmit_count1)
    private TextView countTV1;

    @ViewInject(R.id.product_ordsubmit_count)
    private TextView countTV;

    @ViewInject(R.id.product_ordsubmit_intgtotal1)
    private TextView intgtotal1TV;

    @ViewInject(R.id.product_ordsubmit_intgtotal2)
    private TextView intgtotal2TV;

    @ViewInject(R.id.product_ordsubmit_pricetotal1)
    private TextView pricetotal1TV;

    @ViewInject(R.id.product_ordsubmit_pricetotal2)
    private TextView pricetotal2TV;

    @ViewInject(R.id.product_ordsubmit_counttotal)
    private TextView counttotalTV;

    @ViewInject(R.id.product_ordsubmit_ok)
    private Button okBtn;

    @ViewInject(R.id.product_ordsubmit_say_et)
    private TextView sayEt;

    @ViewInject(R.id.product_ordsubmit_img)
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        goods = new Goods();
        goods.costprice=100;
        goods.productid=692356222;
        goods.producttypeid=11;
        goods.productname="测试商品";
        goods.discountprice=0.01;
        goods.productdescription="商品描述";
        goods.companydesc="测试商户简单描述";
        goods.comanyadress="商户地址未知";
        goods.companyname="测试商户";
        goods.score=1;
        goods.status=1;
        goods.stock=300;
        count=1;
        initData();
        initView();
    }

    private void initData() {

            username ="客户名称";
            mobile = "13800380038";
            adress="客户地址";  
    }

    private void initView() {   
            usernameTV.setText("收货人:"+username);
            phoneTV.setText(mobile+"");
            adressTV.setText(adress);
            descTV.setText(goods.productdescription);
            priceTV.setText("¥"+goods.discountprice);
            intgTV.setText("积分:"+goods.score);
            countTV1.setText("X"+count);
            countTV.setText(count+"");
            intgtotal1TV.setText("共得到"+count*goods.score+"积分");
            intgtotal2TV.setText("积分:"+count*goods.score);
            counttotalTV.setText("共"+count+"件");
            pricetotal1TV.setText("¥"+Arith.mul(goods.discountprice, count));
            pricetotal2TV.setText("¥"+Arith.mul(goods.discountprice, count));
            //ImageLoader.getInstance().displayImage(goods.pic1, img);
    }

    /**
     * 增加数量
     * @param v
     */
    @OnClick(R.id.product_ordsubmit_count_add)
    public void add(View v) {
        count++;
        countTV1.setText("X"+count);
        countTV.setText(count+"");
        intgtotal1TV.setText("共得到"+count*goods.score+"积分");
        intgtotal2TV.setText("积分:"+count*goods.score);
        counttotalTV.setText("共"+count+"件");
        pricetotal1TV.setText("¥"+Arith.mul(goods.discountprice, count));
        pricetotal2TV.setText("¥"+Arith.mul(goods.discountprice, count));
    }

    /**
     * 减少数量
     * @param v
     */
    @OnClick(R.id.product_ordsubmit_count_sub)
    public void sub(View v) {
        if (count>1) {
            count--;
            countTV1.setText("X"+count);
            countTV.setText(count+"");
            intgtotal1TV.setText("共得到"+count*goods.score+"积分");
            intgtotal2TV.setText("积分:"+count*goods.score);
            counttotalTV.setText("共"+count+"件");
            pricetotal1TV.setText("¥"+Arith.mul(goods.discountprice, count));
            pricetotal2TV.setText("¥"+Arith.mul(goods.discountprice, count));
        }
    }

    /**
     * 提交订单
     * @param v
     */
    @OnClick(R.id.product_ordsubmit_ok)
    public void submit(View v) {

        final OrderInfo orderInfo=new OrderInfo();
        orderInfo.userid=13752;
        orderInfo.areacode=23;
        orderInfo.buildno="10";
        orderInfo.roomno="1001";
        orderInfo.producttypeid=goods.producttypeid;
        orderInfo.productid=goods.productid;
        orderInfo.amount=goods.discountprice;//单价
        orderInfo.account=count;//数量
        orderInfo.totalamount=Arith.mul(goods.discountprice, count);
        //double offsetamount;//抵扣金额
        orderInfo.score=count*goods.score;
        //int assessitem;//评价项
        //int assesslevel;//评价级别
        //String assesscontent;//评价内容
        //long payid=;//支付编号
        orderInfo.status=2;//支付状态待付款
        orderInfo.type=11;//日用品
        orderInfo.usermemo =sayEt.getText().toString();//业主备注
        orderInfo.address =adress;
        orderInfo.productname =goods.productname;//
        orderInfo.desccontext =goods.productdescription;//
        orderInfo.outtradeno=System.currentTimeMillis()+""+orderInfo.userid;
        orderInfo.merchantid=goods.companyid;
        submitorder(orderInfo);
    }
    /**
     * 订单提交成功,进入付款界面
     * @param orderInfo 
     * @return
     */
    private void submitorder(OrderInfo orderInfo) {
        Intent intent=new Intent(this, SelectPayTypeActivity.class);
        intent.putExtra("data", orderInfo);
        startActivity(intent);
    }
}

(2)在mainactivty中点击确认按钮调用支付方式选择页面SelectPayTypeActivity,用来发起支付选择。 (3)根据支付方式调用对应工具类微信(WXpayUtil)

public class WXpayUtil {
    private IWXAPI api;
    private OrderInfo order;
    private Context context;
    private PayReq req;
    private Map resultunifiedorder;
    private static final String TAG = "ewuye.online.SelectPayTypeActivity";

    public WXpayUtil(Context mcontext,OrderInfo order){
        //初始化微信支付
        this.order=order;
        this.context=mcontext;
        if (TextUtils.isEmpty(ParameterConfig.WX_APP_ID) || TextUtils.isEmpty(ParameterConfig.WX_MCH_ID) || TextUtils.isEmpty(ParameterConfig.WX_API_KEY)) {
            new AlertDialog.Builder(context).setTitle("警告").setMessage("需要配置WX_APP_ID | WX_MCH_ID| WX_API_KEY\n请到ParameterConfig.java里配置")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialoginterface, int i) {
                            //
                            ((Activity)context).finish();
                        }
                    }).show();
            return;
        }

        api = WXAPIFactory.createWXAPI(context, null);
        req = new PayReq();
        //生成prepay_id
        GetPrepayIdTask getPrepayId = new GetPrepayIdTask();
        getPrepayId.execute();
    }

    /**
     * 用于获取
     * @author 95
     *
     */
    private class GetPrepayIdTask extends AsyncTask> {

        private ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(context, "提示", "正在获取预支付订单...");
        }

        @Override
        protected void onPostExecute(Map result) {
            if (dialog != null) {
                dialog.dismiss();
            }
            resultunifiedorder=result;
            genPayReq();

        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected Map  doInBackground(Void... params) {

            String url = String.format("https://api.mch.weixin.qq.com/pay/unifiedorder");
            String entity = genProductArgs();

            Log.e("orion",entity);

            byte[] buf = httpPost(url, entity);

            String content = new String(buf);
            Log.e("orion", content);
            Map xml=decodeXml(content);

            return xml;
        }
    }

    private void genPayReq() {

        req.appId = ParameterConfig.WX_APP_ID;
        req.partnerId = ParameterConfig.WX_MCH_ID;
        req.prepayId = resultunifiedorder.get("prepay_id");
        req.packageValue = "prepay_id="+resultunifiedorder.get("prepay_id");
        req.nonceStr = genNonceStr();
        req.timeStamp = String.valueOf(genTimeStamp());


        List signParams = new LinkedList();
        signParams.add(new BasicNameValuePair("appid", req.appId));
        signParams.add(new BasicNameValuePair("noncestr", req.nonceStr));
        signParams.add(new BasicNameValuePair("package", req.packageValue));
        signParams.add(new BasicNameValuePair("partnerid", req.partnerId));
        signParams.add(new BasicNameValuePair("prepayid", req.prepayId));
        signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));

        req.sign = genAppSign(signParams);
        Log.e("orion", signParams.toString());
        sendPayReq();
    }
    private void sendPayReq() {
        api.registerApp(ParameterConfig.WX_APP_ID);
        api.sendReq(req);

    }

    private String genProductArgs() {
        StringBuffer xml = new StringBuffer();

        try {
            String  nonceStr = genNonceStr();
            xml.append("");
           List packageParams = new LinkedList();
            packageParams.add(new BasicNameValuePair("appid", ParameterConfig.WX_APP_ID));
            packageParams.add(new BasicNameValuePair("body", order.productname));
            packageParams.add(new BasicNameValuePair("mch_id", ParameterConfig.WX_MCH_ID));
            packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));
            packageParams.add(new BasicNameValuePair("notify_url", ParameterConfig.WX_notifyUrl));
            packageParams.add(new BasicNameValuePair("out_trade_no",genOutTradNo()));
            packageParams.add(new BasicNameValuePair("spbill_create_ip","127.0.0.1"));
            packageParams.add(new BasicNameValuePair("total_fee", (int)(order.totalamount*100)+""));
            packageParams.add(new BasicNameValuePair("trade_type", "APP"));

            String sign = genPackageSign(packageParams);
            packageParams.add(new BasicNameValuePair("sign", sign));

           String xmlstring =toXml(packageParams);
           return new String(xmlstring.toString().getBytes(), "ISO8859-1");
            //return xmlstring;

        } catch (Exception e) {
            Log.e(TAG, "genProductArgs fail, ex = " + e.getMessage());
            return null;
        }
    }

    private String genAppSign(List params) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < params.size(); i++) {
            sb.append(params.get(i).getName());
            sb.append('=');
            sb.append(params.get(i).getValue());
            sb.append('&');
        }
        sb.append("key=");
        sb.append(ParameterConfig.WX_API_KEY);

        String appSign = getMessageDigest(sb.toString().getBytes());
        Log.e("orion",appSign);
        return appSign;
    }

    private  HttpClient getNewHttpClient() { 
           try { 
               KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
               trustStore.load(null, null); 

               SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); 
               sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

               HttpParams params = new BasicHttpParams(); 
               HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
               HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

               SchemeRegistry registry = new SchemeRegistry(); 
               registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
               registry.register(new Scheme("https", sf, 443)); 

               ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

               return new DefaultHttpClient(ccm, params); 
           } catch (Exception e) { 
               return new DefaultHttpClient(); 
           } 
        }
    private class SSLSocketFactoryEx extends SSLSocketFactory {      

        SSLContext sslContext = SSLContext.getInstance("TLS");      

        public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {      
            super(truststore);      

            TrustManager tm = new X509TrustManager() {      

                public X509Certificate[] getAcceptedIssuers() {      
                    return null;      
                }      

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
                }  
            };      

            sslContext.init(null, new TrustManager[] { tm }, null);      
        }      

        @Override
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
            return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
        }

        @Override
        public Socket createSocket() throws IOException {
            return sslContext.getSocketFactory().createSocket();
        } 
    }  
    public  byte[] httpPost(String url, String entity) {
        if (url == null || url.length() == 0) {
            Log.e(TAG, "httpPost, url is null");
            return null;
        }

        HttpClient httpClient = getNewHttpClient();
        HttpPost httpPost = new HttpPost(url);

        try {
            httpPost.setEntity(new StringEntity(entity));
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");

            HttpResponse resp = httpClient.execute(httpPost);
            if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                Log.e(TAG, "httpGet fail, status code = " + resp.getStatusLine().getStatusCode());
                return null;
            }

            return EntityUtils.toByteArray(resp.getEntity());
        } catch (Exception e) {
            Log.e(TAG, "httpPost exception, e = " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    private String genOutTradNo() {
        Random random = new Random();
        return getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
    }

    public Map decodeXml(String content) {

        try {
            Map xml = new HashMap();
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(new StringReader(content));
            int event = parser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {

                String nodeName=parser.getName();
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:

                        break;
                    case XmlPullParser.START_TAG:

                        if("xml".equals(nodeName)==false){
                            //实例化student对象
                            xml.put(nodeName,parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                }
                event = parser.next();
            }

            return xml;
        } catch (Exception e) {
            Log.e("orion",e.toString());
        }
        return null;

    }

    private String genNonceStr() {
        Random random = new Random();
        return getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
    }

    private long genTimeStamp() {
        return System.currentTimeMillis() / 1000;
    }

    public  String getMessageDigest(byte[] buffer) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(buffer);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     生成签名
     */

    private String genPackageSign(List params) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < params.size(); i++) {
            sb.append(params.get(i).getName());
            sb.append('=');
            sb.append(params.get(i).getValue());
            sb.append('&');
        }
        sb.append("key=");
        sb.append(ParameterConfig.WX_API_KEY);


        String packageSign = getMessageDigest(sb.toString().getBytes()).toUpperCase();
        Log.e("orion",packageSign);
        return packageSign;
    }

    private String toXml(List params) {
        StringBuilder sb = new StringBuilder();
        sb.append("");
        for (int i = 0; i < params.size(); i++) {
            sb.append("<"+params.get(i).getName()+">");


            sb.append(params.get(i).getValue());
            sb.append("");
        }
        sb.append("");

        Log.e("orion",sb.toString());
        return sb.toString();
    }
}

微信回调的Activity

package com.gan.mypay.wxapi;
import com.gan.mypay.ParameterConfig;
import com.gan.mypay.R;
import com.gan.mypay.SelectPayTypeActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;
   // private TextView reulttv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wx_pay_result);
        api = WXAPIFactory.createWXAPI(this, ParameterConfig.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            //builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
            builder.show();
            Intent intent;
            int code = resp.errCode;
            switch (code) {
            case 0:
                Toast.makeText(this, "支付成功",0).show();
                intent=new Intent(this,SelectPayTypeActivity.class);
                intent.putExtra("result", 0);
                startActivity(intent);
                finish();
                break;
            case -1:
                Toast.makeText(this, "支付失败",0).show();
                intent=new Intent(this,SelectPayTypeActivity.class);
                intent.putExtra("result", -1);
                startActivity(intent);
                finish();
                break;
            case -2:
                Toast.makeText(this, "支付取消",0).show();
                intent=new Intent(this,SelectPayTypeActivity.class);
                intent.putExtra("result", -2);
                startActivity(intent);
                finish();
                break;
            default:
                break;
            }
        }
    }
}

关于Android中如何集成微信支付问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享文章:Android中如何集成微信支付
URL标题:http://bjjierui.cn/article/jdcipg.html

其他资讯