public function anyWxPay() { if (Input::has('user_id') && Input::has('out_trade_no') && Input::has('goods_name') && Input::has('total_fee')) { $outTradeNo = Input::get('out_trade_no'); $goodsName = Input::get('goods_name'); if (Input::get('total_fee') <= 1) { $totalFee = 100; } else { $totalFee = (int) Input::get('total_fee') * 100; } } else { Log::error(var_export('参数错误', true), array(__CLASS__)); return Response::json($this->response('10005')); } //②、统一下单 请求微信预下单 $input = new WxPayUnifiedOrder(); $input->SetBody($goodsName); $input->SetOut_trade_no($outTradeNo); $input->SetTotal_fee($totalFee); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 7200)); $input->SetGoods_tag("test_goods"); $input->SetNotify_url(Input::root() . "/callback/weixin/callback"); $input->SetTrade_type("APP"); //浏览器测试记得注释掉 $inputObj->SetSpbill_create_ip("1.1.1.1"); $order = WxPayApi::unifiedOrder($input); if (array_key_exists('err_code', $order)) { return Response::json($this->response('0', $order['err_code'], $order['err_code_des'])); } if ($order['return_code'] == 'SUCCESS') { $timestamp = time(); //参与签名的字段 无需修改 预支付后的返回值 $arr = array(); $arr['appid'] = trim(WxPayConfig::APPID); $arr['partnerid'] = trim(WxPayConfig::MCHID); $arr['prepayid'] = $order['prepay_id']; $arr['package'] = 'Sign=WXPay'; $arr['noncestr'] = $order['nonce_str']; $arr['timestamp'] = $timestamp; $obj = new WxPayDataBase(); $obj->SetValues($arr); $sign = $obj->SetSign(); //返回给APP数据 $data = array(); $data['return_code'] = $order['return_code']; $data['return_msg'] = $order['return_msg']; $data['prepay_id'] = $order['prepay_id']; $data['trade_type'] = $order['trade_type']; $data['nonce_str'] = $order['nonce_str']; $data['timestamp'] = $timestamp; $data['sign'] = $sign; Log::error(var_export($data, true), array(__CLASS__)); return Response::json($this->response('1', '预订单成功', $data)); } else { Log::error(var_export('微信回调错误', true), array(__CLASS__)); return Response::json($this->response('0')); } }
public function postCallback() { //获取回调通知xml $xml = $GLOBALS['HTTP_RAW_POST_DATA']; Log::error(var_export($xml, true), array(__CLASS__)); $reply = new WxPayNotifyReply(); $data = $reply->FromXml($xml); Log::error(var_export($data, true), array(__CLASS__)); if (!$data) { Log::error(var_export('非法请求', true), array(__CLASS__)); return Response::json($this->response('10006')); } $return_code = $data['return_code']; if ($return_code == 'FAIL') { $err_code_des = $data['err_code_des']; Log::error(var_export('异步回调通知错误FAIL', true), array(__CLASS__)); return Response::json($this->response('0', $err_code_des)); } if ($return_code == 'SUCCESS') { //对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败, //微信会通过一定的策略(如30分钟共8次)定期重新发起通知, //尽可能提高通知的成功率,但微信不保证通知最终能成功。 $transaction_id = $data['transaction_id']; $input = new WxPayOrderQuery(); $input->SetTransaction_id($transaction_id); $result = WxPayApi::orderQuery($input); if (array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") { $total_fee = $data['total_fee'] / 100; //插入数据库 $weixinData = array('transaction_id' => $data['transaction_id'], 'out_trade_no' => $data['out_trade_no'], 'total_fee' => $total_fee, 'nonce_str' => $data['nonce_str'], 'sign' => $data['sign'], 'create_time' => $this->nowTime, 'time_expire' => date("Y-m-d H:i:s", time() + 7200), 'time_end' => $data['time_end'], 'is_subscribe' => $data['is_subscribe'], 'trade_type' => $data['trade_type'], 'bank_type' => $data['bank_type'], 'fee_type' => $data['fee_type'], 'cash_fee' => $data['cash_fee'], 'appid' => $data['appid'], 'mch_id' => $data['mch_id'], 'openid' => $data['openid'], 'return_code' => $data['return_code']); $trade_status = $data['return_code']; $out_trade_no = $data['out_trade_no']; $pay_amount = $total_fee; $flag = false; if ($trade_status == 'SUCCESS') { $weixinInfo = $this->jnlWeixinModel->load($transaction_id); if (empty($weixinInfo)) { $this->jnlWeixinModel->add($weixinData); $flag = true; } else { $db_status = $weixinInfo->return_code; if ($db_status != $trade_status) { $param = ['return_code' => $trade_status, 'create_time' => $this->nowTime]; $this->jnlWeixinModel->update($transaction_id, $param); } } } try { if ($flag) { $id = $this->rechargeService->recharge($out_trade_no, $pay_amount, $transaction_id, JnlService::Recharge_Channel_weixin); if ($id) { if ($this->jnlService->payCallbackUpdateJnl($out_trade_no, $pay_amount, $id, JnlService::JnlTrans_Status_pay_success, JnlService::Recharge_Channel_weixin)) { $this->periodService->duobaoNeedRecharge($out_trade_no); } } } } catch (\Exception $e) { Log::error(var_export($e, true), array(__CLASS__)); } return 'SUCCESS'; } return 'FAIL'; } else { //验证失败 return 'FAIL'; } }