1.公众号上申请授权码之类等权限开通
2.代码实现
/** * 微信钱包支付(扫二维码付款) * @return array|mixed */ function order_wx_qrcode_wallet_action() { return parent::order_wx_qrcode_wallet( [ "auth_code"=>$this->data['auth_code'], "order_money"=>$this->data['order_money'], "gun_id"=>$this->data['gun_id'], "store_id"=>$this->data['store_id'] ] ); }
/** * 微信二维码付款 * @param array $_data * @return array|mixed */ protected function order_wx_qrcode_wallet($_data = array()) { $data = $this->order_wx_qrcode_wallet_valid_private($_data); if ($data['code'] == 1) { return $data; } $user = $data['user']; $openid = $data['openid']; $store = $data['store']; //跳到内部获取用信息优惠等等 $response = doCurlGetRequest(WEBURL . 'api.php', [ 'request' => 'private.gas_station.get_my_discount_oil_price_for_pos', 'platform' => 'app', 'store_id' => $_data['store_id'], 'gun_id' => $_data['gun_id'], 'money' => $_data['order_money'], 'userid' => $user['userid'] ] ); $price_money = json_decode($response, true); $coupon_id = 0; $coupon_money = 0; if (!empty($price_money)) { if ($price_money['code'] == 1) { return $price_money; } $oil_type = $this->DB('slave1')->query("SELECT GU.oil_type_id,GU.status,OT.oil_name,OT.oil_price,OT.merchant_oil_price," . " OT.country_oil_price,OT.oil_price_get" . " FROM " . TABLE_OIL_GUN . " AS GU " . " LEFT JOIN " . TABLE_OIL_TYPE . " AS OT ON GU.oil_type_id=OT.id AND OT.store_id='" . $_data['store_id'] . "'" . " WHERE GU.gun_id='" . $_data['gun_id'] . "' " . " AND GU.store_id='" . $_data['store_id'] . "'")->fetch(\PDO::FETCH_ASSOC); if ($price_money['data']['perfect_coupon_status'] == 0) { $coupon_id = $price_money['data']['perfect_coupon']['id']; $coupon_money = $price_money['data']['perfect_coupon']['coupon_money']; } $orderid = sprintf("%03d", $_data['store_id']) . sprintf("%03d", $_data['gun_id']) . date("YmdHis") . rand(1000, 9999); $this->DB('master')->insert(TABLE_ORDER_MICROPAY_WX, [ 'orderid' => $orderid, 'oil_money' => $_data['order_money'], 'money' => $_data['order_money'], 'order_time' => date("Y-m-d H:i:s"), 'gun_id' => $_data['gun_id'], 'store_id' => $_data['store_id'], 'order_status' => 1, 'discount_money' => $price_money['data']['discountMoney'], 'openid' => $openid, 'oil_type_id' => $oil_type['oil_type_id'], 'userid' => $user['userid'], 'need_payment' => $price_money['data']['need_payment'] - $coupon_money, 'country_oil_price' => $price_money['data']['country_oil_price'], 'merchant_oil_price' => $price_money['data']['merchant_oil_price'], 'yc_oil_price' => 0, 'zc_oil_price' => $price_money['data']['zc_oil_price'], 'hy_oil_price' => $price_money['data']['hy_oil_price'], 'use_oil_price' => $price_money['data']['use_oil_price'], 'oil_lit' => $price_money['data']['litre'], 'country_oil_money' => $price_money['data']['country_oil_money'], 'who_discount' => $price_money['data']['discountName'], 'user_level_id' => $price_money['data']['vip_level'], 'auth_code' => $_data['auth_code'], 'coupon_money' => $coupon_money ]); $id = $this->DB('master')->id(); if ($id) { $nonce_str = $this->getNonceStr(); $body = $store['name'] . '刷卡支付'; $Micropay = new NativeCall_pub(array("appid", "appsecret", "mchid", "sub_mch_id", "mchkey", "curltimeout", "sslsert_path", "sslkey_path")); $Micropay->setReturnParameter("appid", $store['appid']); if (!empty($store['wx_pay_method'])) { $Micropay->setReturnParameter("mch_id", $store['wxfws_mch_id']); $Micropay->setReturnParameter("sub_mch_id", $store['wxsub_mch_id']); $mchkey = $store['wxfws_mchkey']; } else { $Micropay->setReturnParameter("mch_id", $store['wxmch_id']); $mchkey = $store['wxmchkey']; } $Micropay->setReturnParameter("auth_code", $_data['auth_code']); $Micropay->setReturnParameter("nonce_str", $nonce_str); $Micropay->setReturnParameter("body", $body); $Micropay->setReturnParameter("attach", $_data['store_id']); $Micropay->setReturnParameter("out_trade_no", $orderid); $Micropay->setReturnParameter("total_fee", $price_money['data']['need_payment'] * 100); // $Micropay->setReturnParameter("total_fee", 0.01 * 100); // $Micropay->setReturnParameter("total_fee", 100000 * 100); $Micropay->setReturnParameter("spbill_create_ip", $_SERVER['REMOTE_ADDR']); $Micropay->setReturnParameter("auth_code", $_data['auth_code']); $my_array = $Micropay->GetReturnParameter(); //排序验签 ksort($my_array); $sign = strtoupper(md5(urldecode(http_build_query($my_array)) . "&key=" . $mchkey . "")) . ""; $Micropay->setReturnParameter("sign", $sign); $returnXml = $Micropay->createXml(); $return = $Micropay->xmlToArray($Micropay->postXmlCurl($returnXml, 'https://api.mch.weixin.qq.com/pay/micropay', $second = 30)); if (empty($return)) { //请求超时发出 $this->DB('master')->update(TABLE_ORDER_MICROPAY_WX, [ 'err_status' => 1, 'err_code' => 'TIMEOUT', 'err_code_des' => '请求超时' ], [ 'id' => $id ]); return array("code" => 1, "msg" => "请求网络发生超时"); } elseif ($return['return_code'] == "SUCCESS" && $return['result_code'] == "SUCCESS") { $point = $this->GetPointMethed(0, array("oil_type_id" => $oil_type['oil_type_id'], "oil_lit" => $price_money['data']['litre'], "fee" => ($return['total_fee'] / 100))); $this->AddPointLog(3, $point, $user['userid'], $orderid); //用户可能首次支付处理 $wx_id = $this->DB('slave1')->get(TABLE_USER_WXGZH, ['wx_id'], ['openid' => $openid]); if (empty($wx_id)) { $this->DB('master')->insert(TABLE_USER_WXGZH, [ 'openid' => $openid, 'nickname' => '未关注用户', 'subscribe' => 0, 'addtime' => date("Y-m-d H:i:s") ]); $wx_id = $this->DB('master')->id(); } else { $wx_id = $wx_id['wx_id']; } $this->DB('master')->insert(TABLE_ORDER_WXGZH, [ 'orderid' => $orderid, 'order_time' => date("Y-m-d H:i:s"), 'pay_time' => date("Y-m-d H:i:s", strtotime($return['time_end'])), 'oil_money' => $_data['order_money'], 'pay_money' => ($return['total_fee'] / 100), 'order_money' => $_data['order_money'], 'order_status' => 3, 'openid' => $openid, 'wx_id' => $wx_id, 'pay_openid' => $return['openid'], 'pay_type' => 1, 'err_status' => 0, 'store_id' => $_data['store_id'], 'gun_id' => $_data['gun_id'], 'oil_type_id' => $oil_type['oil_type_id'], 'coupon_id' => $coupon_id, 'coupon_money' => $coupon_money, 'discount_money' => $price_money['data']['discountMoney'], 'userid' => $user['userid'], 'transaction_id' => $return['transaction_id'], 'pay_method' => 1, 'oil_lit' => $price_money['data']['litre'], 'point' => $point, 'use_oil_price' => $price_money['data']['use_oil_price'], 'merchant_discount' => $price_money['data']['merchant_discount'], 'who_discount' => $price_money['data']['discountName'], 'merchant_oil_price' => $price_money['data']['merchant_oil_price'] ]); $id = $this->DB('master')->id(); //支付完成之后,更新订单状态 $this->DB('master')->update(TABLE_ORDER_MICROPAY_WX, [ 'err_status' => 0, 'err_code' => $return['err_code'], 'err_code_des' => addslashes($return['err_code_des']), 'weixin_payment' => ($return['total_fee'] / 100), 'order_status' => 3 ], [ 'id' => $id ]); //优惠券核销 $this->DB('master')->update(TABLE_COUPON, [ 'use_status' => 1, 'use_time' => date("Y-m-d H:i:s"), 'orderid' => $orderid ], [ 'id' => $coupon_id ]); //微信MNS进行队列各种处理 $sentClass = new Message(ACCESS_KEY_ID, ACCESS_KEY_SECRET, ALIYUN_ID, ALIYUN_URL); $_message = [ 'port' => 'weixin', 'model' => 'wx_pay_order_mns_notify', 'data' => $id . ',1,' . $return['is_subscribe'] ]; $sentClass->SendMessage('oilpaynotifymns', json_encode($_message)); //data参数传输中间的1代表是微信公众号端 return ["code" => 0, "msg" => "扣款成功"]; } elseif ($return['return_code'] == "SUCCESS" && $return['result_code'] == "FAIL") { //请求失败处理 $this->DB('master')->update(TABLE_ORDER_MICROPAY_WX, [ 'err_status' => 1, 'err_code' => $return['err_code'], 'err_code_des' => addslashes($return['err_code_des']) ], [ 'id' => $id ]); if ($return['err_code'] == "USERPAYING") { $upload_status = 0; $this->DB('master')->insert(TABLE_ADMIN_MICROPAY_RESENT_WX, [ 'id' => $id, 'addtime' => date("Y-m-d H:i:s"), 'status' => 0, 'tj_count' => 0, 'upload_status' => $upload_status ]); $sentClass = new Message(ACCESS_KEY_ID, ACCESS_KEY_SECRET, ALIYUN_ID, ALIYUN_URL); $sentClass->SendMessage('YcWXShuaKaPay', $id); // $this->SendMessage('YcWXShuaKaPay', $id); } return ["code" => 1, "msg" => addslashes($return['err_code_des'])]; } elseif ($return['return_code'] == "FAIL") { return ["code" => 1, "msg" => addslashes($return['return_msg'])]; } else { $this->AddLogAlert('扫码支付未知错误', addslashes(json_encode($return))); return ["code" => 1, "msg" => "未知错误"]; } } } }
/** * 扫描二维码先执行 * @param array $_data * @return array */ private function order_wx_qrcode_wallet_valid_private($_data = array()) { if (!regExp::checkNULL($this->data['auth_code'])) { return ["code" => 1, "msg" => "授权码未填写"]; } if (!regExp::checkNULL($this->data['order_money'])) { return ["code" => 1, "msg" => "订单金额不正确"]; } if (!regExp::checkNULL($this->data['gun_id'])) { return ["code" => 1, "msg" => "枪号不正确"]; } if (!regExp::checkNULL($this->data['store_id'])) { return ["code" => 1, "msg" => "门店不正确"]; } //判断是否已经生成过账单 $store = $this->DB('slave1')->query("SELECT S.name,S.wx_pay_method,S.wxsub_mch_id,S.wxsub_appid," . " M.wxfws_mch_id,M.wxfws_mchkey,M.appid,M.appsecret,M.wxmch_id,M.wxmchkey FROM " . TABLE_STORE . " AS S" . " LEFT JOIN " . TABLE_MERCHANT_WX . " AS M ON M.id=1" . " WHERE S.id='" . $_data['store_id'] . "'")->fetch(\PDO::FETCH_ASSOC); if (empty($store)) { return ["code" => 1, "msg" => "配置信息错误"]; } //检查重复 $check_cf = $this->DB('slave1')->get(TABLE_ORDER_MICROPAY_WX, '*', ['auth_code' => $_data['auth_code']]); if (!empty($check_cf)) { return ["code" => 1, "msg" => "该码已经扫描过,请勿多次扫码"]; } //授权码查询用户身份 $nonce_str = $this->getNonceStr(); $Micropay = new NativeCall_pub(array("appid", "appsecret", "mchid", "sub_mch_id", "mchkey", "curltimeout", "sslsert_path", "sslkey_path")); $Micropay->setReturnParameter("appid", $store['appid']); if (!empty($store['wx_pay_method'])) { $Micropay->setReturnParameter("mch_id", $store['wxfws_mch_id']); $Micropay->setReturnParameter("sub_mch_id", $store['wxsub_mch_id']); $mchkey = $store['wxfws_mchkey']; } else { $Micropay->setReturnParameter("mch_id", $store['wxmch_id']); $mchkey = $store['wxmchkey']; } $Micropay->setReturnParameter("auth_code", $_data['auth_code']); $Micropay->setReturnParameter("nonce_str", $nonce_str); $my_array = $Micropay->GetReturnParameter(); ksort($my_array); $sign = strtoupper(md5(urldecode(http_build_query($my_array)) . "&key=" . $mchkey . "")) . ""; $Micropay->setReturnParameter("sign", $sign); $returnXml = $Micropay->createXml(); $return = $Micropay->xmlToArray($Micropay->postXmlCurl($returnXml, 'https://api.mch.weixin.qq.com/tools/authcodetoopenid', $second = 30)); if (!empty($return['openid'])) { $openid = $return['openid']; } else { return ["code" => 1, "msg" => "身份获取失败"]; } //查询出支付人的用户信息 $user = $this->DB('slave1')->get(TABLE_USER_WXGZH, ['userid'], ['openid' => $openid]); if (empty($user)) { $res = $this->DB('master')->insert(TABLE_USER_WXGZH, [ 'openid' => $openid ]); if ($res->rowCount()) { $user['userid'] = 0; } else { return ['code' => 1, 'msg' => '添加未关注用户数据失败']; } } return array('user' => $user, 'openid' => $openid, 'store' => $store); }