public function unifiedorder($openId, $product_id) { //统一下单 $input = new WxPayUnifiedOrder(); $input->SetBody("test"); $input->SetAttach("test"); $input->SetOut_trade_no(WxPayConfig::$mchId . date("YmdHis")); $input->SetTotal_fee("1"); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php"); $input->SetTrade_type("NATIVE"); $input->SetOpenid($openId); $input->SetProduct_id($product_id); $result = WxPayApi::unifiedOrder($input); // Log::DEBUG("unifiedorder:" . json_encode($result)); return $result; }
/** * 二维码支付 */ public function actionNative() { $request = Yii::$app->getRequest(); $params = $request->get(); if (!isset($params['signature']) || !$params['signature']) { // TODO:: 必须传递签名参数 return; } $signature = rawurldecode($params['signature']); unset($params['signature']); $data = arrayToQuery($params); $file = dirname(Yii::$app->getBasePath()) . '/common/rsa_key/signature/public_key.pem'; $result = AsymmetryCrypt::verifySign($data, $signature, $file); if (!$result) { return; } $notifyUrl = Url::toRoute(['wxpay/notify'], true); $notify = new NativePay(); $url = ''; //模式一 /** * 流程: * 1、组装包含支付信息的url,生成二维码 * 2、用户扫描二维码,进行支付 * 3、确定支付之后,微信服务器会回调预先配置的回调地址,在【微信开放平台-微信支付-支付配置】中进行配置 * 4、在接到回调通知之后,用户进行统一下单支付,并返回支付信息以完成支付(见:native_notify.php) * 5、支付完成之后,微信服务器会通知支付成功 * 6、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php) */ //$url = $notify->GetPrePayUrl("123456789"); //模式二 /** * 流程: * 1、调用统一下单,取得code_url,生成二维码 * 2、用户扫描二维码,进行支付 * 3、支付完成之后,微信服务器会通知支付成功 * 4、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php) */ $input = new WxPayUnifiedOrder(); $input->SetBody($params['product_name']); if (isset($params['attach'])) { $input->SetAttach($params['attach']); } $input->SetOut_trade_no($params['order_sn']); $input->SetTotal_fee($params['turnover'] * 100); $input->SetTime_start(date('YmdHis', $params['create_time'])); $input->SetTime_expire(date('YmdHis', $params['expires_time'])); if (isset($params['product_desc'])) { $input->SetGoods_tag($params['product_desc']); } $input->SetNotify_url($notifyUrl); $input->SetTrade_type("NATIVE"); $input->SetProduct_id($params['product_id']); $result = $notify->GetPayUrl($input); $url = $result["code_url"]; $qrCode = new QrCode(); $qrCode->setText($url)->setSize(100)->setPadding(10)->setErrorCorrection('high')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setLabel('')->setLabelFontSize(16)->render(); }