php微信支付

服务端控制器代码

class PaymentController  {
	private $common = null;

	public function __construct() {
		$this->common = new Common();
	}

	private function message($message, $error = '1') {
		$this->common->jsonMessage($message, $error);
	}

	private function json($data) {
		$this->common->jsonData($data);
	}

	public function index() {
		$this->message('禁止访问');
	}

	/**
	 * 微信支付异步通知
	 */
	public function wxpay_notify() {
		$xml = !empty($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents('php://input');
		$this->logs($xml, '_wxpay_notify');
		require_once(COMMON_PATH.'Payment/wxpay/app/notify.php');
		$notify = new \PayNotifyCallBack();
		$notify->Handle(false);
	}

	/**
	 * 微信支付
	 */
	public function wxPay(){
		$order_amount = '1.00';//支付金额
		$user_id = I('user_id');
		//获取openid
		$openId = M('oauth')->where('user_id='.$user_id)->find();//在用户登录时存下用户的openid,此处取用
		if (empty($openId['friend_openid'])) {
			$this->message('用户状态异常', '2');
		}
		header('Access-Control-Allow-Origin: *');
		header('Content-type: text/plain');
		require_once COMMON_PATH.'Payment/wxpay/friendPay/WxPay.Api.php';//微信sdk

		$unifiedOrder = new \WxPayUnifiedOrder();
		$unifiedOrder->SetBody('小程序支付');//商品或支付单简要描述
		$unifiedOrder->SetOut_trade_no(mt_rand(1000, 9999));
		$unifiedOrder->SetTotal_fee($order_amount * 100);//支付金额要乘100,详见微信官方文档
		$unifiedOrder->SetTrade_type('JSAPI');
		$unifiedOrder->SetNotify_url(APP_URL.'Payment/wxpay_notify');
		$unifiedOrder->SetOpenid($openId['openid']);
		try{
			$result = \WxPayApi::unifiedOrder($unifiedOrder);
			//$this->logs([$unifiedOrder, $result]);
			if (is_array($result)) {
				$arr = array();
				$arr['appId']     = $result['appid'];
				$arr['nonceStr']  = $result['nonce_str'];
				$arr['package']   = "prepay_id=".$result['prepay_id'];
				$arr['signType']  = "MD5";
				$arr['timeStamp'] = (string)time();
				$str = $this->ToUrlParams($arr);//调用构建字符串
				$jmstr = $str."&key=".\WxPayConfig::KEY;
				$arr['paySign'] = strtoupper(MD5($jmstr));
				echo json_encode($arr);
				exit;
			}
		} catch (\WxPayException $e) {
			$this->logs($e);
			$this->message('调取支付失败');
		}
		exit;
	}
		//构建字符串
	private function ToUrlParams($urlObj){
		$buff = "";
		foreach ($urlObj as $k => $v)
		{
			if($k != "sign"){
				$buff .= $k . "=" . $v . "&";
			}
		}
		
		$buff = trim($buff, "&");
		return $buff;
	}
}

在下载的sdk的notify.php中重写回调函数

require_once __DIR__ . '/WxPay.Api.php';
require_once __DIR__ . '/WxPay.Notify.php';

class PayNotifyCallBack extends WxPayNotify {

	//查询订单
	public function Queryorder($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'
		) {
			return true;
		}
		return false;
	}

	//重写回调处理函数
	public function NotifyProcess($data, &$msg) {
		logs($data, '_notify_wx');
		if (!array_key_exists('transaction_id', $data)) {
			$msg = '输入参数不正确';
			return false;
		}
		//查询订单,判断订单真实性
		if (!$this->Queryorder($data['transaction_id'])) {
			$msg = '订单查询失败';
			return false;
		}
		//在此处进行回调后的逻辑处理
		return true;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值