国内流行的内容管理系统(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.

285 lines
12KB

  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. $config = array(
  46. "appid" => $pData['AppID'],
  47. "mch_id" => $pData['MchID'],
  48. "mch_key" => $pData['APIv2Secret'],
  49. );
  50. $wechat = new \WeChat\Pay($config);
  51. $options = array(
  52. 'out_trade_no' => $buyid,
  53. );
  54. $result = $wechat->queryOrder($options);
  55. if ($result['return_code'] === "SUCCESS" && $result['trade_state'] === "SUCCESS") {
  56. $row = $dsql->GetOne("SELECT * FROM `#@__moneycard_type` WHERE tid='{$moRow['pid']}'");
  57. $query = "UPDATE `#@__member_operation` SET sta = '2' WHERE buyid = '$buyid'";
  58. $dsql->ExecuteNoneQuery($query);
  59. $query = "UPDATE `#@__member` SET money = money+{$row['num']} WHERE mid = '$mid'";
  60. $dsql->ExecuteNoneQuery($query);
  61. ShowMsg("已经完成付款", "index.php");
  62. exit;
  63. } else {
  64. ShowMsg("尚未完成付款操作", "index.php");
  65. exit;
  66. }
  67. }
  68. if (isset($pd_encode) && isset($pd_verify) && md5("payment".$pd_encode.$cfg_cookie_encode) == $pd_verify) {
  69. $result = json_decode(mchStrCode($pd_encode, 'DECODE'));
  70. $product = preg_replace("#[^0-9a-z]#i", "", $result->product);
  71. $pid = preg_replace("#[^0-9a-z]#i", "", $result->pid);
  72. $row = $dsql->GetOne("SELECT * FROM `#@__member_operation` WHERE mid='$mid' AND sta=0 AND product='$product'");
  73. if (!isset($row['buyid'])) {
  74. ShowMsg("请不要重复提交表单", 'javascript:;');
  75. exit();
  76. }
  77. if ($paytype === 0) {
  78. ShowMsg("请选择支付方式", 'javascript:;');
  79. exit();
  80. }
  81. $buyid = $row['buyid'];
  82. } else {
  83. $buyid = 'M'.$mid.'T'.$mtime.'RN'.mt_rand(100, 999);
  84. //删除用户旧的未付款的同类记录
  85. if (!empty($product)) {
  86. $dsql->ExecuteNoneQuery("DELETE FROM `#@__member_operation` WHERE mid='$mid' AND sta=0 AND product='$product'");
  87. }
  88. }
  89. if (empty($product)) {
  90. ShowMsg("请选择一个产品", 'javascript:;');
  91. exit();
  92. }
  93. $pid = isset($pid) && is_numeric($pid) ? $pid : 0;
  94. if ($product == 'member') {
  95. $ptype = "会员升级";
  96. $row = $dsql->GetOne("SELECT * FROM `#@__member_type` WHERE aid='{$pid}'");
  97. if (!is_array($row)) {
  98. ShowMsg("无法识别您的订单", 'javascript:;');
  99. exit();
  100. }
  101. $pname = $row['pname'];
  102. $price = $row['money'];
  103. } else if ($product == 'card') {
  104. $ptype = "积分购买";
  105. $row = $dsql->GetOne("SELECT * FROM `#@__moneycard_type` WHERE tid='{$pid}'");
  106. if (!is_array($row)) {
  107. ShowMsg("无法识别您的订单", 'javascript:;');
  108. exit();
  109. }
  110. $pname = $row['pname'];
  111. $price = $row['money'];
  112. }
  113. if ($paytype === 0) {
  114. $inquery = "INSERT INTO `#@__member_operation` (`buyid`,`pname`,`product`,`money`,`mtime`,`pid`,`mid`,`sta`,`oldinfo`) VALUES ('$buyid','$pname','$product','$price','$mtime','$pid','$mid','0','$ptype');";
  115. $isok = $dsql->ExecuteNoneQuery($inquery);
  116. if (!$isok) {
  117. echo "数据库出错,请重新尝试".$dsql->GetError();
  118. exit();
  119. }
  120. if ($price == '') {
  121. echo "无法识别您的订单";
  122. exit();
  123. }
  124. //获取支付接口设置
  125. $payment_list = array();
  126. $dsql->SetQuery("SELECT * FROM `#@__sys_payment` WHERE `status`=1 ORDER BY sortrank ASC");
  127. $dsql->Execute();
  128. $i = 0;
  129. while ($row = $dsql->GetArray()) {
  130. $payment_list[] = $row;
  131. $i++;
  132. }
  133. $pr_encode = array();
  134. foreach ($_REQUEST as $key => $val) {
  135. if (!in_array($key, array('product', 'pid'))) {
  136. continue;
  137. }
  138. $val = preg_replace("#[^0-9a-z]#i", "", $val);
  139. $pr_encode[$key] = $val;
  140. }
  141. $pr_encode = str_replace('=', '', mchStrCode(json_encode($pr_encode)));
  142. $pr_verify = md5("payment".$pr_encode.$cfg_cookie_encode);
  143. $tpl = new DedeTemplate();
  144. $tpl->LoadTemplate(DEDEMEMBER.'/templets/buy_action_payment.htm');
  145. $tpl->Display();
  146. } else {
  147. $moRow = $dsql->GetOne("SELECT * FROM `#@__member_operation` WHERE buyid='$buyid'");
  148. if ($moRow['sta'] == 2) {
  149. ShowMsg("已完成支付,无需重复付款", "javascript:;");
  150. exit;
  151. }
  152. if($paytype === 1) {
  153. //微信支付
  154. include_once(DEDEINC.'/libraries/oxwindow.class.php');
  155. $pInfo = $dsql->GetOne("SELECT * FROM `#@__sys_payment` WHERE id = $paytype");
  156. $pData = (array)json_decode($pInfo['config']);
  157. $config = array(
  158. "appid" => $pData['AppID'],
  159. "mch_id" => $pData['MchID'],
  160. "mch_key" => $pData['APIv2Secret'],
  161. );
  162. $wechat = new \WeChat\Pay($config);
  163. $options = array(
  164. 'product_id' => $buyid,
  165. 'body' => $row['pname'],
  166. 'out_trade_no' => $buyid,
  167. 'total_fee' => $row['money']*100,
  168. 'trade_type' => 'NATIVE',
  169. 'notify_url' => $GLOBALS['cfg_basehost'].$GLOBALS['cfg_phpurl'].'/notify.php?dopost=wechat',
  170. );
  171. try {
  172. //生成预支付码
  173. $result = $wechat->createOrder($options);
  174. $payurl = $result['code_url'];
  175. $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>";
  176. $script = '<script type="text/javascript">var qrcode = new QRCode(document.getElementById("qrcode"), {
  177. width : 300,
  178. height : 300,
  179. correctLevel : 3
  180. });qrcode.makeCode("'.$payurl.'");</script>';
  181. $wintitle = "微信支付";
  182. $wecome_info = " ";//这个空格不要去
  183. $win = new OxWindow();
  184. $win->AddMsgItem($msg);
  185. $winform = $win->GetWindow("hand", "&nbsp;", false);
  186. $win->Display(DEDEMEMBER."/templets/win_templet.htm");
  187. } catch (Exception $e) {
  188. ShowMsg("生成微信支付信息失败,请联系网站管理员", "javascript:;");
  189. exit;
  190. }
  191. } elseif ($paytype === 2) {
  192. include_once(DEDEINC.'/libraries/oxwindow.class.php');
  193. $pInfo = $dsql->GetOne("SELECT * FROM `#@__sys_payment` WHERE id = $paytype");
  194. $pData = (array)json_decode($pInfo['config']);
  195. $config = array(
  196. "sign_type" => $pData['SignType'],
  197. "appid" => $pData['APPID'],
  198. "private_key" => $pData['PrivateKey'],
  199. "public_key" => $pData['CertPublicKey'],
  200. "notify_url" => $GLOBALS['cfg_basehost'].$GLOBALS['cfg_phpurl'].'/notify.php?dopost=alipay',
  201. "return_url" => $GLOBALS['cfg_basehost'].$GLOBALS['cfg_phpurl'].'/return.php?dopost=alipay',
  202. );
  203. //var_dump($config);exit;
  204. //支付宝
  205. try {
  206. //实例支付对象
  207. $pay = \AliPay\Web::instance($config);
  208. $result = $pay->apply(array(
  209. 'out_trade_no' => $buyid, // 商户订单号
  210. 'total_amount' => $row['money'], // 支付金额
  211. 'subject' => $row['pname'], // 支付订单描述
  212. ));
  213. echo $result;
  214. } catch (Exception $e) {
  215. echo $e->getMessage();
  216. }
  217. } elseif ($paytype === 3) {
  218. include_once(DEDEINC.'/libraries/oxwindow.class.php');
  219. //银行转账
  220. $pInfo = $dsql->GetOne("SELECT * FROM `#@__sys_payment` WHERE id = $paytype");
  221. $pData = (array)json_decode($pInfo['config']);
  222. $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>";
  223. $wintitle = "银行转账";
  224. $wecome_info = " ";//这个空格不要去
  225. $win = new OxWindow();
  226. $win->AddMsgItem($msg);
  227. $winform = $win->GetWindow("hand", "&nbsp;", false);
  228. $win->Display(DEDEMEMBER."/templets/win_templet.htm");
  229. } elseif ($paytype === 4) {
  230. //余额付款
  231. if ($cfg_ml->M_UserMoney < $row['money']) {
  232. ShowMsg("余额不足,请确保当前账户有足够金币支付", "javascript:;");
  233. exit;
  234. }
  235. $query = "UPDATE `#@__member_operation` SET sta = '2' WHERE buyid = '$buyid'";
  236. $dsql->ExecuteNoneQuery($query);
  237. $query = "UPDATE `#@__member` SET money = money+{$row['num']} WHERE mid = '$mid'";
  238. $dsql->ExecuteNoneQuery($query);
  239. $query = "UPDATE `#@__member` SET user_money = user_money-{$row['money']} WHERE mid = '$mid'";
  240. $dsql->ExecuteNoneQuery($query);
  241. ShowMsg("成功使用余额付款", "javascript:;");
  242. exit;
  243. } elseif ($paytype === 5) {
  244. //货到付款
  245. ShowMsg("虚拟物品,不支持货到付款", "javascript:;");
  246. exit;
  247. }
  248. }
  249. /**
  250. * 加密函数
  251. *
  252. * @access public
  253. * @param string $string 字符串
  254. * @param string $operation 操作
  255. * @return string
  256. */
  257. function mchStrCode($string, $operation = 'ENCODE')
  258. {
  259. $key_length = 4;
  260. $expiry = 0;
  261. $key = md5($GLOBALS['cfg_cookie_encode']);
  262. $fixedkey = md5($key);
  263. $egiskeys = md5(substr($fixedkey, 16, 16));
  264. $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
  265. $keys = md5(substr($runtokey, 0, 16).substr($fixedkey, 0, 16).substr($runtokey, 16).substr($fixedkey, 16));
  266. $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16).$string : base64_decode(substr($string, $key_length));
  267. $i = 0;
  268. $result = '';
  269. $string_length = strlen($string);
  270. for ($i = 0; $i < $string_length; $i++) {
  271. $result .= chr(ord($string[$i]) ^ ord($keys[$i % 32]));
  272. }
  273. if ($operation == 'ENCODE') {
  274. return $runtokey.str_replace('=', '', base64_encode($result));
  275. } else {
  276. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
  277. return substr($result, 26);
  278. } else {
  279. return '';
  280. }
  281. }
  282. }
  283. ?>