package com.android.chmo.wxapi;

import android.content.Context;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by Administrator on 2018/8/10.
 */

public class WeChatManager {
    private final String APP_ID = "wx2d60e82cb3df6926";
    private static  WeChatManager wcManager;

    private IWXAPI api = null;


    private WeChatManager(){

    }

    public static WeChatManager getInstance() {
        if (null == wcManager) {
            wcManager = new WeChatManager();
        }
        return wcManager;
    }

    public void init(Context context) {
        api = WXAPIFactory.createWXAPI(context, APP_ID, true);
    }

    public IWXAPI getApi() {
        return  api;
    }

    public void wxPay(String appid, String partnerid, String prepayid,String noncestr, String timestamp, String packageValue, String sign,String extData) {
        PayReq req = new PayReq();
        req.appId			= appid;
        req.partnerId		= partnerid;
        req.prepayId		= prepayid;
        req.nonceStr		= noncestr;
        req.timeStamp		= timestamp;
        req.packageValue	= packageValue;
        req.sign			= sign;
        req.extData			= extData;

        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.registerApp(APP_ID);
        api.sendReq(req);
    }
}
