private void weixinPay() {
    String packge = "Sign=WXPay";
    api = WXAPIFactory.createWXAPI(this, WeixinConstants.APP_ID, true);
    req.appId = app_id;
    req.nonceStr = nonce_str;
    req.packageValue = packge;
    req.partnerId = mch_id;
    req.prepayId = prepay_id;
    req.timeStamp = timeStamp;

    List<NameValuePair> signParams = new LinkedList<NameValuePair>();
    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("sign", sign));
    signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));
    req.sign = genAppSign(signParams);
    if (dialog.isShowing()) {
      dialog.dismiss();
    }
    api.registerApp(WeixinConstants.APP_ID);
    api.sendReq(req);
    bt_pay.setEnabled(true);
  }
Exemple #2
0
  public static void shareWechatUrl(
      Context context, boolean isTimeLine, String url, String title, String dsc, Bitmap thumb) {
    IWXAPI api = getWechatApi(context);
    api.registerApp(Constants.APP_ID);

    //        WXTextObject textObj = new WXTextObject();
    //        textObj.text = text;

    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = url;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = title;
    msg.description = dsc;
    if (thumb != null) {
      msg.thumbData = Util.bmpToByteArray(thumb, true);
    }
    //        msg.mediaObject = textObj;

    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("webpage");
    req.message = msg;
    req.scene =
        isTimeLine ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
    api.sendReq(req);
  }
  /**
   * @param type 0:分享到微信好友,1:分享到微信朋友圈
   * @param url 分享链接
   */
  private void senWechatReq(final int type, String url, String title, String des, String img) {
    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = url;
    final WXMediaMessage wechatmsg = new WXMediaMessage(webpage);
    wechatmsg.title = title;
    wechatmsg.description = des;
    Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.worklife_ic_launcher);
    wechatmsg.thumbData = bmpToByteArray(thumb, true);
    // ImageLoader.getInstance().loadImage(img,new
    // SimpleImageLoadingListener(){
    // @Override public void onLoadingComplete(String imageUri, View view,
    // Bitmap loadedImage) {
    // super.onLoadingComplete(imageUri, view, loadedImage);
    // if (loadedImage != null) {
    // wechatmsg.thumbData = bmpToByteArray(loadedImage, true);
    // SendMessageToWX.Req req = new SendMessageToWX.Req();
    // req.transaction = buildTransaction("webpage");
    // req.message = wechatmsg;
    // req.scene = type==0 ? SendMessageToWX.Req.WXSceneTimeline :
    // SendMessageToWX.Req.WXSceneSession;
    // api.sendReq(req);
    // }
    // }
    // });

    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("webpage");
    req.message = wechatmsg;
    req.scene =
        type == 0 ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
    api.sendReq(req);
  }
Exemple #4
0
  public static void shareWechat(Context context, String text) {
    IWXAPI api = getWechatApi(context);
    api.registerApp(Constants.APP_ID);
    int wxSdkVersion = api.getWXAppSupportAPI();
    if (wxSdkVersion < TIMELINE_SUPPORTED_VERSION) {
      return;
    }

    if (text == null || text.length() == 0) {
      return;
    }

    // 初始化一个WXTextObject对象
    WXTextObject textObj = new WXTextObject();
    textObj.text = text;

    // 用WXTextObject对象初始化一个WXMediaMessage对象
    WXMediaMessage msg = new WXMediaMessage();
    msg.mediaObject = textObj;
    // 发送文本类型的消息时,title字段不起作用
    // msg.title = "Will be ignored";
    msg.description = text;

    // 构造一个Req
    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("text"); // transaction字段用于唯一标识一个请求
    req.message = msg;
    req.scene = SendMessageToWX.Req.WXSceneTimeline;

    // 调用api接口发送数据到微信
    api.sendReq(req);
  }
Exemple #5
0
 public static void WXImageShare(Context context, WXMediaMessage msg) {
   api = WXAPIFactory.createWXAPI(context, APP_WX_ID);
   api.registerApp(APP_WX_ID);
   if (api.isWXAppInstalled()) {
     SendMessageToWX.Req req = new SendMessageToWX.Req();
     req.transaction = buildTransaction("img");
     req.message = msg;
     req.scene = SendMessageToWX.Req.WXSceneSession;
     api.sendReq(req);
   } else {
     Toast.makeText(context, "您没有安装微信客户端,无法使用该功能!", Toast.LENGTH_LONG).show();
   }
 }
