/** * 微信支付通知 * */ public function action_notice_wxpay() { //获取微信支付服务器提供的数据 $xml = $GLOBALS['HTTP_RAW_POST_DATA']; $result = \handler\common\Tool::xmlToArray($xml); //获取商户的支付配置信息 $trade = \Model_OrderTrade::query()->where('out_trade_no', $result['out_trade_no'])->get_one(); if (!$trade) { die('trade empty'); } //订单交易对象 $trade->response_msg = json_encode($result); $trade->return_trade_no = $result['transaction_id']; $trade->real_money = $result['total_fee'] / 100; $trade->updated_at = time(); //订单对象 $order = $trade->order; //支付配置 $access = \Model_AccessConfig::query()->where('access_type', 'wxpay')->where('seller_id', $order->from_id)->where('enable', 'ENABLE')->get_one(); //检验签名 $tmpSign = $result; unset($tmpSign['sign']); $sign = handler\mp\Tool::getWxPaySign($tmpSign, $access->access_key); $params = array('return_code' => 'SUCCESS'); if ($result['sign'] != $sign) { $order->order_status = 'PAYMENT_ERROR'; $trade->return_status = 'ERROR'; $params = array('return_code' => 'FAIL', 'return_msg' => '签名失败'); } else { if ($order->order_status == 'WAIT_PAYMENT') { $order->paid_fee += $result['total_fee'] / 100; $order->pay_at = time(); if ($order->paid_fee >= $order->original_money) { $order->order_status = 'PAYMENT_SUCCESS'; } $trade->return_status = 'SUCCESS'; $trade->return_trade_no = $result['transaction_id']; $trade->response_msg = json_encode($result); } } if ($order->save() && $order->remark == 'qrcode') { \Model_Order::delivery($order->id); } $trade->save(); $data = \handler\common\Tool::arrayToXml($params); die($data); }
/** * 发起统一支付 * * @param $account 微信公众号对象 * @param $order 订单对象 * @param $access 支付配置对象 * @param $openid 微信Openid * @param string $trade_type 支付类型 * @return bool|\handler\common\Array * @throws \Exception */ public static function wxpay_order($account, $order, $access, $openid, $trade_type = 'JSAPI') { //创建支付记录 $ip = \Input::real_ip(); $timestamp = time(); //创建支付单号 $order_no = md5("{$order->order_no}{$timestamp}{$ip}"); $data = array('order_id' => $order->id, 'return_status' => 'NONE', 'out_trade_no' => $order_no, 'remark' => \Input::get('remark', ''), 'name' => \Input::get('name', ''), 'name_stype' => \Input::get('name_style', ''), 'real_money' => \Input::get('total_fee', $order->original_fee), 'openid' => $openid); $trade = \Model_OrderTrade::forge($data); if (!$trade->save()) { \Log::error('微信支付时发生异常,原因:交易记录创建失败'); die('trade save error!'); } //是否指定收款金额 $total_fee = $order->original_fee; if (\Input::get('total_fee', false)) { $total_fee = floatval(\Input::get('total_fee')); } $params = array('openid' => $openid, 'body' => $order->order_body ? $order->order_body : '', 'out_trade_no' => $order_no, 'total_fee' => $total_fee * 100, 'notify_url' => \Config::get('base_url') . 'services/gateway/notice_wxpay', 'trade_type' => $trade_type, 'appid' => $account->app_id, 'mch_id' => $access->access_id, 'nonce_str' => \Str::random('alnum', 16)); $params['sign'] = static::getWxPaySign($params, $access->access_key); $data = \handler\common\Tool::arrayToXml($params); $data = "<xml>{$data}</xml>"; $result = \handler\common\UrlTool::request_xml('https://api.mch.weixin.qq.com/pay/unifiedorder', 'POST', $data); $result = \handler\common\Tool::xmlToArray($result); if ($result['return_code'] == 'FAIL') { var_dump($result); die; } return $result; }