<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\advertisement\public_class;
use \plugin\advertisement\init;
use temmoku\db;
class payment{
	
	public function run($total_amount,$trade_no,$data){
		$row=db::select('*')->from(init::$table.'pay_log')->where("transaction_code= '{$data['transaction_code']}' ")->row();
		
		$need_rmb=$row['rmb'] - ( $row['integral']/C('advertisement|setting.integral') );
		
		//如果金额不对直接抛弃.
		if($total_amount != $need_rmb){
			return;
		}
		//防止异步通知
		if($row['state']){
			return;
		}
		// 改变支付状态
		$array=[
			'state'=>1,
			'trade_no'=>$trade_no
		];
		$this->order($row);
	}
	
	public function order(&$row,&$array){
		db::update(init::$table.'pay_log')->cols($array)->where("id='{$row['id']}'")->query();
		$data=C(init::$dir.'|ad_list.'.$row['tag']);
		$data=\unserialize($data);
		$data['price']=\unserialize($data['price']);
		
		$time=$data['price']['time'][$row['buytime']];
		$endtime=NOWTIME+(3600 * 24 * $time);
		$_array=[
			'bid'=>$data['id'],
			'ad_type'=>$data['ad_type'],
			'btime'=>$time,
			'uid'=>$row['uid'],
			'endtime'=>$endtime,
			'posttime'=>NOWTIME
		];
		$id=db::insert(init::$table.'buy_list')->cols($_array)->query();
		return $id;
	}
	
}
?>