<?php
/**
*订单中心
*/
class orderAction extends baseAction
{
	/**
	  * @method 生成支付订单
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  por_guid  string [必填] 32位长度的唯一编码GUID,
	  * @param  por_u_id  int [必填]宜配会员ID,
	  * @param  por_from  string [可选] YP(宜配网)|YPAPP|YPGZG|YPIM|YPYPC|PJD(跑街的)|PJDAPP|PJDZS(跑街的助手)|PJDGZH|PJDSGZH(商城店铺公众号)|PJL(跑街令)|CRM|BMTSR|CLIENT(客户网)
	  * @param  por_pay_method  string [可选]weixin微信(默认) alipay支付宝
	  * @param  por_pay_type  string [可选]pc:扫码(默认)  app:APP  wap:手机网 gzh:公众号
	  * @param  por_orderids  string [可选]来源的订单ID,
	  * @param  por_money  float [必填]'10.56',
	  * @param  por_title  string [必填]'宜配网-积分充值订单：121245',
	  * @param  por_back_url  string [可选]成功后的回调链接,
	  * @param  por_wx_openid  string [可选]gzh:公众号支付需要提供,											
	  * @author soul
	  * @copyright 2017/8/16
	  * @return                       {"status":true,"data":{"por_u_id":149894,"por_from":"\u4e3b\u7ad9","por_orderids":"127233","por_pay_type":"weixin","por_money":"100.00","por_des":"\u5b9c\u914d\u7f51-\u79ef\u5206\u5145\u503c\u8ba2\u5355\uff1a127233","por_atime":1502884544,"por_id":"200000","err_code":"SUCCESS","err_msg":"","por_return_data":"http:\/\/paysdk.weixin.qq.com\/example\/qrcode.php?data=weixin%3A%2F%2Fwxpay%2Fbizpayurl%3Fpr%3DUtaDMlm"},"code":0}
	  */
	function buildPay()
	{
		$MainOrder = new MainOrder();
		$res = $MainOrder->buildPay($_POST);
		echo json_encode($res);
	}

	/**
	  * @method 通过支付单号获取支付页面信息
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  por_id
	  * @author soul
	  * @copyright 2017/8/16
	  * @return 
	  */
	function getPayPage()
	{
		$MainOrder = new MainOrder();
		$res = $MainOrder->getPayPage($_GET['por_id']);
		echo json_encode($res);
	}


	/**
	  * @method 检查支付订单状态
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  por_id || por_guid int 二选一
	  * @author soul
	  * @copyright 2017/8/16
	  * @return 
	  */
	function checkPay()
	{
		$MainOrder = new MainOrder();
		if(isset($_POST['por_guid']))
		{
			$res = $MainOrder->checkPay($_POST['por_guid'], 'por_guid');
		}
		else
		{
			$res = $MainOrder->checkPay($_POST['por_id']);
		}
		echo json_encode($res);
	}

	/**
	  * @method 关闭支付订单
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  por_id             int|array
	  * @author soul
	  * @copyright 2017/8/16
	  * @return 
	  */
	function closePay()
	{
		$MainOrder = new MainOrder();
		$res = $MainOrder->closePay($_POST['por_id']);
		echo json_encode($res);
	}


	/**
	  * @method 获取支付订单的信息
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  por_id             int|array  [必填] 111, [111,2222,3333....]
	  * @param  style              string     [可选] base默认 | all
	  * @author soul
	  * @copyright 2017/8/16
	  * @return 
	  */
	function getPayInfo()
	{
		$arr = [];
		if(empty($_POST['por_id']))
		{
			LibFc::ReturnJson(false, 'por_id参数必填');
			exit;
		}
		$one = true;
		$porIdArr = [];
		if(is_array($_POST['por_id']))
		{
			$one = false;
			foreach($_POST['por_id'] as $v)
			{
				$porIdArr[] = (int) $v;
			}
			$porIdArr = array_unique($porIdArr);
		}
		else
		{
			$porIdArr = [(int) $_POST['por_id']];
		}
		$filed = !empty($_POST['style']) && $_POST['style'] == 'all'?  ['*']: ['por_id', 'por_status', 'por_trade_no','por_pay_time', 'por_transaction_id', 'por_money', 'por_orderids', 'por_pay_method', 'por_pay_type'];
		$where = sprintf(" AND por_id IN (%s)", implode(',', $porIdArr));
		$MainBase = new MainBase();
		$res = $MainBase->get('pa_order', $filed, $where, $one);
		if(!$one)
		{
			foreach($res['data'] as $v)
			{
				$arr[$v['por_id']] = $v;
			}
		}
		else
		{
			$arr = $res['data'];
		}
		LibFc::ReturnJson(true, $arr);
	}



