<?php namespace Pay\Lib;
use Dever;

#银联商务正常交易
class Yspay extends Core
{
	public function __construct($config)
	{
		$project = Dever::project('pay');
		# 通知接口
		$config['notify'] = $this->url($config['type'], $config['id']);
		$set = explode('|', $config['mchid']);
		$config['mid'] = $set[0];
		$config['tid'] = $set[1];
		if (isset($set[2])) {
			$config['prefix'] = $set[2];
		}

		$set = explode('|', $config['key']);
		$config['sub_appid'] = $set[0];
		$config['sub_appsecret'] = $set[1];
		if (isset($set[2])) {
			$config['key'] = $set[2];
		}
		$this->config = $config;
	}

	/**
	 * 通知
	 */
	public function notify()
	{
		$input = file_get_contents("php://input");
		if ($input) {
			parse_str($input, $input);
		} else {
			$input = Dever::input();
		}
		$test = Dever::input('test');
		if ($test == 1) {
			$input = json_decode('{"msgType":"wx.notify","payTime":"2021-12-29 18:24:42","buyerCashPayAmt":"1","connectSys":"UNIONPAY","sign":"E82978003E0121B320D3520B1930FB4C2072B639126F3427351631F1D0A998CF","merName":"中食民安（北京）科技有限公司","mid":"89810007372106N","invoiceAmount":"1","settleDate":"2021-12-29","billFunds":"现金:1","buyerId":"otdJ_uD-zTNmDn7-u13oActXu8lA","mchntUuid":"2d9081bd7db8ad75017dbbe68981314f","tid":"DM098308","instMid":"MINIDEFAULT","receiptAmount":"1","couponAmount":"0","targetOrderId":"4200001344202112290882463274","signType":"SHA256","billFundsDesc":"现金支付0.01元。","subBuyerId":"oGlMn5e_vdYK8uAaCgyexarFKBrY","orderDesc":"中食民安（北京）科技有限公司","seqId":"25267273719N","merOrderId":"138K_G202112297347788956443487","targetSys":"WXPay","bankInfo":"OTHERS","totalAmount":"1","createTime":"2021-12-29 18:24:38","buyerPayAmount":"1","iB":"KRHn","notifyId":"d4122965-b772-4f8f-b5c4-f3d483da0962","subInst":"100000","status":"TRADE_SUCCESS"}', true);

			$input = json_decode('{"bankInfo":"OTHERS","billDate":"2022-01-06","billDesc":"中食民安（北京）科技有限公司","billNo":"138KG202201064666839470673943","billPayment":"{\"payTime\":\"2022-01-06 13:24:56\",\"buyerCashPayAmt\":1,\"paySeqId\":\"25394941600N\",\"invoiceAmount\":1,\"settleDate\":\"2022-01-06\",\"buyerId\":\"otdJ_uHh75dIaI6EKu-6usw0hjD4\",\"receiptAmount\":1,\"totalAmount\":1,\"couponAmount\":0,\"billBizType\":\"bills\",\"buyerPayAmount\":1,\"targetOrderId\":\"4200001348202201065614123115\",\"payDetail\":\"现金支付0.01元。\",\"merOrderId\":\"138KG2022010646668394706739430\",\"status\":\"TRADE_SUCCESS\",\"targetSys\":\"WXPay\"}","billQRCode":"https:\/\/qr.95516.com\/48020000\/138K2201066502428138291294","billStatus":"PAID","createTime":"2022-01-06 13:24:28","instMid":"QRPAYDEFAULT","mchntUuid":"2d9081bd7db8ad75017dbbe68981314f","merName":"中食民安（北京）科技有限公司","mid":"89810007372106N","notifyId":"49a83755-b476-47c3-a0b6-629dc7a43742","qrCodeId":"138K2201066502428138291294","qrCodeType":"BILLPAY","receiptAmount":"1","seqId":"25394941600N","signType":"SHA256","subInst":"100000","tid":"DM098308","totalAmount":"1","vE":"Bclv","sign":"1D048E292E17225A4EEF4AB438630601A19E50CDA71DF9E7A4304AABBD134A38","e_sign":"1D048E292E17225A4EEF4AB438630601A19E50CDA71DF9E7A4304AABBD134A38"}', true);
		}
		$sign = $input['sign'];
		unset($input['sign']);
		if (isset($input['e_sign'])) {
			unset($input['e_sign']);
		}
		
		ksort($input);

		$string = array();
		foreach ($input as $k => $v) {
			if (strstr($v, '&') || strstr($v, '@')) {
				$v = urlencode($v);
			}
			$string[] = $k . '=' . $v;
		}
		$string = implode('&', $string);
		$string .= $this->config['key'];

		if ($input['signType'] == 'SHA256') {
			$string = hash("sha256", $string);
		} else {
			$string = md5($string);
		}

		$string = strtoupper($string);

		$input['sign'] = $sign;
		$input['e_sign'] = $string;
		$this->log('支付回调-初始化', $input);

		if ($sign == $string) {
			if (isset($input['billNo']) && $input['billNo']) {
				$key = 'billNo';
			} else {
				$key = 'merOrderId';
			}
			if (isset($this->config['prefix']) && $this->config['prefix']) {
				$input[$key] = str_replace($this->config['prefix'], '', $input[$key]);
			}
			if (isset($input['billPayment'])) {
				$input['billPayment'] = json_decode($input['billPayment'], true);
				if (isset($input['billPayment']['status'])) {
					$input['status'] = $input['billPayment']['status'];
				}
			}
			if (!isset($input['status'])) {
				echo 'FAILED';die;
			}
			$desc = '';
			if ($input['status'] == 'TRADE_SUCCESS') {
				# 成功
			} elseif ($input['status'] == 'TRADE_REFUND') {
				# 退款
				echo 'SUCCESS';die;
			} elseif ($input['status'] == 'TRADE_CLOSED') {
				$desc = '交易关闭';
			} elseif ($input['status'] == 'UNKNOWN') {
				$desc = '不明确的交易状态';
			} else {
				echo 'FAILED';die;
			}
			$this->updateOrder($input[$key], $input['totalAmount']);
			echo 'SUCCESS';die;
		} else {
			echo 'FAILED';die;
		}
	}