Exemple #6
0
  private void callPay(final WxOrderVO order) {
    final IWXAPI wxApi = WXAPIFactory.createWXAPI(ctx, GlobalConsts.WX_ID);
    wxApi.registerApp(GlobalConsts.WX_ID);

    PayReq req = new PayReq();
    req.appId = order.getAppid();
    req.partnerId = order.getPartnerid();
    req.prepayId = order.getPrepayid();
    req.packageValue = order.getPkg();
    req.nonceStr = order.getNoncestr();
    req.timeStamp = order.getTimestamp();
    req.sign = order.getSign();

    wxApi.sendReq(req);
  }
  private void auth(JSONArray params, final CallbackContext callbackContext)
      throws JSONException, MalformedURLException, IOException {
    // 微信登录授权
    // params[0] -- scope
    // params[1] -- state
    // 返回值:0-成功;1-微信未安装;2-发送失败
    // final IWXAPI api = ShareWechatPlugin.api;
    if (!api.isWXAppInstalled()) {
      callbackContext.error(ERR_WECHAT_NOT_INSTALLED);
      return;
    }
    if (params == null) {
      callbackContext.error(ERR_INVALID_OPTIONS);
      return;
    }

    String scope = params.getString(0);
    String state = params.getString(1);

    final SendAuth.Req req = new SendAuth.Req();
    req.scope = scope;
    req.state = state;

    boolean success = api.sendReq(req);
    if (success) {
      // Log.i(TAG, "发送成功.");
      //			System.out.println("授权请求发送成功");
      sendNoResultPluginResult(callbackContext);
    } else {
      //			System.out.println("授权请求发送失败了");
      callbackContext.error(ERR_SENT_FAILED);
    }

    //		currentCallbackContext = callbackContext;
    //		try {
    //            boolean success = api.sendReq(req);
    //            if (!success) {
    //                //callbackContext.error(ERR_SENT_FAILED);
    //                //return;
    //            }else{
    //            	//callbackContext.success(SUCCESS);
    //            }
    //        } catch (Exception e) {
    //            //callbackContext.error(e.getMessage());
    //            return;
    //        }

  }
  private void wechatShare(int flag) {
    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = url;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = title;
    msg.description = "";
    // 这里替换一张自己工程里的图片资源
    // Bitmap thumb = BitmapFactory.decodeResource(getResources(),
    // R.drawable.action_btn);
    // msg.setThumbImage(thumb);

    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = String.valueOf(System.currentTimeMillis());
    req.message = msg;
    req.scene =
        flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
    wxApi.sendReq(req);
  }
  private void pay(JSONArray params, final CallbackContext callbackContext)
      throws JSONException, MalformedURLException, IOException {
    // 微信支付
    // 返回值:0-成功;1-微信未安装;2-发送失败
    // 参数,传入时按顺序(partnerId,prepayId,timeStamp,nonceStr,sign)

    if (!api.isWXAppInstalled()) {
      callbackContext.error(ERR_WECHAT_NOT_INSTALLED);
      return;
    }
    if (params == null) {
      callbackContext.error(ERR_INVALID_OPTIONS);
      return;
    }

    final PayReq req = new PayReq();
    req.appId = getAppId();
    req.partnerId = params.getString(0);
    req.prepayId = params.getString(1);
    req.timeStamp = params.getString(2);
    req.nonceStr = params.getString(3);
    req.sign = params.getString(4);
    req.packageValue = "Sign=WXPay";

    try {
      boolean success = api.sendReq(req);
      if (!success) {
        callbackContext.error(ERR_SENT_FAILED);
        return;
      } else {
        callbackContext.success(SUCCESS);
      }
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return;
    }

    currentCallbackContext = callbackContext;
  }
  public void registerToWX() {

    boolean r = api.registerApp(APP_ID);
    Log.e("debug", "result:" + r);
    String text = "haha";
    // 初始化一个WXTextObject对象
    WXTextObject textObj = new WXTextObject();
    textObj.text = text;

    // 用WXTextObject对象初始化一个WXMediaMessage对象
    WXMediaMessage msg = new WXMediaMessage();
    msg.mediaObject = textObj;
    // 发送文本类型的消息时,title字段不起作用
    // msg.title = "Will be ignored";
    msg.description = text;

    // 构造一个Req
    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("text"); // transaction字段用于唯一标识一个请求
    req.message = msg;
    req.scene = SendMessageToWX.Req.WXSceneTimeline; // 调用api接口发送数据到微信
    api.sendReq(req);
  }
  @Override
  public void onClick(View arg0) {
    switch (arg0.getId()) {
      case R.id.btn_back:
        finish();
        break;
      case R.id.btn_newsdalycomment_add:
        addComment();
        break;
      case R.id.btn_newsdayly_share:
        // 启动微信
        // Toast.makeText(this, String.valueOf(api.openWXApp()),
        // Toast.LENGTH_LONG)
        // .show();
        // 发送文本到朋友圈
        //
        // String text = "application test";
        // WXTextObject textObj = new WXTextObject();
        // textObj.text = text;
        //
        // WXMediaMessage msg = new WXMediaMessage(textObj);
        // msg.mediaObject = textObj;
        // msg.description = text;
        //
        // SendMessageToWX.Req req = new SendMessageToWX.Req();
        // req.transaction = String.valueOf(System.currentTimeMillis());
        // req.message = msg;
        // req.scene = SendMessageToWX.Req.WXSceneTimeline;
        //
        // api.sendReq(req);

        // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
        // R.drawable.ic_seehdu);
        // WXImageObject imgobj = new WXImageObject(bitmap);
        // WXMediaMessage msg = new WXMediaMessage();
        // msg.mediaObject = imgobj;
        // Bitmap thumbitmap = Bitmap.createScaledBitmap(bitmap, 120, 150,
        // true);
        // bitmap.recycle();
        // msg.thumbData = bmpToArray(thumbitmap, true);
        // SendMessageToWX.Req req = new SendMessageToWX.Req();
        // req.transaction = String.valueOf(System.currentTimeMillis());
        // req.message = msg;
        // req.scene = SendMessageToWX.Req.WXSceneTimeline;
        // api.sendReq(req);
        //
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = surl;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = stitle;
        msg.description = "application test";
        msg.mediaObject = webpage;
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_seehdu);
        Bitmap thumbitmap = Bitmap.createScaledBitmap(bitmap, 120, 150, true);
        bitmap.recycle();
        msg.thumbData = bmpToArray(thumbitmap, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;

        api.sendReq(req);
    }
  }
