
/*=============================================================================================
	author:ZhaoJun
	version:1.2
	email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
'use strict';
/*-------------------------------------------------------------------------------------------*/


// 支付统一操作,所有单位为分
/*-------------------------------------------------------------------------------------------*/	
class index
{
	//body商品名
	//out_trade_no订单号
	//total_fee总金额
	//openid微信网页内支付时才需要传
	//notify_url接收微信支付异步通知回调地址，通知url必须为直接可访问的url，不能携带参数
	//return_url付款后返回的页面
	//quit_url支付宝付款中途退出返回商户网站的地址，微信不支持,只能前端判断
	//ip微信h5支付,微信app支付需传

	static async pay(obj,type='wx')
	{
		let ip=obj.ip
		let bizContent = 
		{
			subject:obj.body,
			out_trade_no:obj.out_trade_no,
			total_amount:(obj.total_fee/100).toFixed(2),
			notify_url:obj.notify_url,
			return_url:obj.return_url,
			quit_url:obj.quit_url,
			product_code: 'QUICK_MSECURITY_PAY',
			remark:obj.remark,
			payee_account:obj.account
		}
		let other={app_name:obj.app_name,package_name:obj.package_name,bundle_id:obj.bundle_id,wap_url:obj.wap_url,wap_name:obj.wap_name}
		obj={body:obj.body,out_trade_no:obj.out_trade_no,notify_url:obj.notify_url,openid:obj.openid,total_fee:obj.total_fee}
		let result
		switch (type) 
		{
			case 'ali':
				let urlParam= await $Sys.model('/tool/pay/ali','buildParams',[bizContent,'alipay.trade.wap.pay'])
				return 'https://openapi.alipay.com/gateway.do?'+urlParam
			break;
			case 'aliApp':
				return await $Sys.model('/tool/pay/ali','buildParams',[bizContent,'alipay.trade.app.pay'])
			break;
			case 'aliCode':
				result = await $Sys.model('/tool/pay/ali','code',[bizContent])
				return result.alipay_trade_precreate_response && result.alipay_trade_precreate_response.qr_code
			break;
			case 'wx':
				obj.spbill_create_ip=ip
				return await $Sys.model('/tool/pay/wx','buildParams',[obj])
			break;
			case 'wxAppIos':
				obj.spbill_create_ip=ip
				obj.trade_type='APP'
				obj.appid=obj.openid?obj.openid:$ToolPay.wx.appidApp
				return await $Sys.model('/tool/pay/wx','buildParams',[obj])
			break;
			case 'wxAppAnd':
				obj.spbill_create_ip=ip
				obj.trade_type='APP'
				obj.appid=obj.openid?obj.openid:$ToolPay.wx.appidApp
				return await $Sys.model('/tool/pay/wx','buildParams',[obj])
			break;
			case 'wxH5Web':
				obj.spbill_create_ip=ip
				obj.trade_type='MWEB'
				obj.scene_info={"h5_info":{"type":"Wap","wap_url": other.wap_url?other.wap_url:$ToolPay.wx.wap_url,wap_name:other.wap_name?other.wap_name:$ToolPay.wx.wap_name}}
				obj.scene_info=JSON.stringify(obj.scene_info)
				let obj1= await $Sys.model('/tool/pay/wx','buildParams',[obj])
				if(bizContent.return_url)obj1.mweb_url+='&redirect_url='+bizContent.return_url
				return obj1.mweb_url
			break;
			case 'wxH5And':
				obj.spbill_create_ip=ip
				obj.trade_type='MWEB'
				obj.scene_info={"h5_info":{"type":"Android","app_name": other.app_name?other.app_name:$ToolPay.wx.app_name,package_name:other.package_name?other.package_name:$ToolPay.wx.package_name}}
				obj.scene_info=JSON.stringify(obj.scene_info)
				let obj2= await $Sys.model('/tool/pay/wx','buildParams',[obj])
				return obj2.mweb_url
			break;
			case 'wxH5Ios':
				obj.spbill_create_ip=ip
				obj.trade_type='MWEB'
				obj.scene_info={"h5_info":{"type":"IOS","app_name": other.app_name?other.app_name:$ToolPay.wx.app_name,bundle_id:other.bundle_id?other.bundle_id:$ToolPay.wx.bundle_id}}
				obj.scene_info=JSON.stringify(obj.scene_info)
				let obj3= await $Sys.model('/tool/pay/wx','buildParams',[obj])
				return obj3.mweb_url
			break;
			case 'wxCode':
				obj.spbill_create_ip=ip
				obj.trade_type='NATIVE'
				let obj4= await $Sys.model('/tool/pay/wx','buildParams',[obj])
				return obj4.code_url
			break;
			case 'private':
				obj.spbill_create_ip=ip
				obj.trade_type='WEB'
				return await $Sys.model('/tool/pay/private','buildParams',[obj])
			break;
			case 'privateApp':
				obj.spbill_create_ip=ip
				obj.trade_type='APP'
				return await $Sys.model('/tool/pay/private','buildParams',[obj])
			break;
		}
	}

