国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
14KB

  1. <?php
  2. /**
  3. * @version $id:buy_action.php 8:38 2023年02月13日 tianya $
  4. * @package DedeBIZ.User
  5. * @copyright Copyright (c) 2022 DedeBIZ.COM
  6. * @license https://www.dedebiz.com/license
  7. * @link https://www.dedebiz.com
  8. */
  9. require_once(dirname(__FILE__)."/config.php");
  10. CheckRank(0, 0);
  11. $dopost = isset($dopost)? $dopost : '';
  12. $menutype = 'mydede';
  13. $menutype_son = 'op';
  14. require_once DEDEINC.'/dedetemplate.class.php';
  15. $product = isset($product) ? trim(HtmlReplace($product, 1)) : '';
  16. $mid = $cfg_ml->M_ID;
  17. $ptype = '';
  18. $pname = '';
  19. $price = '';
  20. $mtime = time();
  21. $paytype = isset($paytype)? intval($paytype) : 0;
  22. $buyid = isset($buyid)? HtmlReplace($buyid, 1) : '';
  23. if ($dopost === "bank_ok") {
  24. $moRow = $dsql->GetOne("SELECT * FROM `#@__member_operation` WHERE buyid='$buyid' AND mid={$mid}");
  25. if (empty($moRow)) {
  26. ShowMsg("订单查询错误,请确保是您自己发起的订单", "javascript:;");
  27. exit;
  28. }
  29. if ($moRow['sta'] == 2) {
  30. ShowMsg("已完成支付,无需重复付款", "javascript:;");
  31. exit;
  32. }
  33. $query = "UPDATE `#@__member_operation` SET sta = '1' WHERE buyid = '{$moRow['buyid']}'";
  34. $dsql->ExecuteNoneQuery($query);
  35. ShowMsg("已经完成付款,等待管理员审核", "operation.php");
  36. exit;
  37. } else if ($dopost === "wechat_ok") {
  38. $moRow = $dsql->GetOne("SELECT * FROM `#@__member_operation` WHERE buyid='$buyid' AND mid={$mid}");
  39. if (empty($moRow)) {
  40. ShowMsg("订单查询错误,请确保是您自己发起的订单", "javascript:;");
  41. exit;
  42. }
  43. $pInfo = $dsql->GetOne("SELECT * FROM `#@__sys_payment` WHERE id = 1");
  44. $pData = (array)json_decode($pInfo['config']);
  45. try {
  46. $config = array(
  47. "appid" => $pData['AppID'],
  48. "mch_id" => $pData['MchID'],
  49. "mch_key" => $pData['APIv2Secret'],
  50. );
  51. $wechat = new \WeChat\Pay($config);
  52. $options = array(
  53. 'out_trade_no' => $buyid,
  54. );
  55. $result = $wechat->queryOrder($options);
  56. } catch (Exception $e) {
  57. ShowMsg("生成微信支付信息失败,请联系网站管理员", "javascript:;");
  58. exit;
  59. }
  60. if ($result['return_code'] === "SUCCESS" && $result['trade_state'] === "SUCCESS") {
  61. $row = $dsql->GetOne("SELECT * FROM `#@__moneycard_type` WHERE tid='{$moRow['pid']}'");
  62. $query = "UPDATE `#@__member_operation` SET sta = '2' WHERE buyid = '$buyid'";
  63. $dsql->ExecuteNoneQuery($query);
  64. $query = "UPDATE `#@__member` SET money = money+{$row['num']} WHERE mid = '$mid'";
  65. $dsql->ExecuteNoneQuery($query);
  66. ShowMsg("已经完成付款", "index.php");
  67. exit;
  68. } else {
  69. ShowMsg("尚未完成付款操作", "index.php");
  70. exit;
  71. }
  72. }
  73. if (isset($pd_encode) && isset($pd_verify) && md5("payment".$pd_encode.$cfg_cookie_encode) == $pd_verify) {
  74. $result = json_decode(mchStrCode($pd_encode, 'DECODE'));
  75. $product = preg_replace("#[^0-9a-z]#i", "", $result->product);
  76. $pid = preg_replace("#[^0-9a-z]#i", "", $result->pid);
  77. $row = $dsql->GetOne("SELECT * FROM `#@__member_operation` WHERE mid='$mid' AND sta=0 AND product='$product'");
  78. if (!isset($row['buyid'])) {
  79. ShowMsg("请不要重复提交表单", 'javascript:;');
  80. exit();
  81. }
  82. if ($paytype === 0) {
  83. ShowMsg("请选择支付方式", 'javascript:;');
  84. exit();
  85. }
  86. $buyid = $row['buyid'];
  87. } else {
  88. $buyid = 'M'.$mid.'T'.$mtime.'RN'.mt_rand(100, 999);
  89. //删除用户旧的未付款的同类记录
  90. if (!empty($product)) {
  91. $dsql->ExecuteNoneQuery("DELETE FROM `#@__member_operation` WHERE mid='$mid' AND sta=0 AND product='$product'");
  92. }
  93. }
  94. if (empty($product)) {
  95. ShowMsg("请选择一个产品", 'javascript:;');
  96. exit();
  97. }
  98. $pid = isset($pid) && is_numeric($pid) ? $pid : 0;
  99. if ($product == 'member') {
  100. $ptype = "会员升级";
  101. $row = $dsql->GetOne("SELECT * FROM `#@__member_type` WHERE aid='{$pid}'");
  102. if (!is_array($row)) {
  103. ShowMsg("无法识别您的订单", 'javascript:;');
  104. exit();
  105. }
  106. $pname = $row['pname'];
  107. $price = $row['money'];
  108. } else if ($product == 'card') {
  109. $ptype = "积分购买";
  110. $row = $dsql->GetOne("SELECT * FROM `#@__moneycard_type` WHERE tid='{$pid}'");
  111. if (!is_array($row)) {
  112. ShowMsg("无法识别您的订单", 'javascript:;');
  113. exit();
  114. }
  115. $pname = $row['pname'];
  116. $price = $row['money'];
  117. }
  118. if ($paytype === 0) {
  119. $inquery = "INSERT INTO `#@__member_operation` (`buyid`,`pname`,`product`,`money`,`mtime`,`pid`,`mid`,`sta`,`oldinfo`) VALUES ('$buyid','$pname','$product','$price','$mtime','$pid','$mid','0','$ptype');";
  120. $isok = $dsql->ExecuteNoneQuery($inquery);
  121. if (!$isok) {
  122. echo "数据库出错,请重新尝试".$dsql->GetError();
  123. exit();
  124. }
  125. if ($price == '') {
  126. echo "无法识别您的订单";
  127. exit();
  128. }
  129. //获取支付接口设置
  130. $payment_list = array();
  131. $dsql->SetQuery("SELECT * FROM `#@__sys_payment` WHERE `status`=1 ORDER BY sortrank ASC");
  132. $dsql->Execute();
  133. $i = 0;
  134. while ($row = $dsql->GetArray()) {
  135. $payment_list[] = $row;
  136. $i++;
  137. }
  138. $pr_encode = array();
  139. foreach ($_REQUEST as $key => $val) {
  140. if (!in_array($key, array('product', 'pid'))) {
  141. continue;
  142. }
  143. $val = preg_replace("#[^0-9a-z]#i", "", $val);
  144. $pr_encode[$key] = $val;
  145. }
  146. $pr_encode = str_replace('=', '', mchStrCode(json_encode($pr_encode)));
  147. $pr_verify = md5("payment".$pr_encode.$cfg_cookie_encode);
  148. $tpl = new DedeTemplate();
  149. $tpl->LoadTemplate(DEDEMEMBER.'/templets/buy_action_payment.htm');
  150. $tpl->Display();
  151. } else {
  152. $moRow = $dsql->GetOne("SELECT * FROM `#@__member_operation` WHERE buyid='$buyid'");
  153. if ($moRow['sta'] == 2) {
  154. ShowMsg("已完成支付,无需重复付款", "javascript:;");
  155. exit;
  156. }
  157. if($paytype === 1) {
  158. //微信支付
  159. include_once(DEDEINC.'/libraries/oxwindow.class.php');
  160. $pInfo = $dsql->GetOne("SELECT * FROM `#@__sys_payment` WHERE id = $paytype");
  161. $pData = (array)json_decode($pInfo['config']);
  162. $config = array(
  163. "appid" => $pData['AppID'],
  164. "mch_id" => $pData['MchID'],
  165. "mch_key" => $pData['APIv2Secret'],
  166. );
  167. try {
  168. $wechat = new \WeChat\Pay($config);
  169. $options = array(
  170. 'product_id' => $buyid,
  171. 'body' => $row['pname'],
  172. 'out_trade_no' => $buyid,
  173. 'total_fee' => $row['money']*100,
  174. 'trade_type' => 'NATIVE',
  175. 'notify_url' => $GLOBALS['cfg_basehost'].$GLOBALS['cfg_phpurl'].'/notify.php?dopost=wechat',
  176. );
  177. //生成预支付码
  178. $result = $wechat->createOrder($options);
  179. $payurl = $result['code_url'];
  180. $msg = "请使用微信扫一扫,扫描二维码支付:<div id='qrcode' style='width:200px; height:200px; margin-top:15px;'></div><br/><a href='buy_action.php?dopost=wechat_ok&buyid={$buyid}' class='btn btn-success btn-sm'>已完成支付</a> <a href='operation.php' class='btn btn-outline-success btn-sm'>返回订单管理</a>";
  181. $script = '<script type="text/javascript">var qrcode = new QRCode(document.getElementById("qrcode"), {
  182. width : 300,
  183. height : 300,
  184. correctLevel : 3
  185. });qrcode.makeCode("'.$payurl.'");</script>';
  186. $wintitle = "微信支付";
  187. $wecome_info = " ";//这个空格不要去
  188. $win = new OxWindow();
  189. $win->AddMsgItem($msg);
  190. $winform = $win->GetWindow("hand", "&nbsp;", false);
  191. $win->Display(DEDEMEMBER."/templets/win_templet.htm");
  192. } catch (Exception $e) {
  193. ShowMsg("生成微信支付信息失败,请联系网站管理员", "javascript:;");
  194. exit;
  195. }
  196. } elseif ($paytype === 2) {
  197. include_once(DEDEINC.'/libraries/oxwindow.class.php');
  198. $pInfo = $dsql->GetOne("SELECT * FROM `#@__sys_payment` WHERE id = $paytype");
  199. $pData = (array)json_decode($pInfo['config']);
  200. $config = array(
  201. "sign_type" => $pData['SignType'],
  202. "appid" => $pData['APPID'],
  203. "private_key" => $pData['PrivateKey'],
  204. "public_key" => $pData['CertPublicKey'],
  205. "notify_url" => $GLOBALS['cfg_basehost'].$GLOBALS['cfg_phpurl'].'/notify.php?dopost=alipay',
  206. "return_url" => $GLOBALS['cfg_basehost'].$GLOBALS['cfg_phpurl'].'/return.php?dopost=alipay',
  207. );
  208. //var_dump($config);exit;
  209. //支付宝
  210. try {
  211. //实例支付对象
  212. $pay = \AliPay\Web::instance($config);
  213. $result = $pay->apply(array(
  214. 'out_trade_no' => $buyid, // 商户订单号
  215. 'total_amount' => $row['money'], // 支付金额
  216. 'subject' => $row['pname'], // 支付订单描述
  217. ));
  218. echo $result;
  219. } catch (Exception $e) {
  220. ShowMsg("生成微信支付信息失败,请联系网站管理员", "javascript:;");
  221. exit;
  222. }
  223. } elseif ($paytype === 3) {
  224. include_once(DEDEINC.'/libraries/oxwindow.class.php');
  225. //银行转账
  226. $pInfo = $dsql->GetOne("SELECT * FROM `#@__sys_payment` WHERE id = $paytype");
  227. $pData = (array)json_decode($pInfo['config']);
  228. $msg = "请汇款至如下账户:<br><b>账户名:</b>{$pData['AccountName']}<br><b>账号:</b>{$pData['AccountNO']}<br><b>开户行:</b>{$pData['Name']}<br/><b>备注:</b>{$buyid}<br/>如您已经完成转账,请点击下面按钮,等待管理员确认后即可完成充值<br><br><a href='buy_action.php?dopost=bank_ok&buyid={$buyid}' class='btn btn-success btn-sm'>已完成银行转账</a> <a href='operation.php' class='btn btn-outline-success btn-sm'>返回订单管理</a>";
  229. $wintitle = "银行转账";
  230. $wecome_info = " ";//这个空格不要去
  231. $win = new OxWindow();
  232. $win->AddMsgItem($msg);
  233. $winform = $win->GetWindow("hand", "&nbsp;", false);
  234. $win->Display(DEDEMEMBER."/templets/win_templet.htm");
  235. } elseif ($paytype === 4) {
  236. //余额付款
  237. if ($cfg_ml->M_UserMoney < $row['money']) {
  238. ShowMsg("余额不足,请确保当前账户有足够金币支付", "javascript:;");
  239. exit;
  240. }
  241. $query = "UPDATE `#@__member_operation` SET sta = '2' WHERE buyid = '$buyid'";
  242. if ($product == 'card') {
  243. $dsql->ExecuteNoneQuery($query);
  244. $query = "UPDATE `#@__member` SET money = money+{$row['num']} WHERE mid = '$mid'";
  245. $dsql->ExecuteNoneQuery($query);
  246. $query = "UPDATE `#@__member` SET user_money = user_money-{$row['money']} WHERE mid = '$mid'";
  247. $dsql->ExecuteNoneQuery($query);
  248. } else if($product == 'member'){
  249. $rank = $row['rank'];
  250. $exptime = $row['exptime'];
  251. $rs = $dsql->GetOne("SELECT uptime,exptime FROM `#@__member` WHERE mid='".$mid."'");
  252. if($rs['uptime']!=0 && $rs['exptime']!=0 )
  253. {
  254. $nowtime = time();
  255. $mhasDay = $rs['exptime'] - ceil(($nowtime - $rs['uptime'])/3600/24) + 1;
  256. $mhasDay=($mhasDay>0)? $mhasDay : 0;
  257. }
  258. $memrank = $dsql->GetOne("SELECT money,scores FROM `#@__arcrank` WHERE `rank`='$rank'");
  259. //更新会员信息
  260. $sqlm = "UPDATE `#@__member` SET `rank`='$rank',`money`=`money`+'{$memrank['money']}',
  261. scores=scores+'{$memrank['scores']}',exptime='$exptime'+'$mhasDay',uptime='".time()."'
  262. WHERE mid='".$mid."'";
  263. $sqlmo = "UPDATE `#@__member_operation` SET sta='2',oldinfo='会员升级成功' WHERE buyid='$buyid' ";
  264. if(!($dsql->ExecuteNoneQuery($sqlm) && $dsql->ExecuteNoneQuery($sqlmo)))
  265. {
  266. ShowMsg("余额付款升级会员失败", "javascript:;");
  267. exit;
  268. }
  269. }
  270. ShowMsg("成功使用余额付款", "javascript:;");
  271. exit;
  272. } elseif ($paytype === 5) {
  273. //货到付款
  274. ShowMsg("虚拟物品,不支持货到付款", "javascript:;");
  275. exit;
  276. }
  277. }
  278. /**
  279. * 加密函数
  280. *
  281. * @access public
  282. * @param string $string 字符串
  283. * @param string $operation 操作
  284. * @return string
  285. */
  286. function mchStrCode($string, $operation = 'ENCODE')
  287. {
  288. $key_length = 4;
  289. $expiry = 0;
  290. $key = md5($GLOBALS['cfg_cookie_encode']);
  291. $fixedkey = md5($key);
  292. $egiskeys = md5(substr($fixedkey, 16, 16));
  293. $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
  294. $keys = md5(substr($runtokey, 0, 16).substr($fixedkey, 0, 16).substr($runtokey, 16).substr($fixedkey, 16));
  295. $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16).$string : base64_decode(substr($string, $key_length));
  296. $i = 0;
  297. $result = '';
  298. $string_length = strlen($string);
  299. for ($i = 0; $i < $string_length; $i++) {
  300. $result .= chr(ord($string[$i]) ^ ord($keys[$i % 32]));
  301. }
  302. if ($operation == 'ENCODE') {
  303. return $runtokey.str_replace('=', '', base64_encode($result));
  304. } else {
  305. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
  306. return substr($result, 26);
  307. } else {
  308. return '';
  309. }
  310. }
  311. }
  312. ?>