<?php

/*
  PHP version 5
  Copyright (c) 2002-2019ECISP.CN、EarcLink.COM
  警告：这不是一个免费的软件，请在许可范围内使用，请尊重知识产权，侵权必究，举报有奖
  作者：黄祥云 E-mail:6326420@qq.com  QQ:6326420 TEL:18665655030 / 13797552220
*/
namespace modul_application;
use application\ESPCMS_Core;
use modul_application\ESPCMS_MemberDB;
use modul_application\ESPCMS_MemberAuthority;
use modul_application\ESPCMS_SendMessage;
use modul_application\ESPCMS_OrderDB;
class ESPCMS_Orderasyn {
	public static function AsyncUpdate($paycode = false, $requery_code = false, $xmldb = false) {
		global $espcms_command;
		if (!$paycode) {
			exit();
		}
		if (!is_array($requery_code)) {
			exit();
		}
		$pay_file = ESPCMS_FILE_ROOT . 'espcms_includes/payment/' . $paycode . '.php';
		if (!is_file($pay_file)) {
			exit();
		}
		$pay_plug_read = ESPCMS_OrderDB::get_pay_plug_code_view($paycode);
		$pay_argument = $pay_plug_read['pluglist'] ? unserialize($pay_plug_read['pluglist']) : false;
		if (!$pay_argument || !is_array($pay_argument)) {
			exit();
		}
		include_once $pay_file;
		$payobj = new $paycode();
		$order_read = $payobj->respond($requery_code, $pay_argument, $xmldb);
		if (!$order_read['paysn'] || !espcms_ismatches($order_read['paysn']) || !$order_read['opid'] || !espcms_ismatches($order_read['opid'])) {
			exit();
		}
		$order_read_db = ESPCMS_OrderDB::get_order_view(false, $order_read['ordersn']);
		if (!$order_read_db || !is_array($order_read_db)) {
			exit();
		}
		$order_read['userid'] = $order_read_db['userid'];
		$order_app_config = ESPCMS_Core::get_app_config('order', false);
		if (!$order_app_config['isetup'] || !$order_app_config['isopen']) {
			exit();
		}
		$order_con = unserialize($order_app_config['appconfig']);
		if ($order_read_db['orderamount'] != $order_read['payamount']) {
			exit();
		}
		if (!$order_read_db['ispay']) {
			$oprid = ESPCMS_OrderDB::order_install_order_payreceipt($order_read_db['oid'], $order_read);
			if (!$oprid) {
				exit();
			}
			$order_update_type['opid'] = espcms_ismatches($order_read['opid']) && $order_read['opid'] ? $order_read['opid'] : 0;
			$order_update_type['ordertype'] = 2;
			$order_update_type['islock'] = 1;
			$order_update_type['ispay'] = 1;
			$order_update_type['paysn'] = $order_read['paysn'];
			$order_update_type['opid'] = espcms_ismatches($order_read['opid']) && $order_read['opid'] ? $order_read['opid'] : 0;
			$order_update_type['paytime'] = time();
			$oid = ESPCMS_OrderDB::order_update_type($order_read_db['oid'], $order_update_type);
			if (!$oid) {
				exit();
			}
			$member_app_config = ESPCMS_Core::get_app_config('member', false);
			if ($member_app_config['isetup'] && $member_app_config['isopen'] && $order_read_db['userid'] && espcms_ismatches($order_read_db['userid'])) {
				ESPCMS_MemberDB::member_integral_DB($order_read_db['userid'], $order_read_db['integral']);
				ESPCMS_MemberDB::install_member_integral($order_read_db['userid'], $order_read_db['integral'], 1, $order_read_db['ordersn']);
				$member_cookie_info = ESPCMS_MemberAuthority::getMemberInfo();
			}
			if ($espcms_command['EMAIL_ISOPEN'] && $order_con['ORDER_ISEMAIL']) {
				$order_read_db['addtime'] = espcms_timeformat($order_read_db['addtime'], 3);
				if ($member_cookie_info['isemail'] && espcms_isemail($member_cookie_info['email'])) {
					ESPCMS_SendMessage::send_email_key_message('order_pay_ok', $member_cookie_info['email'], $order_read_db);
				}
				ESPCMS_SendMessage::send_email_key_message('order_system_pay_ok', $espcms_command['ADMINE_MAIL'], $order_read_db);
			}
			if ($espcms_command['SMS_ISOPEN'] && $order_con['ORDER_ISMOBLIE']) {
				$order_read_db['addtime'] = espcms_timeformat($order_read_db['addtime'], 3);
				if ($member_cookie_info['ismoblie'] && espcms_ismoblie($member_cookie_info['mobile'])) {
					ESPCMS_SendMessage::send_moblie_sms_message('order_pay_ok', $member_cookie_info['mobile'], $order_read_db);
				}
				ESPCMS_SendMessage::send_moblie_sms_message('order_system_pay_ok', $espcms_command['SMS_MOBLIE_NB'], $order_read_db);
			}
		}
		exit($order_read['out_str']);
	}
}
