event_pay_notify.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: notify_credit.php 29236 2012-03-30 05:34:47Z chenmengshu $
  7. */
  8. define('IN_API', true);
  9. define('CURSCRIPT', 'api');
  10. require '../../../source/class/class_core.php';
  11. require '../../../source/function/function_forum.php';
  12. include 'include/sms_func.php';
  13. $discuz = C::app();
  14. $discuz->init();
  15. $apitype = empty($_GET['attach']) || !preg_match('/^[a-z0-9]+$/i', $_GET['attach']) ? 'alipay' : $_GET['attach'];
  16. //$_G['siteurl'] = dhtmlspecialchars('http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(source\/plugin\/xj_event)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/');
  17. //$PHP_SELF = $_SERVER['PHP_SELF'];
  18. $_G['siteurl'] = str_replace('source/plugin/xj_event/','',$_G['siteurl']);
  19. if($apitype == 'alipay'){
  20. list($ec_contract, $ec_securitycode, $ec_partner, $ec_creditdirectpay) = explode("\t", authcode($_G['setting']['ec_contract'], 'DECODE', $_G['config']['security']['authkey']));
  21. define('DISCUZ_PARTNER', $ec_partner);
  22. define('DISCUZ_SECURITYCODE', $ec_securitycode);
  23. define('DISCUZ_DIRECTPAY', $ec_creditdirectpay);
  24. $notifydata = alipay_notifycheck();
  25. $orderid = $notifydata['order_no'];
  26. $tradeno = $notifydata['trade_no'];
  27. $trade_status = $notifydata['trade_status'];
  28. $notify_time = $_G['timestamp'];
  29. if($notifydata['trade_status'] == 'WAIT_BUYER_PAY'){ //等待支付
  30. $buyer_email = $_GET['buyer_email'];
  31. DB::query("UPDATE ".DB::table('xj_eventpay_log')." SET paystate=1, trade_status='$trade_status', tradeno='$tradeno', buyer_email='$buyer_email', notify_time=$notify_time WHERE orderid = '$orderid'");
  32. }elseif($notifydata['trade_status'] == 'WAIT_SELLER_SEND_GOODS'){ //买家已付款,等待发货
  33. $pay_time = $_G['timestamp'];
  34. DB::query("UPDATE ".DB::table('xj_eventpay_log')." SET paystate=2, trade_status='$trade_status', pay_time=$pay_time, notify_time=$notify_time WHERE orderid = '$orderid'");
  35. $item = DB::fetch_first("SELECT applyid,uid,tid FROM ".DB::table('xj_eventpay_log')." WHERE orderid = '$orderid'");
  36. $tid = $item['tid'];
  37. $uid = $item['uid'];
  38. DB::query("UPDATE ".DB::table('xj_eventapply')." SET pay_state=1 WHERE tid = $tid AND uid = $uid");
  39. }elseif($notifydata['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS'){
  40. DB::query("UPDATE ".DB::table('xj_eventpay_log')." SET trade_status='$trade_status', notify_time=$notify_time WHERE orderid = '$orderid'");
  41. }elseif($notifydata['trade_status'] == 'TRADE_FINISHED' || $notifydata['trade_status'] == 'TRADE_SUCCESS'){
  42. $item = DB::fetch_first("SELECT * FROM ".DB::table('xj_eventpay_log')." WHERE orderid = '$orderid'");
  43. if($item['paystate']<3){
  44. DB::query("UPDATE ".DB::table('xj_eventpay_log')." SET paystate=3, trade_status='$trade_status', notify_time=$notify_time WHERE orderid = '$orderid'");
  45. $item = DB::fetch_first("SELECT applyid,uid,tid FROM ".DB::table('xj_eventpay_log')." WHERE orderid = '$orderid'");
  46. $tid = $item['tid'];
  47. $uid = $item['uid'];
  48. DB::query("UPDATE ".DB::table('xj_eventapply')." SET pay_state=1,verify=1 WHERE tid = $tid AND uid = $uid");
  49. if(!$notifydata['location']){
  50. paysmssend($tid,$uid);
  51. }
  52. //邀请的奖励处理
  53. $items = DB::fetch(DB::query("SELECT A.*,B.authorid,B.subject FROM ".DB::table('xj_event')." A LEFT JOIN ".DB::table('forum_thread')." B ON A.tid = B.tid WHERE A.tid = $tid"));
  54. $setting = unserialize($items['setting']);
  55. if($setting['yqjl_jfs']>0){
  56. $apply = DB::fetch_first("SELECT * FROM ".DB::table('xj_eventapply')." WHERE applyid = ".$item['applyid']);
  57. if($apply['fromuid'] > 0){
  58. $applynumber = DB::result_first("SELECT SUM(applynumber) FROM ".DB::table('xj_eventapply')." WHERE tid=".$tid." AND uid=".$uid);
  59. $yqjl = array();
  60. $yqjl['tid'] = $tid;
  61. $yqjl['fromuid'] = $apply['fromuid'];
  62. $yqjl['applyuid'] = $uid;
  63. $yqjl['jfs'] = $setting['yqjl_jfs']*$applynumber;
  64. $yqjl['jflx'] = $setting['yqjl_jflx'];
  65. $yqjl['dateline'] = $_G['timestamp'];
  66. DB::insert('xj_event_yqjl_log',$yqjl);
  67. updatemembercount($yqjl['fromuid'],array($yqjl['jflx']=>+$yqjl['jfs']));
  68. $username = DB::result_first("SELECT username FROM ".DB::table('common_member')." WHERE uid=$uid");
  69. $eventtitle = DB::result_first("SELECT subject FROM ".DB::table('forum_thread')." WHERE tid = $tid");
  70. notification_add($yqjl['fromuid'],'system',$username.lang('plugin/xj_event','beiniyaoqinbaomincanjiale').' <a href="forum.php?mod=viewthread&tid='.$tid.'" target="_blank">'.$eventtitle.'</a> '.lang('plugin/xj_event','huodonghuode').$yqjl['jfs'].$_G['setting']['extcredits'][$yqjl['jflx']]['title'].lang('plugin/xj_event','jiangli'));
  71. }
  72. }
  73. //通知
  74. $username = DB::result_first("SELECT username FROM ".DB::table('common_member')." WHERE uid=$uid");
  75. if($_G['charset']=='gbk'){
  76. notification_add($items['authorid'], 'system', $username.diconv('报名参加了你的活动','UTF-8','GBK').' <a href="forum.php?mod=viewthread&tid='.$tid.'" target="_blank">'.$items['subject'].'</a> '.diconv('并已支付活动费用','UTF-8','GBK'),array(),0);
  77. }else{
  78. notification_add($items['authorid'], 'system', $username.'报名参加了你的活动 <a href="forum.php?mod=viewthread&tid='.$tid.'" target="_blank">'.$items['subject'].'</a> 并已支付活动费用',array(),0);
  79. }
  80. //微信消息
  81. loadcache('plugin');
  82. if($_G['cache']['plugin']['xj_wxmessage']['wxlogin']){
  83. require_once DISCUZ_ROOT . './source/plugin/xj_wxmessage/class/core.class.php';
  84. $xj_wxmessagecore = new xj_wxmessagecore();
  85. //调试记录开始
  86. $log_name="./alipay.txt";//log文件路径
  87. log_result($log_name,"【接收到的notify通知】:\r\n".$item['applyid'].'|'.$items['authorid']."\r\n【返回的信息】:\r\n");
  88. //调试记录结束
  89. $xj_wxmessagecore->send_eventmessage($uid,$tid,1);
  90. $xj_wxmessagecore->send_eventapplymessage($item['applyid'],$items['authorid'],2);
  91. }
  92. }
  93. }elseif($notifydata['trade_status'] == 'TRADE_CLOSED'){
  94. DB::query("UPDATE ".DB::table('xj_eventpay_log')." SET paystate=9, trade_status='$trade_status', notify_time=$notify_time WHERE orderid = '$orderid'");
  95. }
  96. }elseif($apitype == 'tenpay'){
  97. define('DISCUZ_PARTNER', $_G['setting']['ec_tenpay_bargainor']);
  98. define('DISCUZ_SECURITYCODE', $_G['setting']['ec_tenpay_key']);
  99. define('DISCUZ_AGENTID', '1204737401');
  100. define('DISCUZ_TENPAY_OPENTRANS_CHNID', $_G['setting']['ec_tenpay_opentrans_chnid']);
  101. define('DISCUZ_TENPAY_OPENTRANS_KEY', $_G['setting']['ec_tenpay_opentrans_key']);
  102. require '../../../api/trade/api_tenpay.php';
  103. $notifydata = tenpay_notifycheck();
  104. $orderid = $notifydata['order_no'];
  105. $tradeno = $notifydata['trade_no'];
  106. $trade_status = $notifydata['trade_status'];
  107. $notify_time = $_G['timestamp'];
  108. if($orderid){
  109. if($trade_status == 0){
  110. $trade_status = intval($trade_status);
  111. DB::query("UPDATE ".DB::table('xj_eventpay_log')." SET paystate=3, trade_status='$trade_status',tradeno=$tradeno,notify_time=$notify_time WHERE orderid = '$orderid'");
  112. $item = DB::fetch_first("SELECT applyid,uid,tid FROM ".DB::table('xj_eventpay_log')." WHERE orderid = '$orderid'");
  113. $tid = $item['tid'];
  114. $uid = $item['uid'];
  115. DB::query("UPDATE ".DB::table('xj_eventapply')." SET pay_state=1,verify=1 WHERE tid = $tid AND uid = $uid");
  116. paysmssend($tid,$uid);
  117. //邀请的奖励处理
  118. $items = DB::fetch_first("SELECT * FROM ".DB::table('xj_event')." WHERE tid = $tid");
  119. $setting = unserialize($items['setting']);
  120. if($setting['yqjl_jfs']>0){
  121. $apply = DB::fetch_first("SELECT * FROM ".DB::table('xj_eventapply')." WHERE applyid = ".$item['applyid']);
  122. $applynumber = DB::result_first("SELECT SUM(applynumber) FROM ".DB::table('xj_eventapply')." WHERE tid=".$tid." AND uid=".$uid);
  123. $yqjl = array();
  124. $yqjl['tid'] = $tid;
  125. $yqjl['fromuid'] = $apply['fromuid'];
  126. $yqjl['applyuid'] = $uid;
  127. $yqjl['jfs'] = $setting['yqjl_jfs']*$applynumber;
  128. $yqjl['jflx'] = $setting['yqjl_jflx'];
  129. $yqjl['dateline'] = $_G['timestamp'];
  130. DB::insert('xj_event_yqjl_log',$yqjl);
  131. updatemembercount($yqjl['fromuid'],array($yqjl['jflx']=>+$yqjl['jfs']));
  132. $username = DB::result_first("SELECT username FROM ".DB::table('common_member')." WHERE uid=$uid");
  133. $eventtitle = DB::result_first("SELECT subject FROM ".DB::table('forum_thread')." WHERE tid = $tid");
  134. notification_add($yqjl['fromuid'],'system',$username.lang('plugin/xj_event','beiniyaoqinbaomincanjiale').' <a href="forum.php?mod=viewthread&tid='.$tid.'" target="_blank">'.$eventtitle.'</a> '.lang('plugin/xj_event','huodonghuode').$yqjl['jfs'].$_G['setting']['extcredits'][$yqjl['jflx']]['title'].lang('plugin/xj_event','jiangli'));
  135. }
  136. }
  137. }
  138. }
  139. function paysmssend($tid,$uid){
  140. global $_G;
  141. //调试记录开始
  142. //$log_name="./alipay.txt";//log文件路径
  143. //log_result($log_name,"【接收到的notify通知】:\r\n".$xml."\r\n【返回的信息】:\r\n");
  144. //调试记录结束
  145. $apply = DB::fetch_first("SELECT * FROM ".DB::table('xj_eventapply')." WHERE first=1 AND tid=$tid AND uid=$uid");
  146. $thread = DB::fetch_first("SELECT authorid,userfield,setting,subject,starttime,event_address FROM ".DB::table('forum_thread')." A,".DB::table('xj_event')." B WHERE A.tid=$tid and A.tid = B.tid");
  147. $setting = unserialize($thread['setting']);
  148. $event_starttime = dgmdate($thread['starttime'],'dt');
  149. if($setting['seccode'] == 1){
  150. $message = cutstr($thread['subject'],30).'活动报名成功,人数:'.$apply['applynumber'].'人 验证码:'.$apply['seccode'].' 活动时间:'.$event_starttime;
  151. $sendtype = '报名验证码短信';
  152. if($_G[charset]=='gbk'){
  153. $message = diconv($message,'UTF-8','GBK');
  154. $sendtype = diconv($sendtype,'UTF-8','GBK');
  155. }
  156. sendsms_vcode($apply['mobile'],$thread['subject'],$apply['applynumber'],$apply['seccode']);
  157. //xjsendsms(array($apply['mobile']),$message,$sendtype);
  158. sendpm($apply['uid'],'',$message,$thread['authorid']);
  159. }elseif($setting['success_sms'] == 1){
  160. sendsms_success($apply['mobile'],$thread['subject'],$event_starttime);
  161. //易活动短信
  162. //$smsuid = DB::result_first("SELECT uid FROM ".DB::table('common_member')." WHERE username='".$setting['event_admin'][0]."'");
  163. //$smsmobile = DB::result_first("SELECT mobile FROM ".DB::table('common_member_profile')." WHERE uid=$smsuid");
  164. //sendsms_notice_yhd($apply['mobile'],$thread['subject'],$event_starttime,$thread['event_address'],$smsmobile);
  165. }
  166. }
  167. //调试记录开始
  168. /*
  169. $notifydatastr = print_r($notifydata,true)."-------------------notifydata \r\n";
  170. $notifydatastr = $notifydatastr.print_r($_GET,true)."---------GET \r\n";
  171. $notifydatastr = $notifydatastr.print_r($_POST,true)."---------POST \r\n\r\n\r\n";
  172. $notifydatastr = $notifydatastr.DISCUZ_PARTNER.'|'.DISCUZ_SECURITYCODE."---------常量 \r\n\r\n\r\n";;
  173. $filename = "./pay.txt";
  174. $fp = fopen("$filename", "a"); //打开文件指针,创建文件
  175. fwrite($fp, $notifydatastr);
  176. fclose($fp); //关闭指针
  177. */
  178. //调试记录结束
  179. if($notifydata['location']) {
  180. $url = rawurlencode('home.php?mod=spacecp&ac=credit');
  181. if($apitype == 'tenpay') {
  182. echo <<<EOS
  183. <meta name="TENCENT_ONLINE_PAYMENT" content="China TENCENT">
  184. <html>
  185. <body>
  186. <script language="javascript" type="text/javascript">
  187. window.location.href='$_G[siteurl]plugin.php?id=xj_event:event_pay&action=paysucceed';
  188. </script>
  189. </body>
  190. </html>
  191. EOS;
  192. } else {
  193. $orderid = $notifydata['order_no'];
  194. $item = DB::fetch_first("SELECT applyid,uid,tid FROM ".DB::table('xj_eventpay_log')." WHERE orderid = '$orderid'");
  195. $tid = $item['tid'];
  196. dheader('location: '.$_G['siteurl'].'plugin.php?id=xj_event:wsqcenter&mod=join_success&tid='.$tid);
  197. }
  198. } else {
  199. exit($notifydata['notify']);
  200. }
  201. function alipay_notifycheck() {
  202. global $_G;
  203. if(!empty($_POST)) {
  204. $notify = $_POST;
  205. $location = FALSE;
  206. } elseif(!empty($_GET)) {
  207. $notify = $_GET;
  208. $location = TRUE;
  209. } else {
  210. exit('Access Denied');
  211. }
  212. if(dfsockopen("http://notify.alipay.com/trade/notify_query.do?partner=".DISCUZ_PARTNER."&notify_id=".$notify['notify_id'], 60) !== 'true') {
  213. exit('Access Denied');
  214. }
  215. if(!DISCUZ_SECURITYCODE) {
  216. exit('Access Denied');
  217. }
  218. ksort($notify);
  219. $sign = '';
  220. foreach($notify as $key => $val) {
  221. if($key != 'sign' && $key != 'sign_type') $sign .= "&$key=$val";
  222. }
  223. if($notify['sign'] != md5(substr($sign,1).DISCUZ_SECURITYCODE)) {
  224. exit('Access Denied');
  225. }
  226. return array(
  227. 'order_no' => $notify['out_trade_no'],
  228. 'trade_no' => $notify['trade_no'],
  229. 'trade_status' => $notify['trade_status'],
  230. 'price' => $notify['total_fee'],
  231. 'notify' => 'success',
  232. 'location' => $location
  233. );
  234. /*
  235. if(($type == 'credit' || $type == 'invite') && (!DISCUZ_DIRECTPAY && $notify['notify_type'] == 'trade_status_sync' && ($notify['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || $notify['trade_status'] == 'TRADE_FINISHED') || DISCUZ_DIRECTPAY && ($notify['trade_status'] == 'TRADE_FINISHED' || $notify['trade_status'] == 'TRADE_SUCCESS'))
  236. || $type == 'trade' && $notify['notify_type'] == 'trade_status_sync') {
  237. return array(
  238. 'validator' => TRUE,
  239. 'status' => trade_getstatus(!empty($notify['refund_status']) ? $notify['refund_status'] : $notify['trade_status'], 1),
  240. 'order_no' => $notify['out_trade_no'],
  241. 'price' => !DISCUZ_DIRECTPAY && $notify['price'] ? $notify['price'] : $notify['total_fee'],
  242. 'trade_no' => $notify['trade_no'],
  243. 'notify' => 'success',
  244. 'location' => $location
  245. );
  246. } else {
  247. return array(
  248. 'validator' => FALSE,
  249. 'notify' => 'fail',
  250. 'location' => $location
  251. );
  252. }
  253. */
  254. }
  255. function tenpay_notifycheck() {
  256. global $_G;
  257. if(!DISCUZ_SECURITYCODE) {
  258. exit('Access Denied');
  259. }
  260. $resHandler = new ResponseHandler();
  261. $resHandler->setKey(DISCUZ_SECURITYCODE);
  262. $resHandler->setParameter("bankname", "");
  263. if($resHandler->isTenpaySign() && DISCUZ_PARTNER == $_GET['partner']) {
  264. return array(
  265. 'validator' => isset($_GET['trade_state']) ? !$_GET['trade_state'] : 0,
  266. 'trade_state' => $_GET['trade_state'],
  267. 'order_no' => $_GET['out_trade_no'],
  268. 'trade_no' => isset($_GET['transaction_id']) ? $_GET['transaction_id'] : '',
  269. 'price' => $_GET['total_fee'] / 100,
  270. 'bargainor_id' => $_GET['partner'],
  271. 'notify' => 'Success',
  272. 'location' => false,
  273. );
  274. }
  275. }
  276. // 打印log
  277. function log_result($file,$word)
  278. {
  279. $fp = fopen($file,"a");
  280. flock($fp, LOCK_EX) ;
  281. fwrite($fp,"执行日期:".strftime("%Y-%m-%d-%H:%M:%S",time())."\n".$word."\n\n");
  282. flock($fp, LOCK_UN);
  283. fclose($fp);
  284. }
  285. ?>