	private function getType($type)
	{
		switch ($type) {
			case 1:
				//小程序支付
				$type = 'MINI';
				break;
			
			case 2://扫码支付
				$type = 'APP';
				break;

			case 3://APP支付
				$type = 'APP';
				break;

			case 4://页面拉起支付
				$type = 'YUEDAN';
				break;
		}

		return $type;
	}

	/**
	 * 获取统一下单的基本信息
	 */
	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false, $refer = false)
	{
		$trade_type = $this->getType($type);
		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);

		$prefix = '';
		if (isset($this->config['prefix']) && $this->config['prefix']) {
			$prefix = $this->config['prefix'];
		}
		$request['merOrderId'] = $prefix . $order_id;

		$request['mid'] = $this->config['mid'];
		$request['tid'] = $this->config['tid'];
		$request['instMid'] = $trade_type . 'DEFAULT';
		$request['totalAmount'] = round($cash * 100, 2);
		//$request['totalAmount'] = 100;
		$request['subAppId'] = $this->config['sub_appid'];
		$request['requestTimestamp'] = date("Y-m-d H:i:s");
		$request['expireTime'] = date("Y-m-d H:i:s", time() + $this->config['timeout']); 
		$request['notifyUrl'] = $this->config['notify'];
		$request['tradeType'] = $trade_type;

		if ($other) {
			if (!isset($other[0])) {
				$other = array($other);
			}
			$request['originalAmount'] = $request['totalAmount'];
			$request['platformAmount'] = $request['totalAmount'];
			$request['subOrders'] = array();
			foreach ($other as $k => $v) {
				$subOrders = array();
				if (isset($v['amount']) && $v['amount']) {
					$subOrders['totalAmount'] = round($v['amount'] * 100, 2);
					$request['platformAmount'] = round($request['platformAmount'] - $subOrders['totalAmount'], 2);
				} elseif (isset($v['per']) && $v['per']) {
					# 此处为兼容历史业务代码
					$v['per'] = $v['per'] && $v['per'] >= 0 ? $v['per'] : 0;
					$v['per'] = $v['per']/100;
					$request['platformAmount'] = round($request['totalAmount'] * $v['per'], 2);
					$subOrders['totalAmount'] = round($request['totalAmount'] - $request['platformAmount'], 2);
					break;
				} else {
					continue;
				}
				$subOrders['mid'] = $v['mid'];
				$subOrders['merOrderId'] = $prefix . $v['order_id'];
				$request['subOrders'][] = $subOrders;

				$fenzhang = 0;
				if (isset($v['fenzhang']) && $v['fenzhang'] > 0) {
					$fenzhang = $v['fenzhang'];
				}
				if (isset($v['merchant_id']) && $v['merchant_id']) {
					Dever::load('pay/yspay/cash')->add($v['merchant_id'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id'], $fenzhang);
				}
				
			}
			if ($request['subOrders']) {
				$request['divisionFlag'] = true;
				if ($request['platformAmount'] > 0) {
					$merchant = Dever::load('pay/yspay/cash')->getMid($this->config['id']);
					if ($merchant) {
						$subOrders = array
						(
							'totalAmount' => $request['platformAmount'],
							'mid' => $merchant['mid'],
							'merOrderId' => $request['merOrderId'] . '_O',
						);
						$request['subOrders'][] = $subOrders;
						$request['platformAmount'] = 0;
						Dever::load('pay/yspay/cash')->add($merchant['id'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id']);
					}
				}
			}
		}
		if (isset($request['divisionFlag']) && $request['divisionFlag']) {
			
		} else {
			$merchant = Dever::load('pay/yspay/cash')->getMid($this->config['id']);
			if ($merchant) {
				$request['platformAmount'] = 0;
				Dever::load('pay/yspay/cash')->add($merchant['id'], $request['totalAmount'], $request['merOrderId'], $order_id);
			}
		}
		if (!$openid) {
			# 测试的openid
			$request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
		} else {
			$request['subOpenId'] = $openid;
		}
		
		if ($type == 2) {
			# 二维码支付
			$request['instMid'] = 'QRPAYDEFAULT';
			$request['billNo'] = $request['merOrderId'];
			unset($request['merOrderId']);
			$result = Base::get_pay_code($request, $this->config);

			if (isset($result['billQRCode'])) {
				$result['request'] = $request;
				$result['payMsg'] = $result['billQRCode'];
				if ($other) {
					$result['other'] = $other;
				}
				$this->updateOrderParam($order_id, $result);
				return $result['payMsg'];
			}
		} elseif ($type == 1) {
			# 小程序支付
			$result = Base::pay($request, $this->config);

			if (isset($result['miniPayRequest'])) {
				$result['request'] = $request;
				$result['payMsg'] = $result['miniPayRequest'];
				unset($result['miniPayRequest']);
				if ($other) {
					$result['other'] = $other;
				}
				$this->updateOrderParam($order_id, $result);
				return $result['payMsg'];
			}
		} elseif ($type == 4) {
			if ($refer) {
				$request['returnUrl'] = $refer;
			}

			$result = Base::h5_pay($request, $this->config);

			return $result;
		}
		return false;
	}

	# 退款
	public function refund($order_id, $cash, $order, $refund_order_id = false)
	{
		if (isset($this->config['prefix']) && $this->config['prefix']) {
			$request['merOrderId'] = $this->config['prefix'] . $order_id;
			if ($refund_order_id) {
				$request['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
			}
		} else {
			$request['merOrderId'] = $order_id;
			if ($refund_order_id) {
				$request['refundOrderId'] = $refund_order_id;
			}
		}
		$request['mid'] = $this->config['mid'];
		$request['tid'] = $this->config['tid'];
		$request['instMid'] = 'MINIDEFAULT';
		if (isset($order['param']['targetOrderId'])) {
			$request['targetOrderId'] = $order['param']['targetOrderId'];
		}
		
		$request['subAppId'] = $this->config['appid'];
		$request['requestTimestamp'] = date("Y-m-d H:i:s");
		$request['refundAmount'] = round($cash * 100, 2);
		
		if (isset($order['param']['other']) && $order['param']['other']) {
			$other = $order['param']['other'];
			if (!isset($other[0])) {
				$other = array($other);
			}
			$request['platformAmount'] = 0;
			$request['subOrders'] = array();
			foreach ($other as $k => $v) {
				$subOrders = array();
				if (isset($v['amount']) && $v['amount']) {
					$subOrders['totalAmount'] = round($v['amount'] * 100, 2);
				} elseif (isset($v['per']) && $v['per']) {
					$v['per'] = $v['per'] && $v['per'] >= 0 ? $v['per'] : 0;
					$v['per'] = $v['per']/100;
					$request['platformAmount'] = round($request['refundAmount'] * $v['per'], 2);
					$subOrders['totalAmount'] = round($request['totalAmount'] - $request['platformAmount'], 2);
				} else {
					continue;
				}
				
				$subOrders['mid'] = $v['mid'];
				if (isset($this->config['prefix']) && $this->config['prefix']) {
					$subOrders['merOrderId'] = $this->config['prefix'] . $v['order_id'];
					if ($refund_order_id) {
						$subOrders['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
					}
				} else {
					$subOrders['merOrderId'] = $v['order_id'];
					if ($refund_order_id) {
						$subOrders['refundOrderId'] = $refund_order_id;
					}
				}

				$request['subOrders'][] = $subOrders;
			}
		}

		$account = Dever::db('pay/account')->find($order['account_id']);
		$method = 'refund';
		if ($account && $account['system_source'] == 4) {
			$method = 'code_refund';
			$request['billNo'] = $request['merOrderId'];
			$request['instMid'] = 'QRPAYDEFAULT';
			$request['billDate'] = date('Y-m-d');
		}
		$result = Base::$method($request, $this->config);

		if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
			return true;
		}
		return false;
	}

	# 查询
	public function query($order_id)
	{
		$request['merOrderId'] = $order_id;
		$request['mid'] = $this->config['mchid'];
		$request['tid'] = $this->config['key'];
		$request['instMid'] = 'MINIDEFAULT';
		$request['requestTimestamp'] = date("Y-m-d H:i:s");
		
		$result = Base::refund($request, $this->config);

		return $result;
	}

	# 关闭订单
	public function close($order_id)
	{
		$request['merOrderId'] = $order_id;
		$request['mid'] = $this->config['mchid'];
		$request['tid'] = $this->config['key'];
		$request['instMid'] = 'MINIDEFAULT';
		$request['requestTimestamp'] = date("Y-m-d H:i:s");
		
		$result = Base::close($request, $this->config);

		return $result;
	}

	/**
	 * 获取二维码支付
	 */
	public function qrcode($order, $refer)
	{
		$result['type'] = 'qrcode';
		$result['url'] = $order;

		return $result;
	}

	/**
	 * 获取小程序支付
	 */
	public function applet($order)
	{
		$result = array();
		if (isset($order['package'])) {
			$prepay_id = str_replace('prepay_id=', '', $order['package']);
			$result['time'] = $order['timeStamp'];
			$result['nonce_str'] = $order['nonceStr'];
			$result['prepay_id'] = $prepay_id;
			$result['sign_type'] = $order['signType'];
			$result['sign'] = $order['paySign'];
			$result['type'] = 'applet';
		}
		return $result;
	}


	/**
	 * 获取页面支付 去收银台
	 */
	public function page($order, $refer)
	{
		return $order;
	}
}

class Base
{
	static $test_host = 'https://test-api-open.chinaums.com/';
	static $host = 'https://api-mop.chinaums.com/';
	static $signMethod = 'SHA256';  //签名方式

	# 获取token
	static $token_url = 'v1/token/access';

	# 微信下单支付
	static $pay_wechat_url = 'v1/netpay/wx/unified-order';

	# 支付宝下单支付
	static $pay_alipay_url = 'v1/netpay/trade/create';

	# 云闪付下单支付
	static $pay_uac_url = 'v1/netpay/uac/mini-order';

	# 交易查询
	static $query_url = 'v1/netpay/query';

	# 退款
	static $refund_url = 'v1/netpay/refund';

	# 退款查询
	static $refund_query_url = 'v1/netpay/refund-query';

	# 订单关闭
	static $close_url = 'v1/netpay/close';

	# 二维码支付
	static $qrcode_url = 'v1/netpay/bills/get-qrcode';

	# 二维码支付退款
	static $qrcode_refund_url = 'v1/netpay/bills/refund';

	# h5支付
	static $h5_url = 'v1/netpay/webpay/pay';

	# h5支付退款
	static $h5_refund_url = 'v1/netpay/wx/app-pre-order';


	//===================== 支付相关 ==============================
	/**
	 * 支付交易
	 */
	static public function pay($param, $config)
	{
		$url = self::$pay_wechat_url;
		$result = self::get($url, $param, $config);

		return $result;
	}

	/**
	 * h5支付
	 */
	static public function h5_pay($param, $config)
	{
		$url = self::$h5_url;
		$result = self::get($url, $param, $config);

		return $result;
	}

	/**
	 * 获取支付二维码
	 */
	static public function get_pay_code($param, $config)
	{
		$url = self::$qrcode_url;
		$result = self::get($url, $param, $config);

		return $result;
	}

	/**
	 * 二维码交易退款
	 */
	static public function code_refund($param, $config)
	{

		$url = self::$qrcode_refund_url;
		$result = self::get($url, $param, $config);

		return $result;
	}

	/**
	 * 支付撤销
	 */
	static public function close()
	{
		$url = self::$close_url;
		$result = self::get($url, $param, $config);

		return $result;

	}

	/**
	 * 交易退款
	 */
	static public function refund($param, $config)
	{

		$url = self::$refund_url;
		$result = self::get($url, $param, $config, false);

		return $result;
	}

	/**
	 * 交易查询
	 */
	static public function query()
	{
		$url = self::$query_url;
		$result = self::get($url, $param, $config);

		return $result;
	}

	/**
	 * 交易退款查询
	 */
	static public function refundQuery()
	{
		$url = self::$refund_query_url;
		$result = self::get($url, $param, $config);

		return $result;
	}

	//====================== 获取调用凭证===========================
	/**
	 * 通用调用凭证获取
	 * 默认使用token方式,使用签名方式需要传入参数
	 */
	static public function getAuth($type = 'token', $param, $config)
	{
		if ($type === 'token') {
			return self::getAccessTokenByToken($config);
		} elseif ($type === 'form') {
			return self::getAccessTokenByForm($param, $config);
		}
		return self::getAccessTokenBySig($param, $config);
	}

	/**
	 * 方式一，OPEN-ACCESS-TOKEN方式
	 */
	static function getAccessTokenByToken($config)
	{
		$param = [
			'appId' => $config['appid'],
			'timestamp' => date('YmdHis'),
			'nonce' => md5(uniqid(microtime(true),true)),
			'signMethod' => self::$signMethod,
		];
		$param['signature'] = self::signature($param, $config['appsecret']);

		$result = self::get(self::$token_url, $param, $config);

		if (isset($result['errCode']) && isset($result['accessToken'])) {
			return 'OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken'];
		}
		return '';
	}

	/**
	 * 方式二，OPEN-BODY-SIG方式
	 */
	static function getAccessTokenBySig($data, $config)
	{
		$param = [
			'AppId' => $config['appid'],
			'Timestamp' => date('YmdHis'),
			'Nonce' => md5(uniqid(microtime(true),true))
		];
		return 'OPEN-BODY-SIG AppId="'.$param['AppId'].'", Timestamp="'.$param['Timestamp'].'", Nonce="'.$param['Nonce'].'", Signature="'.self::signature2($data, $param, $config['appsecret']).'"';
	}

	/**
	 * 方式三，OPEN-FORM-PARAM方式
	 */
	static function getAccessTokenByForm($data, $config)
	{
		$content = Dever::json_encode($data);
		$param = [
			'appId' => $config['appid'],
			'timestamp' => date('YmdHis'),
			'nonce' => md5(uniqid(microtime(true),true)),
			'authorization' => 'OPEN-FORM-PARAM',
			'content' => $content,
		];
		$param['signature'] = urlencode(self::signature3($param, $config['appsecret']));

		$param['content'] = urlencode($param['content']);

		return $param;
	}

	/**
	 * 计算签名，方式一
	 */
	static function signature($param, $appsecret)
	{
		return bin2hex(hash(self::$signMethod, $param['appId'].$param['timestamp'].$param['nonce'].$appsecret, true));
	}

	/**
	 * 计算签名，方式二
	 */
	static function signature2($data, $param, $appsecret)
	{	
		$str = bin2hex(hash('sha256', Dever::json_encode($data), true));
		return base64_encode(hash_hmac('sha256', $param['AppId'].$param['Timestamp'].$param['Nonce'].$str, $appsecret, true));
	}

	/**
	 * 计算签名，方式三
	 */
	static function signature3($param, $appsecret)
	{	
		$str = bin2hex(hash('sha256', $param['content'], true));
		return base64_encode(hash_hmac('sha256', $param['appId'].$param['timestamp'].$param['nonce'].$str, $appsecret, true));
	}

	/**
	 * 获取信息
	 */
    static function get($url, $param, $config, $state = true)
    {
    	$url = $config['box'] == 1 ? self::$host . $url : self::$test_host . $url;

    	if (strstr($url, 'webpay')) {
    		$url .= '?';
    		$param = self::getAuth('form', $param, $config);
    		foreach ($param as $k => $v) {
    			$url .= '&' . $k . '=' . $v;
    		}
    		$url = str_replace('?&', '?', $url);
    		return $url;
    	} else {
    		$header = array();
    		$header['Authorization'] = self::getAuth('sig', $param, $config);
			$result = Dever::curl($url, $param, 'post', true, $header);
    	}

		if (strstr($result, '<html><head>')) {
			Dever::alert('系统错误');
		}
		$result = Dever::json_decode($result);
		if (isset($result['errCode'])) {
			if (!$state) {
				return $result;
			}
			if ($result['errCode'] == '0000' || $result['errCode'] == 'SUCCESS') {
				return $result;
			} elseif (isset($result['errInfo'])) {
				//Dever::alert('操作失败，请联系管理员');
				Dever::alert($result['errInfo']);
			} elseif (isset($result['errMsg'])) {
				//Dever::alert('操作失败，请联系管理员');
				Dever::alert($result['errMsg']);
			}
		} else {
			Dever::alert('系统错误');
		}

		return $result;
    }
}