人人商城

notify.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php error_reporting(0);
  2. define("IN_MOBILE", true);
  3. $input = file_get_contents("php://input");
  4. libxml_disable_entity_loader(true);
  5. if( !empty($input) && empty($_GET["out_trade_no"]) )
  6. {
  7. $obj = simplexml_load_string($input, "SimpleXMLElement", LIBXML_NOCDATA);
  8. $data = json_decode(json_encode($obj), true);
  9. if( empty($data) )
  10. {
  11. exit( "fail" );
  12. }
  13. if( empty($data["version"]) && ($data["result_code"] != "SUCCESS" || $data["return_code"] != "SUCCESS") )
  14. {
  15. $result = array( "return_code" => "FAIL", "return_msg" => (empty($data["return_msg"]) ? $data["err_code_des"] : $data["return_msg"]) );
  16. echo array2xml($result);
  17. exit();
  18. }
  19. if( !empty($data["version"]) && ($data["result_code"] != "0" || $data["status"] != "0") )
  20. {
  21. exit( "fail" );
  22. }
  23. $get = $data;
  24. }
  25. else
  26. {
  27. $get = $_GET;
  28. }
  29. require(dirname(__FILE__) . "/../../../../framework/bootstrap.inc.php");
  30. require(IA_ROOT . "/addons/ewei_shopv2/defines.php");
  31. require(IA_ROOT . "/addons/ewei_shopv2/core/inc/functions.php");
  32. require(IA_ROOT . "/addons/ewei_shopv2/core/inc/plugin_model.php");
  33. require(IA_ROOT . "/addons/ewei_shopv2/core/inc/com_model.php");
  34. new EweiShopWechatPay($get);
  35. exit( "fail" );
  36. class EweiShopWechatPay
  37. {
  38. public $get = NULL;
  39. public $type = NULL;
  40. public $total_fee = NULL;
  41. public $set = NULL;
  42. public $setting = NULL;
  43. public $sec = NULL;
  44. public $sign = NULL;
  45. public $isapp = false;
  46. public $is_jie = false;
  47. public function __construct($get)
  48. {
  49. global $_W;
  50. $this->get = $get;
  51. $strs = explode(":", $this->get["attach"]);
  52. $this->type = intval($strs[1]);
  53. $this->total_fee = round($this->get["total_fee"] / 100, 2);
  54. $GLOBALS["_W"]["uniacid"] = intval($strs[0]);
  55. $_W["uniacid"] = intval($strs[0]);
  56. $this->init();
  57. }
  58. public function success()
  59. {
  60. $result = array( "return_code" => "SUCCESS", "return_msg" => "OK" );
  61. echo array2xml($result);
  62. exit();
  63. }
  64. public function fail()
  65. {
  66. $result = array( "return_code" => "FAIL", "return_msg" => "签名失败" );
  67. echo array2xml($result);
  68. exit();
  69. }
  70. public function init()
  71. {
  72. if( $this->type == "0" )
  73. {
  74. $this->order();
  75. }
  76. else
  77. {
  78. if( $this->type == "1" )
  79. {
  80. $this->recharge();
  81. }
  82. else
  83. {
  84. if( $this->type == "2" )
  85. {
  86. $this->creditShop();
  87. }
  88. else
  89. {
  90. if( $this->type == "3" )
  91. {
  92. $this->creditShopFreight();
  93. }
  94. else
  95. {
  96. if( $this->type == "4" )
  97. {
  98. $this->coupon();
  99. }
  100. else
  101. {
  102. if( $this->type == "5" )
  103. {
  104. $this->groups();
  105. }
  106. else
  107. {
  108. if( $this->type == "6" )
  109. {
  110. $this->threen();
  111. }
  112. else
  113. {
  114. if( $this->type == "10" )
  115. {
  116. $this->mr();
  117. }
  118. else
  119. {
  120. if( $this->type == "11" )
  121. {
  122. $this->pstoreCredit();
  123. }
  124. else
  125. {
  126. if( $this->type == "12" )
  127. {
  128. $this->pstore();
  129. }
  130. else
  131. {
  132. if( $this->type == "13" )
  133. {
  134. $this->cashier();
  135. }
  136. else
  137. {
  138. if( $this->type == "14" )
  139. {
  140. $this->wxapp_order();
  141. }
  142. else
  143. {
  144. if( $this->type == "15" )
  145. {
  146. $this->wxapp_recharge();
  147. }
  148. else
  149. {
  150. if( $this->type == "16" )
  151. {
  152. $this->wxapp_coupon();
  153. }
  154. else
  155. {
  156. if( $this->type == "17" )
  157. {
  158. $this->grant();
  159. }
  160. else
  161. {
  162. if( $this->type == "18" )
  163. {
  164. $this->plugingrant();
  165. }
  166. else
  167. {
  168. if( $this->type == "19" )
  169. {
  170. $this->wxapp_groups();
  171. }
  172. else
  173. {
  174. if( $this->type == "20" )
  175. {
  176. $this->wxapp_membercard();
  177. }
  178. else
  179. {
  180. if( $this->type == "21" )
  181. {
  182. $this->membercard();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. $this->success();
  203. }
  204. public function order()
  205. {
  206. global $_W;
  207. if( !$this->publicMethod() )
  208. {
  209. exit( "order" );
  210. }
  211. $ordersn = $tid = $this->get["out_trade_no"];
  212. $count_ordersn = m("order")->countOrdersn($tid);
  213. $isborrow = 0;
  214. $borrowopenid = "";
  215. if( strpos($tid, "_borrow") !== false )
  216. {
  217. $tid = str_replace("_borrow", "", $tid);
  218. $isborrow = 1;
  219. $borrowopenid = $this->get["openid"];
  220. }
  221. if( strpos($tid, "_B") !== false )
  222. {
  223. $tid = str_replace("_B", "", $tid);
  224. $isborrow = 1;
  225. $borrowopenid = $this->get["openid"];
  226. }
  227. if( strexists($tid, "GJ") )
  228. {
  229. $tids = explode("GJ", $tid);
  230. list($tid, $ordersn2) = $tids;
  231. $sub_openid = $this->get["sub_openid"];
  232. $openid = $this->get["openid"];
  233. $openid = (empty($sub_openid) ? $openid : $sub_openid);
  234. if( 100 <= $ordersn2 )
  235. {
  236. pdo_update("ewei_shop_order", array( "ordersn2" => $ordersn2 ), array( "ordersn" => $tid, "openid" => $openid ));
  237. }
  238. }
  239. $ispeerpay = 0;
  240. if( 22 < strlen($tid) && $count_ordersn != 2 )
  241. {
  242. $tid2 = $tid;
  243. $ispeerpay = 1;
  244. }
  245. $paytype = 21;
  246. if( strexists($borrowopenid, "2088") || is_numeric($borrowopenid) )
  247. {
  248. $paytype = 22;
  249. }
  250. $tid = substr($tid, 0, 22);
  251. $order = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_order") . " WHERE ordersn = :ordersn AND uniacid = :uniacid", array( ":ordersn" => $tid, ":uniacid" => $_W["uniacid"] ));
  252. $sql = "SELECT * FROM " . tablename("core_paylog") . " WHERE `module`=:module AND `tid`=:tid limit 1";
  253. $params = array( );
  254. $params[":tid"] = $tid;
  255. $params[":module"] = "ewei_shopv2";
  256. $log = pdo_fetch($sql, $params);
  257. if( !empty($log) && ($log["status"] == "0" || $ispeerpay) && ($log["fee"] == $this->total_fee || $ispeerpay) )
  258. {
  259. $transaction_id = $this->get["transaction_id"];
  260. $out_transaction_id = $this->get["out_transaction_id"];
  261. $transaction_id = (empty($transaction_id) ? $out_transaction_id : $transaction_id);
  262. if( $count_ordersn == 2 )
  263. {
  264. pdo_update("ewei_shop_order", array( "tradepaytype" => 21, "isborrow" => $isborrow, "borrowopenid" => $borrowopenid, "apppay" => ($this->isapp ? 1 : 0), "transid" => $transaction_id ), array( "ordersn_trade" => $log["tid"], "uniacid" => $log["uniacid"] ));
  265. }
  266. else
  267. {
  268. pdo_update("ewei_shop_order", array( "paytype" => 21, "isborrow" => $isborrow, "borrowopenid" => $borrowopenid, "apppay" => ($this->isapp ? 1 : 0), "transid" => $transaction_id ), array( "ordersn" => $log["tid"], "uniacid" => $log["uniacid"] ));
  269. }
  270. $site = WeUtility::createModuleSite($log["module"]);
  271. m("order")->setOrderPayType($order["id"], $paytype);
  272. if( !empty($ispeerpay) )
  273. {
  274. $ispeerpay = m("order")->checkpeerpay($order["id"]);
  275. }
  276. if( !empty($ispeerpay) )
  277. {
  278. $openid = $this->get["openid"];
  279. $member = m("member")->getInfo($openid);
  280. m("order")->peerStatus(array( "pid" => $ispeerpay["id"], "uid" => $member["id"], "uname" => $member["nickname"], "usay" => "支持一下,么么哒!", "price" => $this->total_fee, "createtime" => time(), "openid" => $openid, "headimg" => $member["avatar"], "tid" => $tid2 ));
  281. $open_redis = function_exists("redis") && !is_error(redis());
  282. if( $open_redis )
  283. {
  284. $redis_key = (string) $_W["uniacid"] . "_peerpay_order__pay_" . $ispeerpay["id"];
  285. $redis = redis();
  286. $redis->delete($redis_key);
  287. }
  288. if( $_W["config"]["db"]["slave_status"] == true )
  289. {
  290. sleep(1);
  291. }
  292. $peerpay_info = (double) pdo_fetchcolumn("select SUM(price) from " . tablename("ewei_shop_order_peerpay_payinfo") . " where pid=:pid limit 1", array( ":pid" => $ispeerpay["id"] ));
  293. if( $peerpay_info < $ispeerpay["peerpay_realprice"] )
  294. {
  295. $this->success();
  296. }
  297. }
  298. if( !is_error($site) )
  299. {
  300. $method = "payResult";
  301. if( method_exists($site, $method) )
  302. {
  303. $ret = array( );
  304. $ret["acid"] = $log["acid"];
  305. $ret["uniacid"] = $log["uniacid"];
  306. $ret["result"] = "success";
  307. $ret["type"] = $log["type"];
  308. $ret["from"] = "return";
  309. $ret["tid"] = $log["tid"];
  310. $ret["user"] = $log["openid"];
  311. $ret["fee"] = $log["fee"];
  312. $ret["tag"] = $log["tag"];
  313. $result = $site->$method($ret);
  314. if( $result )
  315. {
  316. $log["tag"] = iunserializer($log["tag"]);
  317. $log["tag"]["transaction_id"] = $this->get["transaction_id"];
  318. $record = array( );
  319. $record["status"] = "1";
  320. $record["tag"] = iserializer($log["tag"]);
  321. pdo_update("core_paylog", $record, array( "plid" => $log["plid"] ));
  322. }
  323. }
  324. }
  325. }
  326. else
  327. {
  328. $this->fail();
  329. }
  330. }
  331. public function recharge()
  332. {
  333. global $_W;
  334. if( !$this->publicMethod() )
  335. {
  336. exit( "recharge" );
  337. }
  338. $logno = trim($this->get["out_trade_no"]);
  339. $isborrow = 0;
  340. $borrowopenid = "";
  341. if( strpos($logno, "_borrow") !== false )
  342. {
  343. $logno = str_replace("_borrow", "", $logno);
  344. $isborrow = 1;
  345. $borrowopenid = $this->get["openid"];
  346. }
  347. if( empty($logno) )
  348. {
  349. $this->fail();
  350. }
  351. $log = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_member_log") . " WHERE `uniacid`=:uniacid and `logno`=:logno limit 1", array( ":uniacid" => $_W["uniacid"], ":logno" => $logno ));
  352. $OK = !empty($log) && empty($log["status"]) && $log["money"] == $this->total_fee;
  353. if( $OK )
  354. {
  355. pdo_update("ewei_shop_member_log", array( "status" => 1, "rechargetype" => "wechat", "isborrow" => $isborrow, "borrowopenid" => $borrowopenid, "apppay" => ($this->isapp ? 1 : 0) ), array( "id" => $log["id"] ));
  356. $shopset = m("common")->getSysset("shop");
  357. m("member")->setCredit($log["openid"], "credit2", $log["money"], array( 0, $shopset["name"] . "会员充值:微信充值:余额:" . $log["money"] ));
  358. m("member")->setRechargeCredit($log["openid"], $log["money"]);
  359. com_run("sale::setRechargeActivity", $log);
  360. com_run("coupon::useRechargeCoupon", $log);
  361. m("notice")->sendMemberLogMessage($log["id"]);
  362. $member = m("member")->getMember($log["openid"]);
  363. $params = array( "nickname" => (empty($member["nickname"]) ? "未更新" : $member["nickname"]), "price" => $log["money"], "paytype" => "微信支付", "paytime" => date("Y-m-d H:i:s", time()) );
  364. com_run("printer::sendRechargeMessage", $params);
  365. }
  366. }
  367. public function creditShop()
  368. {
  369. global $_W;
  370. if( !$this->publicMethod() )
  371. {
  372. exit( "creditShop" );
  373. }
  374. $logno = trim($this->get["out_trade_no"]);
  375. if( empty($logno) )
  376. {
  377. exit();
  378. }
  379. $logno = str_replace("_borrow", "", $logno);
  380. if( p("creditshop") )
  381. {
  382. p("creditshop")->payResult($logno, "wechat", $this->total_fee, ($this->isapp ? true : false));
  383. }
  384. }
  385. public function creditShopFreight()
  386. {
  387. global $_W;
  388. if( !$this->publicMethod() )
  389. {
  390. exit( "creditShopFreight" );
  391. }
  392. $dispatchno = trim($this->get["out_trade_no"]);
  393. $dispatchno = str_replace("_borrow", "", $dispatchno);
  394. if( empty($dispatchno) )
  395. {
  396. exit();
  397. }
  398. $log = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_creditshop_log") . " WHERE `dispatchno`=:dispatchno and `uniacid`=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":dispatchno" => $dispatchno ));
  399. if( !empty($log) && $log["dispatchstatus"] < 0 )
  400. {
  401. pdo_update("ewei_shop_creditshop_log", array( "dispatchstatus" => 1 ), array( "dispatchno" => $dispatchno ));
  402. }
  403. }
  404. public function coupon()
  405. {
  406. global $_W;
  407. if( !$this->publicMethod() )
  408. {
  409. exit( "coupon" );
  410. }
  411. $logno = str_replace("_borrow", "", $this->get["out_trade_no"]);
  412. $log = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_coupon_log") . " WHERE `logno`=:logno and `uniacid`=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":logno" => $logno ));
  413. $coupon = pdo_fetchcolumn("select money from " . tablename("ewei_shop_coupon") . " where id=:id limit 1", array( ":id" => $log["couponid"] ));
  414. if( $coupon == $this->total_fee )
  415. {
  416. com_run("coupon::payResult", $logno);
  417. }
  418. }
  419. public function wxapp_coupon()
  420. {
  421. global $_W;
  422. $logno = str_replace("_borrow", "", $this->get["out_trade_no"]);
  423. $log = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_coupon_log") . " WHERE `logno`=:logno and `uniacid`=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":logno" => $logno ));
  424. $coupon = pdo_fetchcolumn("select money from " . tablename("ewei_shop_coupon") . " where id=:id limit 1", array( ":id" => $log["couponid"] ));
  425. if( $coupon == $this->total_fee )
  426. {
  427. com_run("coupon::payResult", $logno);
  428. }
  429. }
  430. public function groups()
  431. {
  432. global $_W;
  433. if( !$this->publicMethod() )
  434. {
  435. exit( "groups" );
  436. }
  437. $orderno = trim($this->get["out_trade_no"]);
  438. $orderno = str_replace("_borrow", "", $orderno);
  439. if( empty($orderno) )
  440. {
  441. exit();
  442. }
  443. if( $this->is_jie )
  444. {
  445. pdo_update("ewei_shop_groups_order", array( "isborrow" => "1", "borrowopenid" => $this->get["openid"] ), array( "orderno" => $orderno, "uniacid" => $_W["uniacid"] ));
  446. }
  447. if( p("groups") )
  448. {
  449. p("groups")->payResult($orderno, "wechat", ($this->isapp ? true : false));
  450. }
  451. }
  452. public function threen()
  453. {
  454. global $_W;
  455. if( !$this->publicMethod() )
  456. {
  457. exit( "threen" );
  458. }
  459. $orderno = trim($this->get["out_trade_no"]);
  460. $orderno = str_replace("_borrow", "", $orderno);
  461. if( empty($orderno) )
  462. {
  463. exit();
  464. }
  465. if( $this->is_jie )
  466. {
  467. pdo_update("ewei_shop_threen_log", array( "isborrow" => "1", "borrowopenid" => $this->get["openid"] ), array( "logno" => $orderno, "uniacid" => $_W["uniacid"] ));
  468. }
  469. if( p("threen") )
  470. {
  471. p("threen")->payResult($orderno, "wechat", ($this->isapp ? true : false));
  472. }
  473. }
  474. public function grant()
  475. {
  476. global $_W;
  477. $setting = pdo_fetch("select * from " . tablename("ewei_shop_system_grant_setting") . " where id = 1 limit 1 ");
  478. if( 0 < $setting["weixin"] )
  479. {
  480. ksort($this->get);
  481. $string1 = "";
  482. foreach( $this->get as $k => $v )
  483. {
  484. if( $v != "" && $k != "sign" )
  485. {
  486. $string1 .= (string) $k . "=" . $v . "&";
  487. }
  488. }
  489. $this->sign = strtoupper(md5($string1 . "key=" . $setting["apikey"]));
  490. if( $this->sign == $this->get["sign"] )
  491. {
  492. $order = pdo_fetch("select * from " . tablename("ewei_shop_system_grant_order") . " where logno = '" . $this->get["out_trade_no"] . "'");
  493. pdo_update("ewei_shop_system_grant_order", array( "paytime" => time(), "paystatus" => 1 ), array( "logno" => $this->get["out_trade_no"] ));
  494. $plugind = explode(",", $order["pluginid"]);
  495. $data = array( "logno" => $order["logno"], "uniacid" => $order["uniacid"], "code" => $order["code"], "type" => "pay", "month" => $order["month"], "isagent" => $order["isagent"], "createtime" => time() );
  496. foreach( $plugind as $key => $value )
  497. {
  498. $plugin = pdo_fetch("select `identity` from " . tablename("ewei_shop_plugin") . " where id = " . $value . " ");
  499. $data["identity"] = $plugin["identity"];
  500. $data["pluginid"] = $value;
  501. pdo_insert("ewei_shop_system_grant_log", $data);
  502. $id = pdo_insertid();
  503. if( m("grant") )
  504. {
  505. m("grant")->pluginGrant($id);
  506. }
  507. }
  508. }
  509. }
  510. }
  511. public function plugingrant()
  512. {
  513. global $_W;
  514. $setting = pdo_fetch("select * from " . tablename("ewei_shop_system_plugingrant_setting") . " where 1 = 1 limit 1 ");
  515. if( 0 < $setting["weixin"] )
  516. {
  517. ksort($this->get);
  518. $string1 = "";
  519. foreach( $this->get as $k => $v )
  520. {
  521. if( $v != "" && $k != "sign" )
  522. {
  523. $string1 .= (string) $k . "=" . $v . "&";
  524. }
  525. }
  526. $this->sign = strtoupper(md5($string1 . "key=" . $setting["apikey"]));
  527. if( $this->sign == $this->get["sign"] )
  528. {
  529. $order = pdo_fetch("select * from " . tablename("ewei_shop_system_plugingrant_order") . " where logno = '" . $this->get["out_trade_no"] . "'");
  530. pdo_update("ewei_shop_system_plugingrant_order", array( "paytime" => time(), "paystatus" => 1 ), array( "logno" => $this->get["out_trade_no"] ));
  531. $plugind = explode(",", $order["pluginid"]);
  532. $data = array( "logno" => $order["logno"], "uniacid" => $order["uniacid"], "type" => "pay", "month" => $order["month"], "createtime" => time() );
  533. foreach( $plugind as $key => $value )
  534. {
  535. $plugin = pdo_fetch("select `identity` from " . tablename("ewei_shop_plugin") . " where id = " . $value . " ");
  536. $data["identity"] = $plugin["identity"];
  537. $data["pluginid"] = $value;
  538. pdo_query("update " . tablename("ewei_shop_system_plugingrant_plugin") . " set sales = sales + 1 where pluginid = " . $value . " ");
  539. pdo_insert("ewei_shop_system_plugingrant_log", $data);
  540. $id = pdo_insertid();
  541. if( p("grant") )
  542. {
  543. p("grant")->pluginGrant($id);
  544. }
  545. }
  546. }
  547. }
  548. }
  549. public function mr()
  550. {
  551. global $_W;
  552. if( !$this->publicMethod() )
  553. {
  554. exit( "mr" );
  555. }
  556. $ordersn = trim($this->get["out_trade_no"]);
  557. $isborrow = 0;
  558. $borrowopenid = "";
  559. if( strpos($ordersn, "_borrow") !== false )
  560. {
  561. $ordersn = str_replace("_borrow", "", $ordersn);
  562. $isborrow = 1;
  563. $borrowopenid = $this->get["openid"];
  564. }
  565. if( empty($ordersn) )
  566. {
  567. exit();
  568. }
  569. if( p("mr") )
  570. {
  571. $price = pdo_fetchcolumn("select payprice from " . tablename("ewei_shop_mr_order") . " where ordersn=:ordersn limit 1", array( ":ordersn" => $ordersn ));
  572. if( $price == $this->total_fee )
  573. {
  574. if( $isborrow == 1 )
  575. {
  576. pdo_update("ewei_shop_order", array( "isborrow" => $isborrow, "borrowopenid" => $borrowopenid ), array( "ordersn" => $ordersn ));
  577. }
  578. p("mr")->payResult($ordersn, "wechat");
  579. }
  580. }
  581. }
  582. public function pstoreCredit()
  583. {
  584. global $_W;
  585. if( !$this->publicMethod() )
  586. {
  587. exit( "pstoreCredit" );
  588. }
  589. $ordersn = trim($this->get["out_trade_no"]);
  590. $ordersn = str_replace("_borrow", "", $ordersn);
  591. if( empty($ordersn) )
  592. {
  593. exit();
  594. }
  595. if( p("pstore") )
  596. {
  597. p("pstore")->payResult($ordersn, $this->total_fee);
  598. }
  599. }
  600. public function pstore()
  601. {
  602. global $_W;
  603. if( !$this->publicMethod() )
  604. {
  605. exit( "pstore" );
  606. }
  607. $ordersn = trim($this->get["out_trade_no"]);
  608. $ordersn = str_replace("_borrow", "", $ordersn);
  609. if( empty($ordersn) )
  610. {
  611. exit();
  612. }
  613. if( p("pstore") )
  614. {
  615. p("pstore")->wechat_complete($ordersn);
  616. }
  617. }
  618. public function cashier()
  619. {
  620. global $_W;
  621. $ordersn = trim($this->get["out_trade_no"]);
  622. if( empty($ordersn) )
  623. {
  624. exit();
  625. }
  626. if( p("cashier") )
  627. {
  628. p("cashier")->payResult($ordersn);
  629. }
  630. }
  631. public function wxapp_order()
  632. {
  633. $tid = $this->get["out_trade_no"];
  634. if( strexists($tid, "GJ") )
  635. {
  636. $tids = explode("GJ", $tid);
  637. $tid = $tids[0];
  638. }
  639. $sql = "SELECT * FROM " . tablename("core_paylog") . " WHERE `module`=:module AND `tid`=:tid limit 1";
  640. $params = array( );
  641. $params[":tid"] = $tid;
  642. $params[":module"] = "ewei_shopv2";
  643. $log = pdo_fetch($sql, $params);
  644. if( !empty($log) && $log["status"] == "0" && $log["fee"] == $this->total_fee )
  645. {
  646. $site = WeUtility::createModuleSite($log["module"]);
  647. if( !is_error($site) )
  648. {
  649. $method = "payResult";
  650. if( method_exists($site, $method) )
  651. {
  652. $ret = array( );
  653. $ret["acid"] = $log["acid"];
  654. $ret["uniacid"] = $log["uniacid"];
  655. $ret["result"] = "success";
  656. $ret["type"] = $log["type"];
  657. $ret["from"] = "return";
  658. $ret["tid"] = $log["tid"];
  659. $ret["user"] = $log["openid"];
  660. $ret["fee"] = $log["fee"];
  661. $ret["tag"] = $log["tag"];
  662. pdo_update("ewei_shop_order", array( "paytype" => 21, "apppay" => 2 ), array( "ordersn" => $log["tid"], "uniacid" => $log["uniacid"] ));
  663. $result = $site->$method($ret);
  664. if( $result )
  665. {
  666. $log["tag"] = iunserializer($log["tag"]);
  667. $log["tag"]["transaction_id"] = $this->get["transaction_id"];
  668. $record = array( );
  669. $record["status"] = "1";
  670. $record["tag"] = iserializer($log["tag"]);
  671. pdo_update("core_paylog", $record, array( "plid" => $log["plid"] ));
  672. }
  673. }
  674. }
  675. }
  676. else
  677. {
  678. $this->fail();
  679. }
  680. }
  681. public function wxapp_recharge()
  682. {
  683. global $_W;
  684. $logno = trim($this->get["out_trade_no"]);
  685. if( empty($logno) )
  686. {
  687. $this->fail();
  688. }
  689. $log = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_member_log") . " WHERE `uniacid`=:uniacid and `logno`=:logno limit 1", array( ":uniacid" => $_W["uniacid"], ":logno" => $logno ));
  690. $OK = !empty($log) && empty($log["status"]) && $log["money"] == $this->total_fee;
  691. if( $OK )
  692. {
  693. pdo_update("ewei_shop_member_log", array( "status" => 1, "rechargetype" => "wechat", "apppay" => 2 ), array( "id" => $log["id"] ));
  694. $shopset = m("common")->getSysset("shop");
  695. m("member")->setCredit($log["openid"], "credit2", $log["money"], array( 0, $shopset["name"] . "会员充值:微信充值:余额:" . $log["money"] ));
  696. m("member")->setRechargeCredit($log["openid"], $log["money"]);
  697. com_run("sale::setRechargeActivity", $log);
  698. com_run("coupon::useRechargeCoupon", $log);
  699. m("notice")->sendMemberLogMessage($log["id"]);
  700. }
  701. else
  702. {
  703. if( $log["money"] == $this->total_fee )
  704. {
  705. pdo_update("ewei_shop_member_log", array( "rechargetype" => "wechat", "apppay" => 2 ), array( "id" => $log["id"] ));
  706. }
  707. }
  708. }
  709. public function publicMethod()
  710. {
  711. global $_W;
  712. if( empty($_W["uniacid"]) )
  713. {
  714. return false;
  715. }
  716. list($set, $payment) = m("common")->public_build();
  717. $this->set = $set;
  718. if( empty($payment["is_new"]) || $this->get["trade_type"] == "APP" )
  719. {
  720. $this->setting = uni_setting($_W["uniacid"], array( "payment" ));
  721. if( is_array($this->setting["payment"]) || $this->set["weixin_jie"] == 1 || $this->set["weixin_sub"] == 1 || $this->set["weixin_jie_sub"] == 1 || $this->get["trade_type"] == "APP" )
  722. {
  723. $this->is_jie = strpos($this->get["out_trade_no"], "_B") !== false || strpos($this->get["out_trade_no"], "_borrow") !== false;
  724. $sec_yuan = m("common")->getSec();
  725. $this->sec = iunserializer($sec_yuan["sec"]);
  726. if( $this->set["weixin_jie"] == 1 && $this->is_jie || $this->set["weixin_sub"] == 1 || $this->set["weixin_jie_sub"] == 1 && $this->is_jie )
  727. {
  728. if( $this->set["weixin_sub"] == 1 )
  729. {
  730. $wechat = array( "version" => 1, "key" => $this->sec["apikey_sub"], "apikey" => $this->sec["apikey_sub"] );
  731. }
  732. if( $this->set["weixin_jie"] == 1 && $this->is_jie )
  733. {
  734. $wechat = array( "version" => 1, "key" => $this->sec["apikey"], "apikey" => $this->sec["apikey"] );
  735. }
  736. if( $this->set["weixin_jie_sub"] == 1 && $this->is_jie )
  737. {
  738. $wechat = array( "version" => 1, "key" => $this->sec["apikey_jie_sub"], "apikey" => $this->sec["apikey_jie_sub"] );
  739. }
  740. }
  741. else
  742. {
  743. if( $this->set["weixin"] == 1 )
  744. {
  745. $wechat = $this->setting["payment"]["wechat"];
  746. if( IMS_VERSION <= 0.8 )
  747. {
  748. $wechat["apikey"] = $wechat["signkey"];
  749. }
  750. }
  751. }
  752. if( $this->get["trade_type"] == "APP" && $this->set["app_wechat"] == 1 )
  753. {
  754. $this->isapp = true;
  755. $wechat = array( "version" => 1, "key" => $this->sec["app_wechat"]["apikey"], "apikey" => $this->sec["app_wechat"]["apikey"], "appid" => $this->sec["app_wechat"]["appid"], "mchid" => $this->sec["app_wechat"]["merchid"] );
  756. }
  757. if( !empty($wechat) )
  758. {
  759. ksort($this->get);
  760. $string1 = "";
  761. foreach( $this->get as $k => $v )
  762. {
  763. if( $v != "" && $k != "sign" )
  764. {
  765. $string1 .= (string) $k . "=" . $v . "&";
  766. }
  767. }
  768. $wechat["apikey"] = ($wechat["version"] == 1 ? $wechat["key"] : $wechat["apikey"]);
  769. $this->sign = strtoupper(md5($string1 . "key=" . $wechat["apikey"]));
  770. $this->get["openid"] = (isset($this->get["sub_openid"]) ? $this->get["sub_openid"] : $this->get["openid"]);
  771. if( $this->sign == $this->get["sign"] )
  772. {
  773. return true;
  774. }
  775. }
  776. }
  777. }
  778. else
  779. {
  780. if( !is_error($payment) )
  781. {
  782. if( $this->get["sign_type"] == "RSA_1_1" || $this->get["sign_type"] == "RSA_1_256" )
  783. {
  784. $signPars = "";
  785. ksort($this->get);
  786. foreach( $this->get as $k => $v )
  787. {
  788. if( "sign" != $k && "" != $v )
  789. {
  790. $signPars .= $k . "=" . $v . "&";
  791. }
  792. }
  793. $signPars = substr($signPars, 0, strlen($signPars) - 1);
  794. $res = openssl_pkey_get_public(m("common")->chackKey($payment["app_qpay_public_key"]));
  795. if( $this->get["sign_type"] == "RSA_1_1" )
  796. {
  797. $result = (bool) openssl_verify($signPars, base64_decode($this->get["sign"]), $res);
  798. openssl_free_key($res);
  799. return $result;
  800. }
  801. if( $this->get["sign_type"] == "RSA_1_256" )
  802. {
  803. $result = (bool) openssl_verify($signPars, base64_decode($this->get["sign"]), $res, OPENSSL_ALGO_SHA256);
  804. openssl_free_key($res);
  805. return $result;
  806. }
  807. }
  808. else
  809. {
  810. ksort($this->get);
  811. $string1 = "";
  812. foreach( $this->get as $k => $v )
  813. {
  814. if( $v != "" && $k != "sign" )
  815. {
  816. $string1 .= (string) $k . "=" . $v . "&";
  817. }
  818. }
  819. $this->sign = strtoupper(md5($string1 . "key=" . $payment["apikey"]));
  820. $this->get["openid"] = (isset($this->get["sub_openid"]) ? $this->get["sub_openid"] : $this->get["openid"]);
  821. if( $this->sign == $this->get["sign"] )
  822. {
  823. return true;
  824. }
  825. }
  826. }
  827. }
  828. return false;
  829. }
  830. public function wxapp_groups()
  831. {
  832. $orderno = $this->get["out_trade_no"];
  833. $sql = "SELECT * FROM " . tablename("ewei_shop_groups_paylog") . " WHERE `tid`=:orderno limit 1";
  834. $params = array( );
  835. $params[":orderno"] = $orderno;
  836. $log = pdo_fetch($sql, $params);
  837. if( !empty($log) && $log["status"] == "0" && $log["fee"] == $this->total_fee )
  838. {
  839. if( p("groups") )
  840. {
  841. pdo_update("ewei_shop_groups_paylog", array( "status" => "1" ), array( "id" => $log["id"] ));
  842. p("groups")->payResult($orderno, "wxapp");
  843. }
  844. }
  845. else
  846. {
  847. $this->fail();
  848. }
  849. }
  850. public function wxapp_membercard()
  851. {
  852. $orderno = $this->get["out_trade_no"];
  853. $sql = "SELECT * FROM " . tablename("core_paylog") . " WHERE `module`=:module AND `tid`=:tid limit 1";
  854. $params = array( );
  855. $params[":tid"] = $orderno;
  856. $params[":module"] = "ewei_shopv2";
  857. $log = pdo_fetch($sql, $params);
  858. if( !empty($log) && $log["status"] == "0" && $log["fee"] == $this->total_fee )
  859. {
  860. $plugin_membercard = p("membercard");
  861. if( $plugin_membercard )
  862. {
  863. $log["tag"] = iunserializer($log["tag"]);
  864. $log["tag"]["transaction_id"] = $this->get["transaction_id"];
  865. $log["tag"]["pay_time"] = time();
  866. $record = array( );
  867. $record["status"] = "1";
  868. $record["tag"] = iserializer($log["tag"]);
  869. pdo_update("core_paylog", $record, array( "plid" => $log["plid"] ));
  870. $plugin_membercard->payResult($orderno, "wechat");
  871. }
  872. }
  873. else
  874. {
  875. $this->fail();
  876. }
  877. }
  878. public function membercard()
  879. {
  880. global $_W;
  881. if( !$this->publicMethod() )
  882. {
  883. exit( "membercard" );
  884. }
  885. $orderno = trim($this->get["out_trade_no"]);
  886. $orderno = str_replace("_borrow", "", $orderno);
  887. if( empty($orderno) )
  888. {
  889. exit();
  890. }
  891. if( $this->is_jie )
  892. {
  893. pdo_update("ewei_shop_member_card_order", array( "isborrow" => "1", "borrowopenid" => $this->get["openid"] ), array( "orderno" => $orderno, "uniacid" => $_W["uniacid"] ));
  894. }
  895. if( p("membercard") )
  896. {
  897. p("membercard")->payResult($orderno, "wechat", ($this->isapp ? true : false));
  898. }
  899. }
  900. }
  901. ?>