<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */
defined('IN_IA') or exit('Access Denied');

load()->model('payment');
load()->model('account');
load()->func('communication');

$dos = array('save_setting', 'display', 'test_alipay', 'get_setting');
$do = in_array($do, $dos) ? $do : 'display';
permission_check_account_user('profile_pay_setting');
$_W['page']['title'] = '支付参数 - 公众号选项';

if ($do == 'get_setting') {
	$setting = uni_setting_load('payment', $_W['uniacid']);
	$pay_setting = $setting['payment'];
	if(!is_array($pay_setting) || empty($pay_setting)) {
		$pay_setting = array(
			'delivery' => array('switch' => false),
			'credit' => array('switch' => false),
			'alipay' => array('switch' => false),
			'wechat' => array('switch' => false),
			
			'unionpay' => array('switch' => false),
			'baifubao' => array('switch' => false),
			'line' => array('switch' => false),
			'jueqiymf' => array('switch' => false),
		);
	}
	iajax(0, $pay_setting, '');
}

if ($do == 'test_alipay') {
	$alipay = $_GPC['param'];
	$pay_data = array(
		'uniacid' => $_W['uniacid'],
		'acid' => $_W['acid'],
		'uniontid' => date('Ymd', time()).time(),
		'module' => 'system',
		'fee' => '0.01',
		'status' => 0,
		'card_fee' => 0.01
	);
	$params = array();
	$params['tid'] = md5(uniqid());
	$params['user'] = '测试用户';
	$params['fee'] = '0.01';
	$params['title'] = '测试支付接口';
	$params['uniontid'] = $pay_data['uniontid'];

	$result = alipay_build($params, $alipay);
	iajax(0, $result['url'], '');
}

if ($do == 'save_setting') {
	$type = $_GPC['type'];
	$param = $_GPC['param'];
	$setting = uni_setting_load('payment', $_W['uniacid']);
	$pay_setting = $setting['payment'];
	if ($type == 'credit' || $type == 'delivery' || $type == 'mix') {
		$param['switch'] = $param['switch'] == 'false' ? true : false;
	}
	if ($type == 'jueqiymf') {
		$param['switch'] = $param['switch'] == 'true' ? true : false;
	}
	
		if ($type == 'alipay' || $type == 'baifubao' || $type == 'line') {
			$param['switch'] = $param['switch'] == 'true' ? true : false;
		}
	
	

	if ($type == 'wechat') {
		$param['account'] = $_W['acid'];
		$param['signkey'] = $param['version'] == 2 ? trim($param['apikey']) : trim($param['signkey']);
	}

	if ($type == 'unionpay') {
		$unionpay = $_GPC['unionpay'];
		if ($unionpay['switch'] && empty($_FILES['unionpay']['tmp_name']['signcertpath']) && !file_exists(IA_ROOT . '/attachment/unionpay/PM_'.$_W['uniacid'].'_acp.pfx')) {
			itoast('请上联银商户私钥证书.', referer(), 'error');
		}
		$param = array(
			'switch' => $unionpay['switch'] == 'false'? false : true,
			'merid' => $unionpay['merid'],
			'signcertpwd' => $unionpay['signcertpwd']
		);
		if($param['switch'] && (empty($param['merid']) || empty($param['signcertpwd']))) {
			itoast('请输入完整的银联支付接口信息.', referer(), 'error');
		}
		if ($param['switch'] && empty($_FILES['unionpay']['tmp_name']['signcertpath']) && !file_exists(IA_ROOT . '/attachment/unionpay/PM_'.$_W['uniacid'].'_acp.pfx')) {
			itoast('请上传银联商户私钥证书.', referer(), 'error');
		}
		if ($param['switch'] && !empty($_FILES['unionpay']['tmp_name']['signcertpath'])) {
			load()->func('file');
			mkdirs(IA_ROOT . '/attachment/unionpay/');
			file_put_contents(IA_ROOT . '/attachment/unionpay/PM_'.$_W['uniacid'].'_acp.pfx', file_get_contents($_FILES['unionpay']['tmp_name']['signcertpath']));
			$public_rsa = '-----BEGIN CERTIFICATE-----
MIIEIDCCAwigAwIBAgIFEDRVM3AwDQYJKoZIhvcNAQEFBQAwITELMAkGA1UEBhMC
Q04xEjAQBgNVBAoTCUNGQ0EgT0NBMTAeFw0xNTEwMjcwOTA2MjlaFw0yMDEwMjIw
OTU4MjJaMIGWMQswCQYDVQQGEwJjbjESMBAGA1UEChMJQ0ZDQSBPQ0ExMRYwFAYD
VQQLEw1Mb2NhbCBSQSBPQ0ExMRQwEgYDVQQLEwtFbnRlcnByaXNlczFFMEMGA1UE
Aww8MDQxQDgzMTAwMDAwMDAwODMwNDBA5Lit5Zu96ZO26IGU6IKh5Lu95pyJ6ZmQ
5YWs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BO6Y/N6ViraBilcXjmWBJNE
MFF/yA==
-----END CERTIFICATE-----';
			file_put_contents(IA_ROOT . '/attachment/unionpay/UpopRsaCert.cer', trim($public_rsa));
		}
	}
	$pay_setting[$type] = $param;
	$payment = iserializer($pay_setting);
	uni_setting_save('payment', $payment);
	
	if ($type == 'unionpay') {
		header('LOCATION: '.url('profile/payment'));
		exit();
	}
	iajax(0, '');
}

if ($do == 'display') {
	$proxy_wechatpay_account = account_wechatpay_proxy();
	$setting = uni_setting_load('payment', $_W['uniacid']);
	$pay_setting = is_array($setting['payment']) ? $setting['payment'] : array();
	if (empty($pay_setting['delivery'])) {
		$pay_setting['delivery'] = array('switch' => false);
	}
	if (empty($pay_setting['mix'])) {
		$pay_setting['mix'] = array('switch' => false);
	}
	if (empty($pay_setting['credit'])) {
		$pay_setting['delivery'] = array('switch' => false);
	}
	if (empty($pay_setting['alipay'])) {
		$pay_setting['alipay'] = array('switch' => false);
	}
	if (empty($pay_setting['wechat'])) {
		$pay_setting['wechat'] = array('switch' => false);
	}
	if (empty($pay_setting['unionpay'])) {
		$pay_setting['unionpay'] = array('switch' => false);
	}
	if (empty($pay_setting['baifubao'])) {
		$pay_setting['baifubao'] = array('switch' => false);
	}
	if (empty($pay_setting['line'])) {
		$pay_setting['line'] = array('switch' => false);
	}
	if (empty($pay_setting['jueqiymf'])) {
		$pay_setting['jueqiymf'] = array('switch' => false);
	}
	
		if (empty($_W['isfounder'])) {
		$user_account_list = pdo_getall('uni_account_users', array('uid' => $_W['uid']), array(), 'uniacid');
		$param['uniacid'] = array_keys($user_account_list);
	}
	$accounts = array();
	$accounts[$_W['acid']] = array_elements(array('name', 'acid', 'key', 'secret', 'level'), $_W['account']);
	$pay_setting['unionpay']['signcertexists'] = file_exists(IA_ROOT . '/attachment/unionpay/PM_'.$_W['uniacid'].'_acp.pfx');
}
template('profile/payment');