Exemple #12
0
 private void sendPayReq() {
   msgApi.registerApp(Constants.APP_ID);
   msgApi.sendReq(req);
 }
 private void sendPayReq() {
   WXPayEntryActivity.handler.sendEmptyMessage(2);
   msgApi.registerApp(APP_ID);
   msgApi.sendReq(req);
 }
 /** 分享朋友圈 */
 private void shareToFriendQuan(
     Context mcontext, String shareUrl, String title, String des, Drawable mDrawable, int type) {
   Bitmap b = null;
   ByteArrayOutputStream output = null;
   try {
     IWXAPI api = WXAPIFactory.createWXAPI(act, APP_ID);
     api.registerApp(APP_ID);
     WXMediaMessage msg = null;
     if (!api.isWXAppInstalled()) {
       Toast.makeText(
               act,
               act.getResources().getString(R.string.weixinsdk_demo_has_not_installed_weibo),
               Toast.LENGTH_SHORT)
           .show();
       return;
     }
     switch (type) {
       case Share_Text_Type:
         WXTextObject textObj = new WXTextObject();
         textObj.text = title;
         msg = new WXMediaMessage();
         msg.mediaObject = textObj;
         msg.description = title;
         break;
       case Share_Webpage_Type:
         WXWebpageObject webpage = new WXWebpageObject();
         webpage.webpageUrl = shareUrl;
         msg = new WXMediaMessage(webpage);
         msg.title = title;
         msg.description = des;
         break;
     }
     if (type == Share_Webpage_Type && mDrawable != null) {
       BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
       b = bitmapDrawable.getBitmap();
       if (b == null) {
         bitmapDrawable =
             ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher));
         b = bitmapDrawable.getBitmap();
       }
       output = new ByteArrayOutputStream();
       b = zoomImg(b, 100);
       b.compress(CompressFormat.PNG, 70, output);
       byte[] result = output.toByteArray();
       msg.thumbData = result;
     }
     SendMessageToWX.Req req = new SendMessageToWX.Req();
     if (type == Share_Text_Type) req.transaction = buildTransaction("text");
     else if (type == Share_Webpage_Type) req.transaction = buildTransaction("webpage");
     req.message = msg;
     req.scene = SendMessageToWX.Req.WXSceneTimeline;
     api.sendReq(req);
   } catch (Exception e) {
     Toast.makeText(mcontext, mcontext.getString(R.string.share_weixin), 300).show();
   } finally {
     if (b != null) {
       b = null;
     }
     if (output != null) {
       try {
         output.close();
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
  private void shareToWeixin(
      Context mcontext, String shareUrl, String title, String des, Drawable mDrawable, int type) {
    Bitmap b = null;
    try {
      IWXAPI api = WXAPIFactory.createWXAPI(act, APP_ID);
      api.registerApp(APP_ID);
      if (!api.isWXAppInstalled()) {
        Toast.makeText(
                act,
                act.getResources().getString(R.string.weixinsdk_demo_has_not_installed_weibo),
                Toast.LENGTH_SHORT)
            .show();
        return;
      }
      WXMediaMessage msg = null;
      switch (type) {
        case Share_Text_Type:
          WXTextObject textObj = new WXTextObject();
          textObj.text = title;
          msg = new WXMediaMessage();
          msg.mediaObject = textObj;
          msg.description = title;
          break;
        case Share_Webpage_Type:
          WXWebpageObject webpage = new WXWebpageObject();
          webpage.webpageUrl = shareUrl;
          msg = new WXMediaMessage(webpage);
          msg.title = title;
          msg.description = des;
          break;
      }

      if (type == Share_Webpage_Type && mDrawable != null) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
        b = bitmapDrawable.getBitmap();
        b = zoomImg(b, 100);
        if (b == null) {
          bitmapDrawable =
              ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher));
          b = bitmapDrawable.getBitmap();
        }
        msg.thumbData = bmpToByteArray(b, true);
      }
      SendMessageToWX.Req req = new SendMessageToWX.Req();
      if (type == Share_Text_Type) req.transaction = buildTransaction("text");
      else if (type == Share_Webpage_Type) {
        req.transaction = buildTransaction("webpage");
        msg.title = title;
        msg.description = des;
      }
      req.message = msg;
      req.scene = SendMessageToWX.Req.WXSceneSession;
      api.sendReq(req);
    } catch (Exception e) {
      e.printStackTrace();
      Toast.makeText(mcontext, mcontext.getString(R.string.share_weixin), 300).show();
    } finally {
      if (b != null) {
        b = null;
      }
    }
  }
  private void share(JSONArray params, final CallbackContext callbackContext)
      throws JSONException, MalformedURLException, IOException {
    // 目前只支持 WXWebpageObject 分享
    // params[0] -- 分享的目标场景(0会话,1朋友圈,2微信收藏)
    // params[1] -- 分享内容的目标 url
    // params[2] -- 标题
    // params[3] -- 描述
    // params[4] -- 图片url
    // 返回值:0-成功;1-微信未安装;2-发送失败
    // final IWXAPI api = ShareWechatPlugin.api;
    if (!api.isWXAppInstalled()) {
      callbackContext.error(ERR_WECHAT_NOT_INSTALLED);
      return;
    }
    if (params == null) {
      callbackContext.error(ERR_INVALID_OPTIONS);
      return;
    }

    int targetScene = params.getInt(0);
    String webUrl = params.getString(1);
    String title = params.getString(2);
    String description = params.getString(3);
    String imgUrl = params.getString(4);

    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = webUrl;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = title;
    msg.description = description;

    try {
      Bitmap bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream());
      Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
      bmp.recycle();
      msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return;
    }

    final SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("webpage");
    req.message = msg;
    switch (targetScene) {
      case 0:
        req.scene = SendMessageToWX.Req.WXSceneSession;
        break;
      case 1:
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        break;
      case 2:
        req.scene = SendMessageToWX.Req.WXSceneFavorite;
        break;
      default:
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
    }

    // run in background
    //        cordova.getThreadPool().execute(new Runnable() {
    //            @Override
    //            public void run() {
    //                if (api.sendReq(req)) {
    //                    Log.i(TAG, "wechat message sent successfully.");
    //                    //send success
    //                    callbackContext.success(NO_RESULT);
    //                } else {
    //                    Log.i(TAG, "wechat message sent failed.");
    //                    // send error
    //                    callbackContext.error(ERR_SENT_FAILED);
    //                }
    //            }
    //        });

    try {
      boolean success = api.sendReq(req);
      if (!success) {
        callbackContext.error(ERR_SENT_FAILED);
        return;
      } else {
        callbackContext.success(SUCCESS);
      }
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return;
    }

    currentCallbackContext = callbackContext;
  }