public function run($open_id, $total_fee, $body, $detail, $out_trade_no) { $response = array('error' => 1, 'msg' => ''); $_SESSION['payment'] = 'wechat'; global $config; $res = create_prepay($config['appid'], $this->mch_id, $this->mch_key, $open_id, $total_fee, $body, $detail, $out_trade_no); $res = simplexml_load_string($res); if ($res->prepay_id) { $response['error'] = 0; } else { $response['msg'] = $res->return_code . ',' . $res->return_msg; } $nonce_str = get_nonce_str(); $response['nonce_str'] = $nonce_str; $time_stamp = time(); //最后参与签名的参数有appId, timeStamp, nonceStr, package, signType。 $sign = 'appId=' . $config['appid'] . '&nonceStr=' . $nonce_str . '&package=prepay_id=' . $res->prepay_id . '&signType=MD5&timeStamp=' . $time_stamp . '&key=' . $this->mch_key; $sign = md5($sign); $sign = strtoupper($sign); $response['timestamp'] = $time_stamp; $response['sign'] = $sign; $response['prepay_id'] = "" . $res->prepay_id; return $response; }
if ($level_id == 2) { $db->autoUpdate('member', array('status' => 2, 'level_expired' => time() + 365 * 24 * 3600), '`account`=\'' . $account . '\''); } else { $db->autoUpdate('member', array('status' => 2), '`account`=\'' . $account . '\''); } //结算 settle($recommend_info['recommend_path'] . $register_id . ',', $total_amount, $pay_order_sn); } else { $res = create_prepay($config['appid'], $config['mch_id'], $config['mch_key'], $_SESSION['openid'], $real_amount, $config['site_name'], $pay_order_sn, $pay_order_sn); $res = simplexml_load_string($res); if ($res->prepay_id) { $response['error'] = 0; } else { $response['msg'] = $res->return_code . ',' . $res->return_msg; } $nonce_str = get_nonce_str(); $pay_params = array(); $pay_params['nonce_str'] = $nonce_str; $time_stamp = time(); //最后参与签名的参数有appId, timeStamp, nonceStr, package, signType。 $sign = 'appId=' . $config['appid'] . '&nonceStr=' . $nonce_str . '&package=prepay_id=' . $res->prepay_id . '&signType=MD5&timeStamp=' . $time_stamp . '&key=' . $config['mch_key']; $sign = md5($sign); $sign = strtoupper($sign); $pay_params['timestamp'] = $time_stamp; $pay_params['sign'] = $sign; $pay_params['prepay_id'] = "" . $res->prepay_id; $response['pay_params'] = $pay_params; } $db->commit(); $response['error'] = 0; $response['msg'] = '会员注册成功!<br/>会员编号:' . $account . '<br/>登录密码:123456<br/>订单编号:' . $pay_order_sn;
/** * 生成预支付交易单 */ function create_prepay($appid, $mch_id, $mch_key, $openid, $total_fee, $body, $detail, $out_trade_no, $params = array()) { global $config; $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $now = time(); $data = array('appid' => $appid, 'mch_id' => $mch_id, 'openid' => $openid, 'total_fee' => $total_fee * 100, 'nonce_str' => get_nonce_str(), 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], 'fee_type' => 'CNY', 'time_start' => date('YmdHis', $now), 'time_expire' => date('YmdHis', $now + 3600 * 24 * 7), 'notify_url' => 'http://' . $_SERVER['HTTP_HOST'] . '/notify.php', 'trade_type' => 'JSAPI', 'body' => $body, 'detail' => $detail, 'out_trade_no' => $out_trade_no); $data = array_merge($data, $params); ksort($data); $param_str = ''; $xml = new SimpleXMLElement('<xml></xml>'); foreach ($data as $key => $value) { if (empty($value)) { unset($data[$key]); } else { $param_str .= $key . '=' . $value . '&'; $xml->addChild($key, $value); } } $param_str .= 'key=' . $mch_key; $sign = md5($param_str); $sign = strtoupper($sign); $xml->addChild('sign', $sign); $response = post($url, $xml->asXML(), false); return $response; }