小程序支付
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | <?php /** * Created by PhpStorm. * User: zb * Date: 17-10-9 * Time: 下午7:15 */ class PaysAction extends CommonAction { public function _initialize(){ parent::_initialize(); } public function pay(){ $openid = trim($_POST[ 'openid' ]); $total_price = ( float )$_POST[ 'total_price' ] * 100; //~~~~ $coupon_id = ( int )$_POST[ 'coupon_id' ]; $coupon_price = ( float )$_POST[ 'coupon_price' ] * 100; $order_remark = trim($_POST[ 'order_remark' ]); $old_order_num = trim($_POST[ 'order_num' ]); //有order_num,再来一单购买 if ($old_order_num == 'undefined' ){ $old_order_num = '' ; } $product_id = ( int )$_POST[ 'product_id' ]; //有product_id,立即购买~~~~NaN,undefined在int转化后都为0; if (!$openid){ $ this ->json->setErr(10000, '缺少参数' ); $ this ->json->Send(); } $user = M( 'user' ); $tel_flag = $user-> where (array( 'openid' =>$openid))->find(); if (!$tel_flag || !$tel_flag[ 'id' ]){ $ this ->json->setErr(10001, '未绑定手机号' ); $ this ->json->Send(); } $uid = $tel_flag[ 'id' ]; if ($total_price <= 0){ $ this ->json->setErr(10002, '支付金额不可为0或负数' ); $ this ->json->Send(); } $order_info = $ this ->makeorder($old_order_num,$product_id,$openid,$uid,$total_price,$coupon_id,$coupon_price,$order_remark); $order_num = $order_info[ 'order_num' ]; $products_name = $order_info[ 'products_name' ]; $unifiedorder = $ this ->unifiedorder($openid,$order_num,$total_price,$products_name); $data = [ 'appId' => C( 'APPID' ), 'timeStamp' => time(), 'nonceStr' => $ this ->createNonceStr(), 'package' => 'prepay_id=' .$unifiedorder[ 'prepay_id' ], 'signType' => 'MD5' ]; $sign = $ this ->MakeSign($data); $data[ 'sign' ] = $sign; $ this ->json->setAttr( 'data' ,$data); $ this ->json->Send(); } /*** 生成订单 优惠券处理 * @param $openid * @param $uid * @param $total_price * @param $coupon_id user_coupon中的id字段,而非coupon_id字段 * @param $coupon_price 用户实际获取优惠额度,而非一定是表格中的price字段; * @param $order_remark 用户提交订单的备注信息; * @param $old_order_num 如有此参数,再来一单购买 * @param $product_id 立即购买方式 * @param order表格中的status字段 1未支付;2已支付;3已申请退款;4已退款;5已完成 * @return string */ private function makeorder($old_order_num= '' ,$product_id=0,$openid,$uid,$total_price,$coupon_id,$coupon_price,$order_remark){ $order = M( 'order' ); $now = time(); if ($old_order_num && !$product_id){ $order_num = 'os' .$uid.substr($now,3).rand(1000,9999); //生成12位以上订单号~~~~如果开头有os,表示从order_shoppingcar转变而来 $shoppingcar = M( 'order_shoppingcar' ); //~~~~ }elseif($product_id && !$old_order_num){ $order_num = 'qs' .$uid.substr($now,3).rand(1000,9999); //生成12位以上订单号~~~~如果开头有qs,表示从quickbuy_shoppingcar转变而来 $shoppingcar = M( 'quickbuy_shoppingcar' ); //~~~~ } else { $order_num = $uid.substr($now,3).rand(1000,9999); //生成12位以上订单号 $shoppingcar = M( 'shoppingcar' ); //~~~~ } $order_add_data = [ 'order_num' => $order_num, 'uid' => $uid, 'coupon_id' => $coupon_id, 'coupon_price' => $coupon_price, 'total_price' => $total_price, //订单价格 'status' => 1, 'addtime' => $now, //订单生成时间 'remark' => $order_remark ]; $order_add_flag = $order->add($order_add_data); // $sql = $order->getLastSql(); // $this->json->setAttr('sql',$sql); // $this->json->Send(); if (!$order_add_flag){ $ this ->json->setErr(10003, '生成订单失败' ); $ this ->json->Send(); } $return_data[ 'order_num' ] = $order_num; $map = array( 'openid' => $openid, 'pnum' => array( 'gt' ,0) ); $shoppingcar_flag = $shoppingcar-> where ($map)->field( 'pid,pnum' )->order( 'updatetime desc' )-> select (); $shoppingcar_count = count($shoppingcar_flag); $product = M( 'product' ); $order_product = M( 'order_product' ); $products_name = '' ; for ($i=0;$i<$shoppingcar_count;$i++){ $product_flag = $product-> where (array( 'id' =>$shoppingcar_flag[$i][ 'pid' ]))->find(); //如果该产品已下架或删除,购物车数据中该产品要剔除 if ($product_flag[ 'status' ] == '0' || ($product_flag[ 'is_del' ] == '1' )){ unset($shoppingcar_flag[$i]); continue ; } if (!$products_name){ $products_name .= $product_flag[ 'title' ]; } else { $products_name .= '-' .$product_flag[ 'title' ]; } $product_add_data = [ 'order_id' => $order_add_flag, 'uid' => $uid, 'pid' => $shoppingcar_flag[$i][ 'pid' ], 'pnum' => $shoppingcar_flag[$i][ 'pnum' ], 'p_name' => $product_flag[ 'title' ], 'p_price' => $product_flag[ 'price' ], 'addtime' => $now ]; $product_add_flag = $order_product->add($product_add_data); if (!$product_add_flag){ $ this ->json->setErr(10004, '订单产品数据添加失败' ); $ this ->json->Send(); } } $return_data[ 'products_name' ] = $products_name; return $return_data; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789" ; $str = "" ; for ( $i = 0; $i < $length; $i++ ) { $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } public function unifiedorder($openid,$order_num,$total_fee,$products_name){ $trade_no = $order_num; $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder' ; $data = [ 'appid' => C( 'APPID' ), 'mch_id' => C( 'MCHID' ), 'nonce_str' => $ this ->createNonceStr(), 'sign_type' => 'MD5' , 'body' => $products_name, //商品名称组合 'attach' => '****-附加数据' , //'detail' => '*****线上销售', 'out_trade_no' => $trade_no, //订单号 'fee_type' => 'CNY' , 'total_fee' => $total_fee, //$total_fee, for test~~~ 'spbill_create_ip' => $_SERVER[ 'REMOTE_ADDR' ], // 'time_start' => $this->getMillisecond(), 'goods_tag' => '******-商品标记' , 'notify_url' => 'https://**************/api.php/Pays/order_notice' , 'trade_type' => 'JSAPI' , 'openid' => $openid ]; $sign = $ this ->MakeSign($data); $data[ 'sign' ] = $sign; $xml = $ this ->ToXml($data); vendor( 'Func.Http' ); $result = $ this ->FromXml(Http::postXmlCurl($url,$xml)); return $result; } public function FromXml($xml) { if (!$xml){ throw new WxPayException( "xml数据异常!" ); } //将XML转为array //禁止引用外部xml实体 libxml_disable_entity_loader( true ); $ this ->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement' , LIBXML_NOCDATA)), true ); return $ this ->values; } private function getMillisecond() { //获取毫秒的时间戳 $time = explode ( " " , microtime () ); $time = $time[1] . ($time[0] * 1000); $time2 = explode( "." , $time ); $time = $time2[0]; return $time; } public function getSign(){ $appid = C( 'APPID' ); $nocestr = $ this ->createNonceStr(); $timeStamp = time(); } public function ToXml($array) { if (!is_array($array) || count($array) <= 0) { return ; } $xml = '<xml version="1.0">' ; foreach ($array as $key=>$val) { if (is_numeric($val)){ $xml.= "<" .$key. ">" .$val. "</" .$key. ">" ; } else { $xml.= "<" .$key. "><![CDATA[" .$val. "]]></" .$key. ">" ; } } $xml.= "</xml>" ; return $xml; } private function MakeSign($data) { //签名步骤一:按字典序排序参数 ksort($data); $ string = $ this ->ToUrlParams($data); //签名步骤二:在string后加入KEY $ string = $ string . "&key=" .C( 'WEIXIN_PAY_KEY' ); //签名步骤三:MD5加密 $ string = md5($ string ); //签名步骤四:所有字符转为大写 $result = strtoupper($ string ); return $result; } private function ToUrlParams($array) { $buff = "" ; foreach ($array as $k => $v) { if ($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&" ; } } $buff = trim($buff, "&" ); return $buff; } //微信支付回调 public function order_notice(){ $xml = $GLOBALS[ 'HTTP_RAW_POST_DATA' ]; // 这句file_put_contents是用来查看服务器返回的XML数据 测试完可以删除了~~for test // vendor('Func.Func'); // $hostname = 'http://'.Func::getHostName(); // $path = $hostname.'/static/log2.txt'; // file_put_contents($path,$xml); //将服务器返回的XML数据转化为数组 //$data = self::xml2array($xml); $data = $ this ->FromXml($xml); // 保存微信服务器返回的签名sign $data_sign = $data[ 'sign' ]; // sign不参与签名算法 unset($data[ 'sign' ]); //$sign = self::makeSign($data); $sign = $ this ->makeSign($data); // 判断签名是否正确 判断支付状态 if ( ($sign===$data_sign) && ($data[ 'return_code' ]== 'SUCCESS' ) && ($data[ 'result_code' ]== 'SUCCESS' ) ) { $result = $data; //获取服务器返回的数据 $order_num = $data[ 'out_trade_no' ]; //订单单号 $openid = $data[ 'openid' ]; //付款人openID $total_fee = $data[ 'total_fee' ]; //付款金额 $transaction_id = $data[ 'transaction_id' ]; //微信支付流水号 $user = M( 'user' ); $user_flag = $user-> where (array( 'openid' =>$openid))->field( 'id' )->find(); $uid = $user_flag[ 'id' ]; $save_data = array( 'total_payed_price' => $total_fee, //实际到帐金额 'transaction_id' => $transaction_id, 'paytime' => time(), 'status' => 2 //1未支付;2已支付;3已申请退款;4已退款;5已完成 ); $order = M( 'order' ); $order_save_flag = $order-> where (array( 'order_num' =>$order_num, 'uid' =>$uid))->save($save_data); //优惠券信息处理~~~~~发起订单还未必就支付~~~~~支付成功才处理 $order_coupon_flag = $order-> where (array( 'order_num' =>$order_num, 'uid' =>$uid))->field( 'coupon_id,id' )->find(); $coupon_id = $order_coupon_flag[ 'coupon_id' ]; if ($coupon_id){ $user_coupon = M( 'user_coupon' ); $coupon_save_data = array( 'is_use' => 1, 'oid' => $order_coupon_flag[ 'id' ], //~~~~order订单的id 'update_time' => time() ); $user_coupon_save_flag = $user_coupon-> where (array( 'id' =>$coupon_id))->save($coupon_save_data); $user_coupon_find_flag = $user_coupon-> where (array( 'id' =>$coupon_id))->field( 'coupon_id' )->find(); if ($user_coupon_save_flag){ $coupon = M( 'coupon' ); $coupon-> where (array( 'id' =>$user_coupon_find_flag[ 'coupon_id' ]))->setInc( 'used_number' , '+1' ); } } //购物车信息清理 // 1.订单号数字开头,立即购买方式,清空shoppingcar; // 2.订单号开头有sn,再来一单购买方式,清空order_shoppingcar(进入我的订单会清理),一定不要清理了shoppingcar // 2.订单号开头有qn,立即购买方式,清空quickbuy_shoppingcar(立即购买触发时间前会清理),一定不要清理了shoppingcar if (preg_match( '/^(os)/' ,$order_num)){ $shoppingcar = M( 'order_shoppingcar' ); }elseif(preg_match( '/^(qs)/' ,$order_num)){ $shoppingcar = M( 'quickbuy_shoppingcar' ); } else { $shoppingcar = M( 'shoppingcar' ); } $clear_data[ 'pnum' ] = 0; $shoppingcar-> where (array( 'openid' =>$openid))->save($clear_data); } else { $result = false ; } // 返回状态给微信服务器 if ($result) { $str= '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>' ; } else { $str= '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>' ; } echo $str; return $result; } }<br><br><br><br><br>go_to_pay: function (e) {<br> var that = this ;<br> var coupon_id = parseInt(e.currentTarget.dataset.coupon_id); //user_coupon的id<br> var coupon_price = e.currentTarget.dataset.coupon_price;//实际优惠价格<br> var total_price = e.currentTarget.dataset.total_price;<br> var products_name = e.currentTarget.dataset.products_name;<br> var order_num = e.currentTarget.dataset.order_num;//可以'',undefined<br> var product_id = parseInt(e.currentTarget.dataset.product_id);//undefined转化int->NaN<br> var input_order_remark = that.data.input_order_remark;<br><br> wx.request({<br> url: 'https://**************.net/api.php/Pays/pay',<br> header: { 'content-type': 'application/x-www-form-urlencoded' },<br> data: {<br> openid: app.globalData.open_id,<br> total_price: total_price,<br> order_num: order_num,<br> product_id: product_id,<br> coupon_id: coupon_id,<br> coupon_price: coupon_price,<br> order_remark: input_order_remark,<br> },<br> method: 'POST', //注意header<br> success: function (res) {<br> if (res.errno) {<br> wx.showToast(res.errdesc);<br> return;<br> }<br> var datas = res.data;<br> //console.log(typeof datas.data.timeStamp);<br> wx.requestPayment({<br> 'timeStamp': datas.data.timeStamp.toString(),<br> 'nonceStr': datas.data.nonceStr,<br> 'package': datas.data.package,<br> 'signType': 'MD5',<br> 'paySign': datas.data.sign,<br> 'success': function (res) {<br> console.log('支付成功');<br> console.log(res);<br> //return;<br> },<br> 'fail': function (res) {<br> console.log('支付失败');<br> //console.log(res);<br> return;<br> },<br> 'complete': function (res) {<br> console.log('支付完成');<br> console.log(res);<br> if (res.errMsg == 'requestPayment:ok') {<br> wx.navigateTo({<br> url: '/pages/pay/pay_success?products_name=' + products_name<br> })<br> }<br> return;<br> }<br> });<br> // console.log(res.data);<br> }<br> })<br>}, |
相信坚持的力量,日复一日的习惯.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)