PayFourBusiness.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. namespace app\business;
  3. use Illuminate\Support\Arr;
  4. use support\Db;
  5. use support\Redis;
  6. use Throwable;
  7. use yzh52521\EasyHttp\Http;
  8. class PayFourBusiness
  9. {
  10. static private $url = "http://ximen-api.zhangsan16688.com/api/order";
  11. static private $apikey = 'etn94hsTDv07h7Yw0VXRNcp8VQz9442C';
  12. static private $memberid = 600569;
  13. /**
  14. * @param array $param [
  15. * order_no
  16. * ]
  17. * @return array
  18. */
  19. static public function payment($order_no, $pay_bankcode, $money, $url = '')
  20. {
  21. if (empty($url)) {
  22. $url = getenv('WEB_HOST');
  23. }
  24. $arr = [
  25. 'amount' => $money,//金额,单位为元,精确到小数点后两位
  26. 'outOrderNum' => $order_no,//商户订单号
  27. 'mchNum' => self::$memberid,//商户号
  28. 'payType' => $pay_bankcode,//通道类型
  29. 'timestamp' => time(),//发送请求的时间戳
  30. 'notifyUrl' => getenv('API_HOST') . '/api/pay/payment_callback_four.html',//异步通知地址,支付成功后将支付成功消息以POST请求发送给这个网址
  31. ];
  32. $arr['sign'] = self::payMd5sign($arr);
  33. $arr['returnUrl'] = $url;
  34. $data = Http::post(self::$url, $arr)->array();
  35. return ['data' => $data, 'arr' => $arr];
  36. }
  37. static public function payMd5sign(array $param)
  38. {
  39. /* @对数组键进行ASCII码排序*/
  40. ksort($param);
  41. $arr = [];
  42. //将数组进行重组装
  43. foreach ($param as $k => $v) {
  44. if (!empty($v)) {
  45. $arr[] = $k . '=' . $v;
  46. }
  47. }
  48. //转换成字符串并且拼接上密钥
  49. $sign = implode('&', $arr) . '&key=' . self::$apikey;
  50. return strtoupper(md5($sign));
  51. }
  52. /** 生成订单号
  53. * @param array $param
  54. * @return int
  55. * @throws \Exception
  56. */
  57. static public function orderAdd(array $param)
  58. {
  59. try {
  60. $goods = Db::table('wa_goods')->where('id', $param['id'])->first();
  61. if (empty($goods)) {
  62. throw new \Exception('产品不存在!');
  63. }
  64. $payAisleData = Db::table('wa_pay_aisle')->where('characteristic', $param['pay_characteristic'])->first();
  65. if (empty($payAisleData)) {
  66. throw new \Exception('支付类型不存在!');
  67. }
  68. $characteristic = $payAisleData->characteristic;
  69. $pay_type = $payAisleData->type;
  70. $order_no = date('YmdHis') . mt_rand(1000, 9999);
  71. $payorderId = Db::table('wa_payorder')->insertGetId([
  72. 'goods_id' => $goods->id,
  73. 'goods_type' => $goods->type,
  74. 'user_id' => $param['user_data']['id'],
  75. 'order_no' => $order_no,
  76. 'pay_characteristic' => $characteristic,
  77. 'pay_type' => $pay_type,
  78. 'money' => bcmul($goods->pay_price, $param['num'], 2),
  79. 'num' => $param['num'],
  80. 'name' => Arr::get($param, 'name', null),
  81. 'mobile' => Arr::get($param, 'mobile', null),
  82. 'number' => Arr::get($param, 'number', null),
  83. 'address' => Arr::get($param, 'address', null),
  84. 'created_at' => date('Y-m-d H:i:s'),
  85. 'updated_at' => date('Y-m-d H:i:s'),
  86. ]);
  87. } catch (\Throwable $exception) {
  88. throw new \Exception($exception->getMessage());
  89. }
  90. return $payorderId;
  91. }
  92. /** 开通银行卡生成订单号
  93. * @param array $param
  94. * @return int
  95. * @throws \Exception
  96. */
  97. static public function CardorderAdd(array $param)
  98. {
  99. try {
  100. $goods = Db::table('wa_goods')->where('id', $param['id'])->first();
  101. if (empty($goods)) {
  102. throw new \Exception('产品不存在!');
  103. }
  104. $price_money = bcmul($goods->pay_price, $param['num']);
  105. if ($param['user_data']['money_one'] < $price_money) {
  106. throw new \Exception('华润财富余额不足!');
  107. }
  108. $payorderId = Db::table('wa_payorder')->insertGetId([
  109. 'goods_id' => $goods->id,
  110. 'goods_type' => $goods->type,
  111. 'user_id' => $param['user_data']['id'],
  112. 'order_no' => date('YmdHis') . mt_rand(1000, 9999),
  113. 'pay_characteristic' => 0,
  114. 'pay_type' => 0,
  115. 'is_pay' => 2,
  116. 'money' => $price_money,
  117. 'num' => 1,
  118. 'created_at' => date('Y-m-d H:i:s'),
  119. 'updated_at' => date('Y-m-d H:i:s'),
  120. ]);
  121. } catch (\Throwable $exception) {
  122. throw new \Exception($exception->getMessage());
  123. }
  124. return $payorderId;
  125. }
  126. /**提现
  127. * @return void
  128. */
  129. static public function tx_withdraw($param, $service_charge = '')
  130. {
  131. try {
  132. $payAisleData = Db::table('wa_pay_aisle')->where('characteristic', $param['pay_characteristic'])->first();
  133. if (empty($payAisleData)) {
  134. throw new \Exception('支付类型不存在!');
  135. }
  136. $payorderId = Db::table('wa_payorder')->insertGetId([
  137. 'goods_type' => 1001,
  138. 'user_id' => $param['user_data']['id'],
  139. 'order_no' => date('YmdHis') . mt_rand(1000, 9999),
  140. 'pay_characteristic' => $payAisleData->characteristic,
  141. 'pay_type' => $payAisleData->type,
  142. 'money' => bcmul($param['money'], $service_charge, 2),
  143. 'withdraw_money' => $param['money'],
  144. 'mold_type' => $param['mold'],
  145. 'created_at' => date('Y-m-d H:i:s'),
  146. 'updated_at' => date('Y-m-d H:i:s'),
  147. ]);
  148. } catch (\Throwable $exception) {
  149. throw new \Exception($exception->getMessage());
  150. }
  151. return $payorderId;
  152. }
  153. /** 购买原始股
  154. * @param $num
  155. * @param $user_id
  156. * @param $pay_characteristic
  157. * @return int
  158. * @throws \Exception
  159. */
  160. static public function orderAddInitialShare($num, $user_id, $pay_characteristic)
  161. {
  162. try {
  163. $wa_system = Db::table('wa_system')->first();
  164. $payAisleData = Db::table('wa_pay_aisle')->where('characteristic', $pay_characteristic)->first();
  165. if (empty($payAisleData)) {
  166. throw new \Exception('支付类型不存在!');
  167. }
  168. $payorderId = Db::table('wa_payorder')->insertGetId([
  169. 'goods_id' => 0,
  170. 'goods_type' => 10,
  171. 'user_id' => $user_id,
  172. 'order_no' => date('YmdHis') . mt_rand(1000, 9999),
  173. 'pay_characteristic' => $payAisleData->characteristic,
  174. 'pay_type' => $payAisleData->type,
  175. 'money' => bcmul($wa_system->money_five_value, $num, 2),
  176. 'num' => $num,
  177. 'created_at' => date('Y-m-d H:i:s'),
  178. 'updated_at' => date('Y-m-d H:i:s'),
  179. ]);
  180. } catch (\Throwable $exception) {
  181. throw new \Exception($exception->getMessage());
  182. }
  183. return $payorderId;
  184. }
  185. /** 提现回调
  186. * @param $data
  187. * @return void
  188. * @throws \Exception
  189. */
  190. static public function payorderJkjtx($data)
  191. {
  192. try {
  193. /** @var $bankCard 银行卡 */
  194. $bankCard = Db::table('wa_bank_card')->where('uid', $data['user_id'])->first();
  195. $withdrawId = Db::table('wa_withdraw')->insertGetId([
  196. 'order_no' => date('YmdHis') . mt_rand(10000, 99999),
  197. 'user_id' => $data['user_id'],
  198. 'money' => $data['withdraw_money'],
  199. 'type' => $data['mold_type'],
  200. 'affiliated_bank' => $bankCard->affiliated_bank,
  201. 'account_holder' => $bankCard->account_holder,
  202. 'card_number' => $bankCard->card_number,
  203. 'created_at' => date('Y-m-d H:i:s'),
  204. 'updated_at' => date('Y-m-d H:i:s'),
  205. ]);
  206. StreamBusiness::delStream($data['user_id'], $data['withdraw_money'], streamType4, $data['mold_type'], moldTypefild($data['mold_type']), $withdrawId);
  207. } catch (Throwable $exception) {
  208. throw new \Exception($exception->getMessage());
  209. }
  210. }
  211. /** 支付回调
  212. * @param $orderNo
  213. * @return void
  214. */
  215. static public function paymentCallback($orderNo)
  216. {
  217. try {
  218. $payorder = Db::table('wa_payorder')->where('order_no', $orderNo)->first();
  219. if ($payorder->is_pay != 1) {
  220. throw new \Exception('订单已处理!');
  221. }
  222. self::payorderSave(collect($payorder)->toArray());
  223. } catch (\Throwable $exception) {
  224. throw new \Exception($exception->getMessage());
  225. }
  226. return true;
  227. }
  228. /** 购买原始股权回调
  229. * @param $data
  230. * @return void
  231. */
  232. // static public function goodsStockRights($data)
  233. // {
  234. // try {
  235. // $goodsData=Db::table('wa_goods')->where('id',$data['goods_id'])->first();
  236. // $thisDay=date('Y-m-d H:i:s');
  237. // /** @var $futureDay 未来时间*/
  238. // $futureDay=futureDay(1095);
  239. //
  240. // $myGoodsId=Db::table('wa_my_goods')->insertGetId([
  241. // 'user_id' => $data['user_id'],
  242. // 'goods_id' => $goodsData->id,
  243. // 'money' => $data['money'],
  244. // 'on' => date('YmdHis').mt_rand(1000,9999),
  245. // 'type' => $goodsData->type,
  246. // 'pay_id' => $data['id'],
  247. // 'stock_rights' => $goodsData->stock_rights,
  248. // 'unit_price' => $goodsData->unit_price,
  249. // 'refund_amount' => $data['money'],
  250. // 'expiretime' => strtotime($futureDay),
  251. // 'expiredate' => $futureDay,
  252. // 'created_at' => $thisDay,
  253. // 'updated_at' => $thisDay
  254. // ]);
  255. //
  256. //
  257. //// StreamBusiness::addStream($data['user_id'],bcmul($goodsData->stock_rights,$goodsData->unit_price,2),streamType5,moldType4,moldTypefild(moldType4),$data['id']);
  258. //
  259. // $usersData=Db::table('wa_users')->where('id',$data['user_id'])->first();
  260. // $system=Db::table('wa_system')->first();
  261. // if(!empty($usersData->pid) && !empty($system->rebate)){
  262. // StreamBusiness::addStream($usersData->pid,$system->rebate,streamType10,moldType4,moldTypefild4,$data['id']);
  263. // }
  264. // if(!empty($usersData->ppid) && !empty($system->rebate_one)){
  265. // StreamBusiness::addStream($usersData->ppid,$system->rebate_one,streamType10,moldType4,moldTypefild4,$data['id']);
  266. // }
  267. //
  268. // if(!empty($usersData->toppid) && !empty($system->rebate_two)){
  269. // StreamBusiness::addStream($usersData->toppid,$system->rebate_two,streamType10,moldType4,moldTypefild4,$data['id']);
  270. // }
  271. //
  272. // /** 新增消费数据 */
  273. // Db::table('wa_users')->where('id',$data['user_id'])->increment('invest_money',$data['money']);
  274. //
  275. // Db::table('wa_goods')->where('id',$goodsData->id)->decrement('num',1);
  276. //
  277. // }catch (\Throwable $exception){
  278. // throw new \Exception($exception->getMessage());
  279. // }
  280. // }
  281. /** 订单回调处理
  282. * @param $data 订单信息
  283. * @return void
  284. */
  285. static public function payorderSave($data)
  286. {
  287. try {
  288. self::payorderGoods($data);
  289. Db::table('wa_payorder')->where('id', $data['id'])->update([
  290. 'is_pay' => 2,
  291. 'updated_at' => date('Y-m-d H:i:s'),
  292. ]);
  293. } catch (\Throwable $exception) {
  294. throw new \Exception($exception->getMessage());
  295. }
  296. return true;
  297. }
  298. /** 商品回调
  299. * @param $data
  300. * @return void
  301. * @throws \Exception
  302. */
  303. static public function payorderGoods($data)
  304. {
  305. try {
  306. $goodsData = Db::table('wa_goods')->where('id', $data['goods_id'])->first();
  307. $thisDay = date('Y-m-d H:i:s');
  308. /** @var $futureDay 未来时间 */
  309. $futureDay = futureDay(1095);
  310. $myGoodsId = Db::table('wa_my_goods')->insertGetId([
  311. 'user_id' => $data['user_id'],
  312. 'goods_id' => $goodsData->id,
  313. 'money' => $data['money'],
  314. 'num' => $data['num'],
  315. 'on' => date('YmdHis') . mt_rand(1000, 9999),
  316. 'type' => $goodsData->type,
  317. 'pay_id' => $data['id'],
  318. 'bonus' => $goodsData->bonus,
  319. 'refund_amount' => $data['money'],
  320. 'name' => Arr::get($data, 'name', null),
  321. 'mobile' => Arr::get($data, 'mobile', null),
  322. 'number' => Arr::get($data, 'number', null),
  323. 'address' => Arr::get($data, 'address', null),
  324. 'expiretime' => strtotime($futureDay),
  325. 'expiredate' => $futureDay,
  326. 'created_at' => $thisDay,
  327. 'updated_at' => $thisDay,
  328. ]);
  329. $userlist = Db::table('wa_users')->where('id', $data['user_id'])->first();
  330. // if (!empty($goodsData->bonus)) {
  331. // Db::table('wa_cron_task')->insert([
  332. // 'user_id' => $data['user_id'],
  333. // 'goods_id' => $goodsData->id,
  334. // 'order_id' => $data['id'],
  335. // 'money' => $data['money'],
  336. // 'bonus' => $goodsData->bonus,
  337. // 'goods_type' => $goodsData->type,
  338. // 'day_dividend_time' => strtotime(date('Y-m-d', strtotime('+1 days')) . ' 08:00:00'),
  339. // 'dividend_time' => strtotime($futureDay),
  340. // 'created_at' => $thisDay,
  341. // 'updated_at' => $thisDay,
  342. // 'my_good_id' => $myGoodsId,
  343. // ]);
  344. // StreamBusiness::addStream($data['user_id'], $goodsData->bonus, streamType9, moldType5, moldTypefild5, $data['id']);
  345. // }
  346. if (!empty($goodsData->balance)) {
  347. StreamBusiness::addStream($data['user_id'], $goodsData->balance, streamType9, moldType3, moldTypefild3, $data['id']);
  348. }
  349. /** 新增消费数据 */
  350. Db::table('wa_users')->where('id', $data['user_id'])->increment('invest_money', $data['money']);
  351. Db::table('wa_goods')->where('id', $goodsData->id)->decrement('num', $data['num']);
  352. /** 分佣 */
  353. // $user = Db::table('wa_users')->where('id', $data['user_id'])->first();
  354. // $system = Db::table('wa_system')->first();
  355. // if (!empty($user->pid) && !empty($system->rebate)) {
  356. // StreamBusiness::addStream($user->pid, bcmul($goodsData->pay_price, bcdiv($system->rebate, 100, 2), 2), streamType10, moldType7, moldTypefild7, $data['id']);
  357. // }
  358. // if (!empty($user->ppid) && !empty($system->rebate_one)) {
  359. // StreamBusiness::addStream($user->ppid, bcmul($goodsData->pay_price, bcdiv($system->rebate_one, 100, 2), 2), streamType10, moldType7, moldTypefild7, $data['id']);
  360. // }
  361. //
  362. // if (!empty($user->toppid) && !empty($system->rebate_two)) {
  363. // StreamBusiness::addStream($user->toppid, bcmul($goodsData->pay_price, bcdiv($system->rebate_two, 100, 2), 2), streamType10, moldType7, moldTypefild7, $data['id']);
  364. // }
  365. } catch (\Throwable $exception) {
  366. throw new \Exception($exception->getMessage());
  367. }
  368. }
  369. /** 国债订单回调处理
  370. * @param $data 订单信息
  371. * @return void
  372. */
  373. static public function CardpayorderSave($data)
  374. {
  375. try {
  376. $goodsData = Db::table('wa_goods')->where('id', $data['goods_id'])->first();
  377. $thisDay = date('Y-m-d H:i:s');
  378. /** @var $futureDay 未来时间 */
  379. $futureDay = futureDay(1095);
  380. $myGoodsId = Db::table('wa_my_goods')->insertGetId([
  381. 'user_id' => $data['user_id'],
  382. 'goods_id' => $goodsData->id,
  383. 'money' => $data['money'],
  384. 'num' => $data['num'],
  385. 'on' => date('YmdHis') . mt_rand(1000, 9999),
  386. 'type' => $goodsData->type,
  387. 'pay_id' => $data['id'],
  388. 'bonus' => $goodsData->bonus,
  389. 'refund_amount' => $data['money'],
  390. 'expiretime' => strtotime($futureDay),
  391. 'expiredate' => $futureDay,
  392. 'created_at' => $thisDay,
  393. 'updated_at' => $thisDay,
  394. ]);
  395. if (!empty($goodsData->bonus)) {
  396. Db::table('wa_cron_task')->insert([
  397. 'user_id' => $data['user_id'],
  398. 'goods_id' => $goodsData->id,
  399. 'order_id' => $data['id'],
  400. 'money' => $data['money'],
  401. 'bonus' => $goodsData->bonus,
  402. 'goods_type' => $goodsData->type,
  403. 'day_dividend_time' => strtotime(date('Y-m-d', strtotime('+1 days')) . ' 00:10:00'),
  404. 'dividend_time' => strtotime($futureDay),
  405. 'created_at' => $thisDay,
  406. 'updated_at' => $thisDay,
  407. 'my_good_id' => $myGoodsId,
  408. ]);
  409. StreamBusiness::addStream($data['user_id'], $goodsData->bonus, streamType14, moldType1, moldTypefild1, $data['id']);
  410. }
  411. StreamBusiness::delStream($data['user_id'], $data['money'], streamType17, moldType2, moldTypefild2, $data['id']);
  412. Db::table('wa_goods')->where('id', $goodsData->id)->decrement('num', $data['num']);
  413. } catch (\Throwable $exception) {
  414. throw new \Exception($exception->getMessage());
  415. }
  416. }
  417. }