微信支付接口

发起支付请求:

nonceStr			= $this->getNonceStr();
		$this->timeStamp		= (string) time();
		$this->spbill_create_ip	= $_SERVER['REMOTE_ADDR'];
	}

	public function makeParams()
	{
		$params = array();
		/* package 加密过程 开始*/
		// 1,按字典排序参数
		$packAge_step1 = "attach={$this->attach}&bank_type={$this->bank_type}&body={$this->body}&fee_type={$this->fee_type}&input_charset={$this->input_charset}¬ify_url={$this->notify_url}&out_trade_no={$this->out_trade_no}&partner={$this->partner}&spbill_create_ip={$this->spbill_create_ip}&total_fee={$this->total_fee}";
		
		// 2,md5加密$packAge_step1,连接key=$partnetKey,再把连接好的字符串转化成大写
		$sign		=  strtoupper(md5($packAge_step1.'&key='.$this->partnetKey));
		
		// 3,再对string1中的每一个键值对中的value进行$this->encodeURIComponent编码后得到
		$packAge_step2 = 'attach='.$this->encodeURIComponent($this->attach).'&bank_type='.$this->encodeURIComponent($this->bank_type).'&body='.$this->encodeURIComponent($this->body).'&fee_type='.$this->encodeURIComponent($this->fee_type).'&input_charset='.$this->encodeURIComponent($this->input_charset).'¬ify_url='.urlencode($this->notify_url).'&out_trade_no='.urlencode($this->out_trade_no).'&partner='.urlencode($this->partner).'&spbill_create_ip='.urlencode($this->spbill_create_ip).'&total_fee='.urlencode($this->total_fee);
		
		// 	4,拼接上sign后得到最终package结果:
		$packAge_step3 = $packAge_step2.'&sign='.$sign;
		
		$package = $packAge_step3;
		/* package 加密过程 结束*/
		
		/* paySign 加密过程 开始*/
		// 1,按字典排序参数
		$paySign_step1 = "appid={$this->appId}&appkey={$this->paySignKey}&noncestr={$this->nonceStr}&package={$package}×tamp={$this->timeStamp}";
		
		// 2,用sha1方式加密
		$paySign = sha1($paySign_step1);
		/* paySign 加密过程 结束*/
		
		
		/************************************************************************************************************************************************
		 * return 
		 *************************************************************************************************************************************************/
		$params['appId']		= $this->appId;
		$params['timeStamp']	= $this->timeStamp;
		$params['nonceStr']		= $this->nonceStr;
		$params['package']		= $package;
		$params['signType']		= $this->signType;
		$params['paySign']		= $paySign;
		
		return $params;
		/*************************************************************************************************************************************************/
	}
	
	public function getNonceStr()
	{
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
		$maxPos = strlen($chars)-1;
		$noceStr = "";
		for ($i = 0; $i < 32; $i++)
		{
			$noceStr .= $chars[rand($i, $maxPos)];
		}
		return $noceStr;
	}
	
	public function priceFormat($price=0)
	{
		return floor($price*100);
	}
	
	//模拟js中的encodeURIComponent方法
	public function encodeURIComponent($str) 
	{
		$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
		return strtr(rawurlencode($str), $revert);
	}
	
}
?>

支付成功回调函数


checkParams();
		
		$out_trade_no	= $this->input->get('out_trade_no');	// 订单号
		$trade_state	= $this->input->get('trade_state');		// 支付状态 0 成功
		$total_fee		= $this->input->get('total_fee');		// 订单金额
		$transaction_id	= $this->input->get('transaction_id');	// 微信支付流水号
		
		if($trade_state != 0)
		{
			exit('falid');
		}
		
		$this->load->model('/order/checkOrder','order');
		$order = $this->order->getOrderInfo($out_trade_no);
		if(!$order)
		{
			exit('falid');
		}
		
		if($order['pay_status'] == 1)
		{
			exit('success');
		}
		
		// 获得用户名
		$this->load->model('user');
		$user = $this->user->getNameById($order['member_id']);
		
		// 获得payment_id
		$this->load->model('payments');
		$payment_id = $this->payments->getPaymentsId();
		
		//  生成收款单信息
		$our_total_fee	= $total_fee/100;
		$pay_time		= time();
		$payments = array();
		$payments['payment_id']	= $payment_id;
		$payments['money']		= $our_total_fee;
		$payments['cur_money']	= $our_total_fee;
		$payments['member_id']	= $order['member_id'];;
		$payments['status']		= 'succ';
		$payments['pay_name']	= '微信支付';
		$payments['pay_type']	= 'online';
		$payments['t_payed']	= $pay_time;
		$payments['op_id']		= 10218489;		// admin
		$payments['account']	= '微信支付';
		$payments['pay_account']= $user['name'];
		$payments['currency']	= 'CNY';
		$payments['pay_app_id']	= '微信支付';
		$payments['t_begin']	= $pay_time;
		$payments['t_confirm']	= $pay_time;
		$payments['memo']		= '微信支付';
		$payments['trade_no']	= $transaction_id;
		
		$bills = array();
		$bills['rel_id']		= $out_trade_no;
		$bills['bill_type']		= 'payments';
		$bills['pay_object']	= 'order';
		$bills['bill_id']		= $payment_id;
		$bills['money']			= $our_total_fee;
		/*--------------------------------------------------------------------------------
		 * 更新订单状态,生成收款单。生成银行单据
		 -------------------------------------------------------------------------------- */
		$this->db->trans_start();
		$status	= array('pay_status'=>'1','payed'=>$our_total_fee);
		$where	= array('order_id'=>$out_trade_no);
		$this->db->update('sdb_b2c_orders',$status,$where);
		$this->db->insert('sdb_ectools_payments',$payments);
		$this->db->insert('sdb_ectools_order_bills',$bills);
		$this->db->trans_complete();
		/*------------------------------------------------------------------------------- */
		if ($this->db->trans_status() === FALSE)
		{
			exit('falid');
		}
		
		if($trade_state == 0)
		{
			exit('success');
		}
	}
	
	private function checkParams()
	{
		$attach			= $this->input->get('attach');			//  支付提交 accach
		$partner		= (int) $this->input->get('partner');			//  商户号
		
		if($attach != '0d10b6fc91971c0a76c522ee1547ca66' || $partner != your id)
		{
			exit('You are not allowed to visit this page.');
		}
	}
	
	public function rights()
	{
		// 用户维权接口 ,暂不使用。
	}
	
	public function delivernotify()
	{
		echo phpinfo();
		$c = curl_init();
		//
	}
}


控制器调用


/*---------------------------------------------------------------------------------------------------------------------
* 微信支付
---------------------------------------------------------------------------------------------------------------------*/
$this->load->library('wx_pay');
$wxPay = new wx_pay();
$wxPay->body			= $body;
$wxPay->product_fee		= $wxPay->priceFormat($goods_price);
$wxPay->transport_fee		= $wxPay->priceFormat($ship_fee);
$wxPay->total_fee		= $wxPay->priceFormat($total_fee);
$wxPay->out_trade_no		= $order_id;
$assign_data['wx_pay'] 		= $wxPay->makeParams();
/*--------------------------------------------------------------------------------------------------------------------*/

$this->load->view('/order/submit',$assign_data);





你可能感兴趣的:(程序员人生,php学习)