public function app_test(){ require_once getcwd()."/Wxpay/lib/WxPay.Api.php"; require_once getcwd()."/Wxpay/unit/WxPay.JsApiPay.php"; $payAmount = (string)100; //统一下单 $tools = new JsApiPay(); $input = new WxPayUnifiedOrder(); $input->SetBody("test"); $input->SetAttach("test"); $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis")); $input->SetTotal_fee($payAmount); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); //$input->SetNotify_url(C('SITE_URL')."/index.php/Index/wx_notify"); $input->SetNotify_url(C('SITE_URL')."/index.php/Wx_Payment/ali_notify_merge"); $input->SetTrade_type("APP"); $order = WxPayApi::unifiedOrder($input); $this->json['appid'] = $order['appid']; $this->json['partnerid'] = $order['mch_id']; $this->json['prepayid'] = $order['prepay_id']; $this->json['package'] = 'Sign=WXPay'; $this->json['noncestr'] = $order['nonce_str']; $this->json['timestamp'] = time(); //再次生成签名 ksort($this->json); //$string = $aa->ToUrlParams($this->json); $buff = ""; foreach ($this->json as $k => $v) { if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); $string = $buff; //签名步骤二:在string后加入KEY $string = $string . "&key=".WxPayConfig::KEY; //签名步骤三:MD5加密 $string = md5($string); //签名步骤四:所有字符转为大写 $result = strtoupper($string); $this->json['sign'] = $result; echo json_encode($this->json); }
/** * * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填 * appid、mchid、spbill_create_ip、nonce_str不需要填入 * @param WxPayUnifiedOrder $inputObj * @param int $timeOut * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static function unifiedOrder($inputObj, $timeOut = 6) { $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //检测必填参数 if (!$inputObj->IsOut_trade_noSet()) { throw new WxPayException("缺少统一支付接口必填参数out_trade_no!"); } else { if (!$inputObj->IsBodySet()) { throw new WxPayException("缺少统一支付接口必填参数body!"); } else { if (!$inputObj->IsTotal_feeSet()) { throw new WxPayException("缺少统一支付接口必填参数total_fee!"); } else { if (!$inputObj->IsTrade_typeSet()) { throw new WxPayException("缺少统一支付接口必填参数trade_type!"); } } } } //关联参数 if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) { throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) { throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!$inputObj->IsNotify_urlSet()) { $inputObj->SetNotify_url(WxPayConfig::NOTIFY_URL); //异步通知url } $inputObj->SetAppid(WxPayConfig::APPID); //公众账号ID $inputObj->SetMch_id(WxPayConfig::MCHID); //商户号 $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']); //终端ip //$inputObj->SetSpbill_create_ip("1.1.1.1"); $inputObj->SetNonce_str(self::getNonceStr()); //随机字符串 //签名 $inputObj->SetSign(); $xml = $inputObj->ToXml(); $startTimeStamp = self::getMillisecond(); //请求开始时间 $response = self::postXmlCurl($xml, $url, false, $timeOut); $result = WxPayResults::Init($response); self::reportCostTime($url, $startTimeStamp, $result); //上报请求花费时间 return $result; }
/** * 收银台 */ public function pay(){ //订单信息 $model = M('Order'); if($_REQUEST['order_sn']){ $data['order_sn'] = $_REQUEST['order_sn']; $out_trade_no = $_REQUEST['order_sn']; //单一订单支付 $notify_url = C('SITE_URL') . '/index.php/Wx_Payment/notify'; } if($_REQUEST['mo_sn']){ $data['mo_sn'] = $_REQUEST['mo_sn']; $out_trade_no = $_REQUEST['mo_sn']; //多订单合并支付 $notify_url = C('SITE_URL') . '/index.php/Wx_Payment/notify_merge'; } $data['member_id'] = $this->user['id']; $orders = $model->field('id,title,order_sn,total_fee,actual_paid,status')->where($data)->select(); //echo $model->getlastsql();dump($orders);exit; if(!$orders){ $this->error('订单不存在'); } $total_fee = 0; $title = ''; foreach($orders as $key=>$order){ $total_fee += $order['total_fee']; $titles[] = $order['title']; if($order['status']!=1 || $order['pay_status']!=0){ $this->error('订单:'.$order['order_id'].'状态错误'); } } $body = implode(',',$titles); //支付接口请求 /* require_once(C('INTERFACE_PATH')."Api/config.php"); require_once(C('INTERFACE_PATH')."Api/Api.class.php"); $api = new Api($config); $para['m'] = 'Payment'; $para['a'] = 'wxWapPay'; if($_REQUEST['order_sn'])$data['order_sn'] = $_REQUEST['order_sn']; if($_REQUEST['mo_sn'])$data['mo_sn'] = $_REQUEST['mo_sn']; $data['openid'] = 'oO8Umt19WO-Ci88IrRm2ywXgr9OM'; $result = json_decode($api->get($para,$data),true); dump($result);exit; */ //如果是微信浏览器直接可支付 $isWeixin = isWeixin(); if($isWeixin){ $openId = cookie('wx_real_openid'); $openId = authcode($openId); $body = $body; $total_fee = $total_fee*100; $total_fee = (string)$total_fee; require_once C('INTERFACE_PATH')."wxwappay/lib/WxPay.Api.php"; require_once C('INTERFACE_PATH')."wxwappay/unit/WxPay.JsApiPay.php"; $tools = new JsApiPay(); $input = new WxPayUnifiedOrder(); $input->SetBody($body); $input->SetOut_trade_no($out_trade_no); $input->SetTotal_fee($total_fee); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); //$input->SetGoods_tag("test"); $input->SetNotify_url($notify_url); $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $payOrder = WxPayApi::unifiedOrder($input); $jsApiParameters = $tools->GetJsApiParameters($payOrder); $this->assign('jsApiParameters',$jsApiParameters); } $this->assign('isWeixin',$isWeixin); $this->assign('order',$order); $this->assign('headerTitle','订单支付页'); $this->assign('headerKeywords','订单支付页'); $this->assign('headerDescription','订单支付页'); $this->assign('wx_title','订单支付页'); $this->assign('wx_desc',C('wx_desc')); $this->display(); }
public function pay() { ini_set('date.timezone', 'Asia/Shanghai'); require_once "lib/WxPay.Api.php"; require_once "WxPay.JsApiPay.php"; $sn = $_GET['sn']; $code = $_GET['code']; $data = json_decode(cookie('login'), true); $arr = D('Shop')->orderdetail($sn, $data['user']); if ($arr['show_status'] == '未付款') { $tools = new JsApiPay(); $openId = $tools->getOpenidFromMp($code); $input = new WxPayUnifiedOrder(); $input->SetBody("民间厨王"); $input->SetAttach("test"); $input->SetOut_trade_no($sn); $total_fee = $arr['goods_price'] * 100; $input->SetTotal_fee(intval($total_fee)); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://www.minjianchuwang.com/Home/Order/notify_url.html"); $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $order = WxPayApi::unifiedOrder($input); $jsApiParameters = $tools->GetJsApiParameters($order); $this->assign('jsApiParameters', $jsApiParameters); $daojishi = $arr['add_time'] + 600 - time(); $this->assign('daojishi', $daojishi); $this->assign('orderdetail', $arr); $this->assign('address', explode(';', $arr['address'])); $totalcount = M('cart')->where(array('user' => $data['user']))->sum('goods_number'); $this->assign('totalcount', $totalcount); $this->display('order_details'); } else { $this->redirect('Order/index'); } }
/** * 订单微信支付 * @author 李东 * @date 2015-07-21 */ public function index() { /*获取微信支付配置信息*/ $wxpay_config = C('payment.wxpay'); $notify = new \NativePay(); //$url1 = $notify->GetPrePayUrl("123456789"); $input = new \WxPayUnifiedOrder(); $input->SetBody("test"); /*设置商品或支付单简要描述*/ $input->SetAttach("test"); /*设置附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据*/ $input->SetOut_trade_no($wxpay_config['mchid'] . date("YmdHis")); /*设置商户系统内部的订单号,32个字符内、可包含字母*/ $input->SetTotal_fee("1"); /*设置订单总金额,只能为整数;如0.01元,设置为1*/ $input->SetTime_start(date("YmdHis")); /*设置订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。*/ $input->SetTime_expire(date("YmdHis", time() + 600)); /*设置订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。*/ $input->SetGoods_tag("test"); /*设置商品标记,代金券或立减优惠功能的参数*/ $input->SetNotify_url('http://' . I('server.HTTP_HOST') . U('notify')); /*设置接收微信支付异步通知回调地址*/ $input->SetTrade_type("NATIVE"); /*设置支付方式,取值如下:JSAPI,NATIVE,APP;*/ $input->SetProduct_id("123456789"); /*设置trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。*/ $result = $notify->GetPayUrl($input); /*获取返回参数*/ $url = 'http://' . I('server.HTTP_HOST') . U('create_code') . '?data=' . urlencode($result["code_url"]); /*返回的支付二维码图片地址*/ $result['show_code'] = $url; $data['result'] = $result; $this->assign($data); $this->display(); }