	//out_trade_no订单号
	//total_fee总金额，用于兼容微信
	//refund_fee退还金额
	//notify_url接收微信支付异步通知回调地址，通知url必须为直接可访问的url，不能携带参数
	//refund_desc退款原因，支付宝传中文时会有bug，稍后解决
	static async back(obj,type='wx')
	{
		let result=
		{
			out_trade_no:obj.out_trade_no,
			refund_amount:(obj.refund_fee/100).toFixed(2),
			refund_reason:obj.refund_desc,
			notify_url:obj.notify_url
		}
		let re={}
		switch (type) 
		{
			case 'ali':
				re=await $Sys.model('/tool/pay/ali','back',[result])
				return {result:re.alipay_trade_refund_response && re.alipay_trade_refund_response.code=='10000',detail:re}
			break;
			case 'wx':
				obj.out_refund_no=obj.out_trade_no+'_back'
				re=await $Sys.model('/tool/pay/wx','back',[obj])
				return {result:re.result_code=='SUCCESS',detail:re}
			break;
			case 'private':
				obj.out_refund_no=obj.out_trade_no+'_back'
				re=await $Sys.model('/tool/pay/private','back',[obj])
				return {result:re.result_code=='SUCCESS',detail:re}
			break;
		}
	}

	static async verify(obj,type='wx')
	{
		if(obj.version)type='ali'
		else if(obj.private)type="private"
		else type='wx'

		let tag,statusTag
		switch (type) 
		{
			case 'ali':
				tag=await $Sys.model('/tool/pay/ali','verifySign',[obj])
				if(!tag)$Sys.err({message:'验证签名失败'})
				statusTag=obj.trade_status && obj.trade_status.indexOf('SUCCESS')!=-1
				return {type:'ali',result:statusTag,out_trade_no:obj.out_trade_no,back:index.success('ali')}
			break;
			case 'wx':
				obj=$Sys.model('/tool/wx/xml','getXml',[obj]) 
				tag=await $Sys.model('/tool/pay/wx','verifySign',[obj])
				if(!tag)$Sys.err({message:'验证签名失败'})
				statusTag=obj.result_code && obj.result_code.indexOf('SUCCESS')!=-1
				return {type:'wx',result:statusTag,out_trade_no:obj.out_trade_no,back:index.success('wx')}
			break;
			case 'private':
				tag=await $Sys.model('/tool/pay/wx','verifySign',[obj])
				if(!tag)$Sys.err({message:'验证签名失败'})
				statusTag=obj.result_code && obj.result_code.indexOf('SUCCESS')!=-1
				return {type:'private',result:statusTag,out_trade_no:obj.out_trade_no,back:index.success('wx')}
			break;
		}
	}

	//out_trade_no订单号
	static async query(obj,type='wx')
	{
		let result
		switch (type) 
		{
			case 'ali':
				result= await $Sys.model('/tool/pay/ali','query',[obj])
				return {type:'ali',result:result.alipay_trade_query_response && result.alipay_trade_query_response.code==10000,...obj}
			break;
			case 'aliTran':
				result= await $Sys.model('/tool/pay/ali','query',[{out_biz_no:obj.out_trade_no},'alipay.fund.trans.order.query'])
				return {type:'ali',result:result.alipay_fund_trans_order_query_response && result.alipay_fund_trans_order_query_response.code==10000,...obj}
			break;
			case 'wx':
				result= await $Sys.model('/tool/pay/wx','query',[obj])
				return {type:'wx',result:result.return_code=='SUCCESS',out_trade_no:obj.out_trade_no}
			break;
			case 'wxTran':
				result= await $Sys.model('/tool/pay/wx','query',[{partner_trade_no:obj.out_trade_no},'/mmpaymkttransfers/gettransferinfo'])
				return {type:'wx',result:result.return_code=='SUCCESS',out_trade_no:obj.out_trade_no}
			break;
			case 'private':
				result= await $Sys.model('/tool/pay/private','query',[obj])
				return {type:'wx',result:result.return_code=='SUCCESS',out_trade_no:obj.out_trade_no}
			break;
		}
	}

	//remark,备注，当付款方为企业账户，且转账金额达到（大于等于）50000元，remark不能为空(微信必填)
	//account,收款方账户,微信为openid
	//amount,金额
	//out_trade_no,账单号
	//name，对方真实姓名，如果填写必须完全正确
	static async tran(obj,type)
	{
		let result
		switch (type) 
		{
			case 'ali':
				result= await $Sys.model('/tool/pay/ali','tran',[{remark:obj.remark,payee_real_name:obj.name,payee_account:obj.account,amount:obj.amount,out_biz_no:obj.out_trade_no}])
				return {type:'ali',result:result.alipay_fund_trans_toaccount_transfer_response && result.alipay_fund_trans_toaccount_transfer_response.code==10000,...obj}
			break;
			case 'wx':
				result= await $Sys.model('/tool/pay/wx','tran',[{desc:obj.remark,amount:obj.remark,re_user_name:obj.name,openid:obj.account,partner_trade_no:obj.out_trade_no}])
				return {type:'wx',result:result.return_code=='SUCCESS',out_trade_no:obj.out_trade_no}
			break;
			case 'private':
				result= await $Sys.model('/tool/pay/private','tran',[{desc:obj.remark,amount:obj.remark,re_user_name:obj.name,openid:obj.account,partner_trade_no:obj.out_trade_no}])
				return {type:'wx',result:result.return_code=='SUCCESS',out_trade_no:obj.out_trade_no}
			break;
		}
	}

	static success(type='wx')
	{
		switch (type) 
		{
			case 'ali':
				return $Sys.model('/tool/pay/ali','success',[])
			break;
			case 'wx':
				return $Sys.model('/tool/pay/wx','success',[])
			break;
			case 'private':
				return $Sys.model('/tool/pay/private','success',[])
			break;
		}
	}
}
/*-------------------------------------------------------------------------------------------*/    
module.exports=index;