	/**
	  * @method 对支付订单申请退款
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  data               array  [必填] [
													'pre_guid'=>32位长度的唯一编码GUID 20171101182820069186096191406248  || CCF1D138D0262E2DBD1A01F187C4965F,【必填】
													'pre_por_id'=>宜配支付中心订单ID,【必填】
													'pre_refund_money'=>1.02,【必填】
													'pre_des'=>''
												];
	  * @author soul
	  * @copyright 2017/8/16
	  * @return 
		{"status":true,"data":退款单ID}
		{"status":false,"data":'失败原因',"code":0}
	  */
	function refund()
	{
		$filedArr = ['pre_guid', 'pre_por_id', 'pre_refund_money', 'pre_des'];
		foreach($filedArr as $filed)
		{
			if(isset($_POST[$filed]))
			{
				$dataArr[$filed] = trim($_POST[$filed]);
			}
		}
		$MainOrder = new MainOrder();
		$res = $MainOrder->refund($dataArr);
		echo json_encode($res);
	}


	/**
	  * @method 查询退款
	  * @param  pre_id || pre_guid int 二选一
	  * @author soul
	  * @copyright 2017/8/16
	  * @return
			成功：['status'=>true, 'data'=>['pre_id'=>xxxx, 'pre_por_id'=>'', 'pre_status'=>''....]]
			失败：['status'=>false, 'data'=>'xxx', 'code'=>'xxx']
	  */
	function getRefund()
	{
		$MainOrder = new MainOrder();
		if(!empty($_POST['pre_id']))
		{
			$res = $MainOrder->getRefund($_POST['pre_id']);
		}
		else
		{
			$res = $MainOrder->getRefund($_POST['pre_guid'], 'pre_guid');
		}
		echo json_encode($res);
	}


	/**
	  * @method 企业付款
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  dataArr            array  [必填] 1、已有订单ID['pmc_id'=>xxx, .....]
										            2、需要新建 [
														'pmc_guid'=>32位长度的唯一编码GUID 20171101182820069186096191406248  || CCF1D138D0262E2DBD1A01F187C4965F,【必填】
														'pmc_u_id'=>宜配会员ID,
														'pmc_from'=>'主站'|'B2C',
														'pmc_to_openid'=>收款账号的唯一码
														'pmc_to_name'=>收款人姓名,
														'pmc_to_idcard'=>收款人身份证,
														'pmc_pay_by'=>'weixin',
														'pmc_money'=>'10.56',
														'pmc_des'=>'提现订单：121245'
													];
	  * @author soul
	  * @copyright 2017/8/16
	  * @return 
		{"status":true,"data":,"code":0}
		{"status":false,"data":'失败原因',"code":0}
	  */
	function mchPay()
	{
		$filedArr = ['pmc_id', 'pmc_guid', 'pmc_u_id', 'pmc_from', 'pmc_to_openid', 'pmc_to_name', 'pmc_to_idcard', 'pmc_pay_by', 'pmc_money', 'pmc_des'];
		foreach($filedArr as $filed)
		{
			if(isset($_POST[$filed]))
			{
				$dataArr[$filed] = trim($_POST[$filed]);
			}
		}
		$MainOrder = new MainOrder();
		$res = $MainOrder->mchPay($dataArr);
		echo json_encode($res);
	}

	/**
	  * @method 查询企业付款
	  * @param  pmc_id || pmc_guid int 二选一
	  * @author soul
	  * @copyright 2017/8/16
	  * @return
			成功：['status'=>true, 'data'=>['pmc_id'=>xxxx, 'pmc_status'=>''....]]
			失败：['status'=>false, 'data'=>'xxx', 'code'=>'xxx']
	  */
	function getMchPay()
	{
		$MainOrder = new MainOrder();
		if(!empty($_POST['pmc_id']))
		{
			$res = $MainOrder->getMchPay($_POST['pmc_id']);
		}
		else
		{
			$res = $MainOrder->getMchPay($_POST['pmc_guid'], 'pmc_guid');
		}
		echo json_encode($res);
	}
}