event_pay.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * [超级活动(xj_event.{modulename})] (C)2012-2099 Powered by 逍遥工作室.
  4. * Version: 1.0
  5. * Date: 2012-9-15 10:27
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. if(strpos($_SERVER["HTTP_USER_AGENT"],'Appbyme')>0){
  11. $Appbyme = true;
  12. }
  13. if(strpos($_SERVER["HTTP_USER_AGENT"],'MAGAPP')>0){
  14. $magapp = true;
  15. }
  16. if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')>0){
  17. $isWeiXin = true;
  18. }
  19. if(strpos($_SERVER['HTTP_USER_AGENT'], 'QianFan')>0){
  20. $QianFan = true;
  21. }
  22. if(!$_G['uid']) {
  23. showmessage('not_loggedin', NULL, array(), array('login' => 1));
  24. }
  25. //调用核心类
  26. include 'source/plugin/xj_event/include/core.class.php';
  27. $eventcore = new xj_eventcore();
  28. //0元支付处理
  29. $tid = intval($_GET['tid']);
  30. $youhuiprice = $eventcore->GetYouHui($tid,$_G['uid']); //优惠计算
  31. $totalprice = $eventcore->GetEventPrice($tid,$_G['uid']); //总价格
  32. //判断APP优惠
  33. if($Appbyme || $magapp){
  34. $items = DB::fetch_first("SELECT * FROM ".DB::table('xj_event')." WHERE tid = $tid");
  35. $setting = unserialize($items['setting']);
  36. if($setting['app_benefit']>0){
  37. $youhuiprice = $youhuiprice + $setting['app_benefit'];
  38. }
  39. }
  40. if(($totalprice-$youhuiprice)<=0 && $tid>0){
  41. $applyid = DB::result_first("SELECT applyid FROM ".DB::table('xj_eventapply')." WHERE tid = '$tid' and uid=".$_G['uid']);
  42. DB::update('xj_eventapply',array('verify'=>1,'pay_state'=>1),"applyid=$applyid");
  43. showmessage(lang('plugin/xj_event','baomingchenggong'),$_G['siteurl']."plugin.php?id=xj_event:wsqcenter&mod=event_view&tid=$tid");
  44. }
  45. if($_GET['action'] == 'paysucceed'){
  46. showmessage(lang('plugin/xj_event', 'hdfyzfwc'),'plugin.php?id=xj_event:event_center');
  47. }
  48. //如果是微信支付跳转
  49. if($_GET['bank_type'] == 'wxpay'){
  50. header('Location: plugin.php?id=xj_event:wxpay&applyid='.intval($_GET['applyid']));
  51. exit();
  52. }
  53. //如果是积分支付跳转
  54. if($_GET['bank_type'] == 'jfpay'){
  55. header('Location: plugin.php?id=xj_event:jfpay&applyid='.intval($_GET['applyid']));
  56. exit();
  57. }
  58. //如果是马甲支付跳转
  59. /*
  60. if($magapp){
  61. $tid = intval($_GET['tid']);
  62. $item = DB::fetch_first("SELECT applyid,applynumber FROM ".DB::table('xj_eventapply')." WHERE tid = '$tid' and uid=".$_G['uid']);
  63. header('Location: plugin.php?id=xj_event:magapp_pay&applyid='.$item['applyid']);
  64. exit();
  65. }
  66. */
  67. //微信支付的配置
  68. if(file_exists($xj_event_wxset = DISCUZ_ROOT.'./data/sysdata/cache_xj_event_wxset.php')) {
  69. @include $xj_event_wxset;
  70. }
  71. if($_GET['bank_type'] === '0'){
  72. $_GET['bank_type'] = 'tenpay';
  73. }
  74. if(!$_GET['bank_type']){
  75. $tid = intval($_GET['tid']);
  76. $items = DB::fetch_first("SELECT A.*,B.subject FROM ".DB::table('xj_event')." A,".DB::table('forum_thread')." B WHERE A.tid = '$tid' and A.tid=B.tid");
  77. $eventnumber = $items['event_number'];
  78. $setting = unserialize($items['setting']);
  79. $subject = $items['subject'];
  80. $use_cost = $items['use_cost'];
  81. $item = DB::fetch_first("SELECT applyid,applynumber FROM ".DB::table('xj_eventapply')." WHERE tid = '$tid' and uid=".$_G['uid']);
  82. //报名方式不同,计算人数
  83. if($setting['nodaibaoming']){
  84. $applynumber = $item['applynumber'];
  85. }else{
  86. $applynumber = DB::result_first("SELECT count(*) FROM ".DB::table('xj_eventapply')." WHERE tid = '$tid' and uid=".$_G['uid']);
  87. }
  88. //判断报名名额是否够,不够就不让支付
  89. if($eventnumber>0){
  90. $applycountnumber = DB::result_first("SELECT SUM(applynumber) FROM ".DB::table('xj_eventapply')." WHERE tid='$tid' and verify=1");
  91. if($applynumber > ($eventnumber-$applycountnumber)){
  92. showmessage(lang('plugin/xj_event','baomrsym'));
  93. exit();
  94. }
  95. }
  96. $totalprice = $use_cost*$applynumber;
  97. $applyid = $item['applyid'];
  98. $apply = DB::fetch_first("SELECT * FROM ".DB::table('xj_eventapply')." WHERE first=1 AND tid = '$tid' AND uid=".$_G['uid']);
  99. $apply['ufielddata'] = unserialize($apply['ufielddata']);
  100. //VIP折扣
  101. $vipgroup = unserialize($_G['cache']['plugin']['xj_event']['vipgroupid']);
  102. //优惠计算
  103. $youhuiprice = $eventcore->GetYouHui($tid,$_G['uid']);
  104. include template('xj_event:event_pay');
  105. }else{
  106. if(!submitcheck('paysubmit')){
  107. showmessage('submit_invalid');
  108. }
  109. $applyid = intval($_GET['applyid']);
  110. $uid = intval($_G['uid']);
  111. $item = DB::fetch_first("SELECT tid,applyid,applynumber FROM ".DB::table('xj_eventapply')." WHERE applyid = $applyid and uid=".$_G['uid']);
  112. $tid = $item['tid'];
  113. $applyid = $item['applyid'];
  114. $applynumber = $item['applynumber'];
  115. $item = DB::fetch_first("SELECT A.use_cost,A.setting,B.subject FROM ".DB::table('xj_event')." A,".DB::table('forum_thread')." B WHERE A.tid = '$tid' and A.tid=B.tid");
  116. $setting = unserialize($item['setting']);
  117. //报名方式不同,计算人数
  118. if($setting['nodaibaoming']){
  119. }else{
  120. $applynumber = DB::result_first("SELECT count(*) FROM ".DB::table('xj_eventapply')." WHERE tid = '$tid' and uid=".$_G['uid']);
  121. }
  122. $subject = $item['subject'];
  123. $use_cost = $item['use_cost'];
  124. $price = number_format($use_cost*$applynumber,2,'.','');
  125. //$paytype = addslashes($_GET['bank_type']);
  126. $paytype = is_numeric($_GET['bank_type']) ? 'tenpay' : $_GET['bank_type'];
  127. $create_time = $_G['timestamp'];
  128. if(empty($uid) || empty($tid) || empty($price)){
  129. exit('Access Denied');
  130. }
  131. //如果是多种报名
  132. if($setting['cost']){
  133. if($setting['nodaibaoming']){
  134. $apply = DB::fetch_first("SELECT * FROM ".DB::table('xj_eventapply')." WHERE tid = '$tid' and uid=".$_G['uid']);
  135. $apply['ufielddata'] = unserialize($apply['ufielddata']);
  136. $price = 0;
  137. foreach($setting['cost'] as $value){
  138. $price = $price+$apply['ufielddata']['cost'.$value['id']]*$value['cost_price'];
  139. }
  140. }else{
  141. $apply = DB::fetch_all("SELECT * FROM ".DB::table('xj_eventapply')." WHERE tid = '$tid' and uid=".$_G['uid']);
  142. $price = 0;
  143. foreach($apply as $value){
  144. $value['ufielddata'] = unserialize($value['ufielddata']);
  145. $price = $price + $setting['cost'][$value['ufielddata']['costclass']]['cost_price'];
  146. }
  147. }
  148. }
  149. //VIP折扣
  150. if(file_exists(DISCUZ_ROOT.'./source/plugin/xj_event/module/vip/event_pay.php')) {
  151. @include 'module/vip/event_pay.php';
  152. }
  153. if($setting['app_benefit']>0 && $Appbyme){
  154. $price = $price - $setting['app_benefit'];
  155. }
  156. $item = DB::fetch_first("SELECT orderid FROM ".DB::table('xj_eventpay_log')." WHERE applyid = $applyid");
  157. $orderid = dgmdate(TIMESTAMP, 'YmdHis').random(18); //创建支付订单号
  158. DB::query("INSERT INTO ".DB::table('xj_eventpay_log')."
  159. (applyid, uid, tid, orderid, paytype, subject, price, total_fee, create_time)
  160. VALUES
  161. ('$applyid', '$uid', '$tid', '$orderid', '$paytype', '$subject', '$use_cost', '$price', '$create_time')");
  162. }
  163. if($paytype=='alipay'){
  164. if($_G['mobile'] && $_G['cache']['plugin']['xj_event']['alipaywap']){
  165. list($ec_contract, $ec_securitycode, $ec_partner, $ec_creditdirectpay) = explode("\t", authcode($_G['setting']['ec_contract'], 'DECODE', $_G['config']['security']['authkey']));
  166. $alipay_config['partner'] = $ec_partner;
  167. $alipay_config['seller_id'] = $alipay_config['partner'];
  168. $alipay_config['key'] = $ec_securitycode;
  169. $alipay_config['notify_url'] = $_G['siteurl'].'source/plugin/xj_event/event_pay_notify.php';
  170. $alipay_config['return_url'] = $_G['siteurl'].'source/plugin/xj_event/event_pay_notify.php';
  171. $alipay_config['sign_type'] = strtoupper('MD5');
  172. //字符编码格式 目前支持utf-8
  173. $alipay_config['input_charset']= strtolower('utf-8');
  174. //ca证书路径地址,用于curl中ssl校验
  175. //请保证cacert.pem文件在当前文件夹目录中
  176. $alipay_config['cacert'] = getcwd().'\\cacert.pem';
  177. //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
  178. $alipay_config['transport'] = 'http';
  179. $alipay_config['payment_type'] = "1";
  180. $alipay_config['service'] = "alipay.wap.create.direct.pay.by.user";
  181. require_once("source/plugin/xj_event/include/alipay_submit.class.php");
  182. //商户订单号,商户网站订单系统中唯一订单号,必填
  183. $out_trade_no = $orderid;
  184. //订单名称,必填
  185. //$subject = $_G['setting']['bbname'].' '.$_G['member']['username'].' '.$subject.' '.lang('plugin/xj_event', 'huodongbm');
  186. if($_G['charset']=='gbk'){
  187. if(mb_strlen($subject,'gbk')>10){
  188. $subject = mb_substr($subject,0,10,'gbk');
  189. }
  190. $subject = iconv('gbk','utf-8',$subject);
  191. }else{
  192. if(mb_strlen($subject,'utf-8')>10){
  193. $subject = mb_substr($subject,0,10,'utf-8');
  194. }
  195. }
  196. //付款金额,必填
  197. $total_fee = $price;
  198. //收银台页面上,商品展示的超链接,必填
  199. $show_url = $_G['siteurl'].'forum.php?mod=viewthread&tid=$tid';
  200. //商品描述,可空
  201. $body = '';
  202. $parameter = array(
  203. "service" => $alipay_config['service'],
  204. "partner" => $alipay_config['partner'],
  205. "seller_id" => $alipay_config['seller_id'],
  206. "payment_type" => $alipay_config['payment_type'],
  207. "notify_url" => $alipay_config['notify_url'],
  208. "return_url" => $alipay_config['return_url'],
  209. "_input_charset" => trim(strtolower($alipay_config['input_charset'])),
  210. "out_trade_no" => $out_trade_no,
  211. "subject" => $subject,
  212. "total_fee" => $total_fee,
  213. "show_url" => $show_url,
  214. "body" => $body,
  215. //其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.2Z6TSk&treeId=60&articleId=103693&docType=1
  216. );
  217. //print_r($alipay_config);
  218. //exit();
  219. $alipaySubmit = new AlipaySubmit($alipay_config);
  220. $html_text = $alipaySubmit->buildRequestForm($parameter,"get", lang('plugin/xj_event', 'queren'));
  221. echo $html_text;
  222. exit();
  223. }else{
  224. list($ec_contract, $ec_securitycode, $ec_partner, $ec_creditdirectpay) = explode("\t", authcode($_G['setting']['ec_contract'], 'DECODE', $_G['config']['security']['authkey']));
  225. define('DISCUZ_PARTNER', $ec_partner);
  226. define('DISCUZ_SECURITYCODE', $ec_securitycode);
  227. define('DISCUZ_DIRECTPAY', $ec_creditdirectpay);
  228. define('STATUS_SELLER_SEND', 4);
  229. define('STATUS_WAIT_BUYER', 5);
  230. define('STATUS_TRADE_SUCCESS', 7);
  231. define('STATUS_REFUND_CLOSE', 17);
  232. $args = array(
  233. 'subject' => $_G['setting']['bbname'].' '.$_G['member']['username'].' '.$subject.' '.lang('plugin/xj_event', 'huodongbm'),
  234. 'body' => lang('plugin/xj_event', 'hdbmfk').' '.$price.lang('plugin/xj_event', 'yuan').$_G['clientip'],
  235. 'service' => 'trade_create_by_buyer',
  236. 'partner' => DISCUZ_PARTNER,
  237. 'notify_url' => $_G['siteurl'].'source/plugin/xj_event/event_pay_notify.php',
  238. 'return_url' => $_G['siteurl'].'source/plugin/xj_event/event_pay_notify.php',
  239. 'show_url' => $_G['siteurl'],
  240. '_input_charset' => CHARSET,
  241. 'out_trade_no' => $orderid,
  242. 'price' => $price,
  243. 'quantity' => 1,
  244. 'seller_email' => $_G['setting']['ec_account'],
  245. );
  246. if(DISCUZ_DIRECTPAY) {
  247. $args['service'] = 'create_direct_pay_by_user';
  248. $args['payment_type'] = '1';
  249. } else {
  250. $args['logistics_type'] = 'EXPRESS';
  251. $args['logistics_fee'] = 0;
  252. $args['logistics_payment'] = 'SELLER_PAY';
  253. $args['payment_type'] = 1;
  254. }
  255. ksort($args);
  256. $urlstr = $sign = '';
  257. foreach($args as $key => $val) {
  258. $sign .= '&'.$key.'='.$val;
  259. $urlstr .= $key.'='.rawurlencode($val).'&';
  260. }
  261. $sign = substr($sign, 1);
  262. $sign = md5($sign.DISCUZ_SECURITYCODE);
  263. header('Location: https://www.alipay.com/cooperate/gateway.do?'.$urlstr.'sign='.$sign.'&sign_type=MD5');
  264. }
  265. }elseif($paytype=='tenpay'){
  266. if($_GET['bank_type'] == 'tenpay'){
  267. $_GET['bank_type'] = '0';
  268. }
  269. $bank = $_GET['bank_type'];
  270. define('DISCUZ_PARTNER', $_G['setting']['ec_tenpay_bargainor']);
  271. define('DISCUZ_SECURITYCODE', $_G['setting']['ec_tenpay_key']);
  272. define('DISCUZ_AGENTID', '1204737401');
  273. define('DISCUZ_TENPAY_OPENTRANS_CHNID', $_G['setting']['ec_tenpay_opentrans_chnid']);
  274. define('DISCUZ_TENPAY_OPENTRANS_KEY', $_G['setting']['ec_tenpay_opentrans_key']);
  275. define('STATUS_SELLER_SEND', 3);
  276. define('STATUS_WAIT_BUYER', 4);
  277. define('STATUS_TRADE_SUCCESS', 5);
  278. define('STATUS_REFUND_CLOSE', 9);
  279. include_once DISCUZ_ROOT . './source/class/class_chinese.php';
  280. include_once DISCUZ_ROOT . './api/trade/api_tenpay.php';
  281. $date = dgmdate(TIMESTAMP, 'YmdHis');
  282. $suffix = dgmdate(TIMESTAMP, 'His').rand(1000, 9999);
  283. $transaction_id = DISCUZ_PARTNER.$date.$suffix;
  284. $chinese = new Chinese(strtoupper(CHARSET), 'GBK');
  285. $subject = $chinese->Convert($_G['setting']['bbname'].' - '.$_G['member']['username'].' - '.$subject.' - '.lang('plugin/xj_event', 'huodongbm'));
  286. $subject = cutstr($subject,32,'');
  287. $reqHandler = new RequestHandler();
  288. $reqHandler->setGateURL("https://gw.tenpay.com/gateway/pay.htm");
  289. $reqHandler->init();
  290. $reqHandler->setKey(DISCUZ_SECURITYCODE);
  291. $reqHandler->setParameter("partner", DISCUZ_PARTNER);
  292. $reqHandler->setParameter("out_trade_no", $orderid);
  293. $reqHandler->setParameter("total_fee", $price * 100);
  294. $reqHandler->setParameter("return_url", $_G['siteurl'].'plugin.php?id=xj_event:event_pay&action=paysucceed');
  295. $reqHandler->setParameter("notify_url", $_G['siteurl'].'source/plugin/xj_event/event_pay_notify.php');
  296. $reqHandler->setParameter("body", $subject);
  297. $reqHandler->setParameter("bank_type", $bank);
  298. $reqHandler->setParameter("spbill_create_ip", $_G['clientip']);
  299. $reqHandler->setParameter("fee_type", "1");
  300. $reqHandler->setParameter("subject", $subject);
  301. $reqHandler->setParameter("sign_type", "MD5");
  302. $reqHandler->setParameter("service_version", "1.0");
  303. $reqHandler->setParameter("input_charset", "GBK");
  304. $reqHandler->setParameter("sign_key_index", "1");
  305. $reqHandler->setParameter("attach", "tenpay");
  306. $reqHandler->setParameter("time_start", $date);
  307. $reqHandler->setParameter("trade_mode","1");
  308. $reqHandler->setParameter("trans_type","1");
  309. $reqHandler->setParameter("agentid", DISCUZ_AGENTID);
  310. $reqHandler->setParameter("agent_type","2");
  311. $reqUrl = $reqHandler->getRequestURL();
  312. header('Location: '.$reqUrl);
  313. }
  314. ?>