<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
if(empty($_G['uid'])){
	showmessage('to_login', '', array(), array('login' => true));
}

$config = $_G['cache']['plugin']['jsjpay'];
$jflx = $_G['setting']['extcredits'][$config['jifen']]['title'];
$apiid = intval($config['apiid']);
$apikey = addslashes($config['apikey']);
$kami = addslashes(trim($_POST['kami']));
$total = number_format($_POST['price'],2,".","");
$credit = intval($_POST['price']*$config['bili']);

if(!$apikey || !$apiid){
	showmessage(lang('plugin/jsjpay','p1'));exit;
}
if(!$total || !$kami || $total<1){
	showmessage(lang('plugin/jsjpay','error'));exit;
}
	//以下即将在平台查询卡密信息

	$chakey = md5($apikey.$kami);
	$array = array('kami' => $kami, 'apiid' => $apiid, 'total' => $total, 'chakey' => $chakey);
	$ch = curl_init ();
	curl_setopt ( $ch, CURLOPT_URL, "https://yun.jsjapp.com/k/q_k.php" );
	curl_setopt ( $ch, CURLOPT_POST, 1 );
	curl_setopt ( $ch, CURLOPT_HEADER, 0 );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $array );
	curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	$run = curl_exec ( $ch );
	curl_close ( $ch );
	
	$run = trim($run, "\xEF\xBB\xBF");

	if($run=="9"){
		showmessage(lang('plugin/jsjpay','kamie9'));exit;
	}else if($run=="1"){

		$data = array();
		$data['uid'] = $_G['uid'];
		$data['op'] = '1';
		$data['addnum'] = $kami;
		$data['total'] = $total;
		$data['time'] = intval($_G['timestamp']);
		C::t('#jsjpay#jsjpay')->insert($data);
		$msg = array('total'=>$total,'jifen'=>$credit,'jflx'=>$jflx);
		notification_add($_G['uid'], 'system', lang('plugin/jsjpay','msg'), $msg, $system = 1);
		updatemembercount($_G['uid'] , array($config['jifen'] => +"$credit"), 1, 'AFD', $_G['uid']);

		if($_G['mobile']){
			showmessage(lang('plugin/jsjpay','n1'),'plugin.php?id=jsjpay:m&act=list', array(), array('alert' => 'right'));
		}else{
			showmessage(lang('plugin/jsjpay','n1'),'home.php?mod=spacecp&ac=plugin&op=credit&id=jsjpay:log', array(), array('alert' => 'right'));
		}
	}else{
		showmessage(lang('plugin/jsjpay','kamie404'));exit;
	}

?>