PayLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\Http\helper\CreateOrder;
  4. use App\Http\helper\HttpCurl;
  5. use App\Models\AgentUser;
  6. use App\Models\Order;
  7. use App\Models\RecordOperateLog;
  8. use App\Models\RecordScoreInfo;
  9. use App\Services\FirstRechargeGifts;
  10. use App\Services\Pay;
  11. use Illuminate\Support\Facades\DB;
  12. use Yansongda\Pay\Log;
  13. class PayLogic extends BaseApiLogic
  14. {
  15. protected $pay_bankcode = 924;
  16. public function pay($userId, $pay_amount, $pay_productname,$GiftsID)
  17. {
  18. // 验证支付金额
  19. if ($pay_amount < 0) {
  20. $this->error = '非法支付金额';
  21. return false;
  22. }
  23. $order_sn = CreateOrder::order_sn($userId);
  24. // 购买礼包
  25. $giftService = new FirstRechargeGifts();
  26. $bool = $giftService->buyGifts($GiftsID, $userId);
  27. if ($bool === false) {
  28. $this->error = $giftService->getError();
  29. return false;
  30. }
  31. $now = date('Y-m-d H:i:s');
  32. $config = config('pay.pay_api');
  33. $service = new Pay();
  34. $service->setPayAmount($pay_amount);
  35. $service->setPayNotifyurl($config['service_notify']); // 回调地址 - 异步
  36. $service->setPayCallbackurl($config['web_notify']); // 回调地址 - 同步
  37. $service->setPayOrderid($order_sn); // 订单ID
  38. $service->setPayApplydate($now);
  39. $service->setPayBankcode($this->pay_bankcode);
  40. $data = $service->allData();
  41. $sign = $service->sign($data);
  42. $create_data = $data;
  43. $create_data['pay_productname'] = $pay_productname;
  44. // 生成订单信息
  45. $logic = new OrderLogic();
  46. $create_data['pay_amount'] = $pay_amount * 100; // 元转化分
  47. $create_order = $logic->create($create_data, $userId, 0);
  48. $data['pay_md5sign'] = $sign;
  49. $result = $service->curlPost($data, 5, 'array');
  50. $to_arr = json_decode($result, true);
  51. if (isset($to_arr['status']) && $to_arr['status'] == 'error') {
  52. $this->error = $to_arr['msg'] ?? '';
  53. return false;
  54. }
  55. return $result;
  56. }
  57. // 异步回调
  58. public function notify($data)
  59. {
  60. try {
  61. $custOrderNo = $data['orderid'];
  62. $sign = $data['sign'];
  63. unset($data['sign']);
  64. $service = new Pay();
  65. // 查询订单信息
  66. $order = Order::where('order_sn', $custOrderNo)->first();
  67. if (!$order) {
  68. Log::info('订单不存在2');
  69. return 'fail';
  70. }
  71. if (!empty($order['pay_at']) || !empty($order['finished_at'])) {
  72. Log::info('订单已支付2');
  73. return 'fail';
  74. }
  75. // 验证签名
  76. $verify_sign = $service->sign($data);
  77. if ($verify_sign != strtoupper($sign)) {
  78. Log::info('签名错误2');
  79. return 'fail';
  80. }
  81. $body = [
  82. 'payment_sn' => $data['transaction_id'],
  83. 'updated_at' => date('Y-m-d H:i:s'),
  84. ];
  85. $apply_data = [
  86. 'order_id' => $order['id'],
  87. 'payment_sn' => $data['transaction_id'],
  88. 'payment_code' => 'SerPayMent',
  89. 'return' => \GuzzleHttp\json_encode($data),
  90. 'is_error' => 0,
  91. 'created_at' => date('Y-m-d H:i:s'),
  92. 'updated_at' => date('Y-m-d H:i:s'),
  93. ];
  94. $ordStatus = $data['returncode'];
  95. // 说明:除了成功,失败和退款状态,都可以认为是处理中
  96. switch ($ordStatus) {
  97. case "00": // 支付成功
  98. $body['pay_status'] = 1;
  99. $body['pay_at'] = date('Y-m-d H:i:s');
  100. $body['finished_at'] = date('Y-m-d H:i:s');
  101. // 判断是不是首冲礼包
  102. $GiftsID = $order->GiftsID ?? '';
  103. if (!empty($GiftsID)) {
  104. $GiftsData = [
  105. 'UserID' => $order->user_id,
  106. 'GiftsID' => $GiftsID,
  107. 'BuyCount' => 1,
  108. 'LastBuyDate' => date('Y-m-d H:i:s')
  109. ];
  110. // 卖出去数量 +1
  111. DB::table('QPAccountsDB.dbo.FirstRechargeGifts')->where('GiftsID', $GiftsID)->increment('TotalCount');
  112. $userGifts = DB::table('QPAccountsDB.dbo.UserFirstRechargeGifts')->where('UserID', $order->user_id)->where('GiftsID', $GiftsID)->first();
  113. if ($userGifts) {
  114. DB::table('QPAccountsDB.dbo.UserFirstRechargeGifts')->where('UserID', $order->user_id)->where('GiftsID', $GiftsID)->increment('BuyCount');
  115. } else {
  116. DB::table('QPAccountsDB.dbo.UserFirstRechargeGifts')->insert($GiftsData);
  117. }
  118. $Gifts = DB::table('QPAccountsDB.dbo.FirstRechargeGifts')->where('GiftsID', $GiftsID)->first();
  119. $favorable_price = $Gifts->AllScore;
  120. $give = $Gifts->ExtraScore;
  121. $Recharge = $Gifts->Price;
  122. $czReason = 50;
  123. $cjReason = 51;
  124. } else {
  125. // 查找送的金额
  126. $recharge_gear = DB::table('agent.dbo.recharge_gear')->where('money', $data['amount'])->select('favorable_price', 'give')->first();
  127. $Recharge = $recharge_gear->favorable_price;
  128. $give = $recharge_gear->give;
  129. $favorable_price = $Recharge + $give;
  130. $czReason = 1;
  131. $cjReason = 45;
  132. }
  133. // 发送邮件给玩家
  134. $recharge = $favorable_price * 100;
  135. $bonus = '30000,' . $recharge;
  136. $data = [
  137. 'MailType' => 2,
  138. 'MailStatus' => 1,
  139. 'UserID' => (int)$order->user_id,
  140. 'CreateTime' => date('Y-m-d H:i:s', time()),
  141. 'TitleString' => 'This is the chip coins you get from Recharge',
  142. 'TextString' => "Order number:{$order->order_sn}",
  143. 'BonusString' => '',
  144. 'type' => 1,
  145. 'order_sn' => $order->order_sn,
  146. 'amount' => $favorable_price
  147. ];
  148. $result = DB::table('QPAccountsDB.dbo.PrivateMail')->insert($data);
  149. // 增加充值记录
  150. $query = DB::table('QPAccountsDB.dbo.YN_VIPAccount')
  151. ->where('UserID', $order->user_id)
  152. ->value('Recharge');
  153. if ($query) {
  154. DB::table('QPAccountsDB.dbo.YN_VIPAccount')
  155. ->where('UserID', $order->user_id)
  156. ->increment('Recharge',$data['amount']);
  157. } else {
  158. DB::table('QPAccountsDB.dbo.YN_VIPAccount')
  159. ->where('UserID', $order->user_id)
  160. ->insert(['Recharge' => $data['amount'], 'UserID' => $order->user_id]);
  161. }
  162. // 增加用户金币变化记录
  163. RecordScoreInfo::addScore($order->user_id, ($Recharge * 100), $czReason); #充值
  164. RecordScoreInfo::addScore($order->user_id, ($give * 100), $cjReason); #赠送彩金
  165. // 充值推广佣金
  166. (new AgentUser())->reward($order->user_id, $data['amount'], $custOrderNo);
  167. // 增加用户金币
  168. $firstScore = DB::table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $order->user_id)->value('Score');
  169. $Score = $recharge + $firstScore;
  170. Log::info('变化金币' . $Score);
  171. DB::table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $order->user_id)->increment('Score',$recharge);
  172. // 服务器通知
  173. $url = config('transfer.stock')['url'] . 'notifyPay';
  174. $data = [
  175. 'userid' => $order->user_id,
  176. 'getScore' => $recharge,
  177. 'score' => $Score
  178. ];
  179. (new HttpCurl())->service($url, $data);
  180. // 埋点记录 -- 支付成功
  181. RecordOperateLog::add_log(10222, $order->user_id, 0);
  182. // 埋点记录 -- 支付卢比
  183. RecordOperateLog::add_log(30102, $order->user_id, $recharge);
  184. break;
  185. default : // 失败
  186. $body['pay_status'] = 2;
  187. $apply_data['is_error'] = 1;
  188. break;
  189. }
  190. $order_up = DB::table('agent.dbo.order')
  191. ->where('order_sn', $custOrderNo)
  192. ->update($body);
  193. $apply = DB::table('agent.dbo.payment_apply')
  194. ->insert($apply_data);
  195. if (($order_up && $apply) != true) {
  196. Log::info('订单更新失败2');
  197. return 'fail';
  198. }
  199. Log::info('success2');
  200. exit("ok");
  201. } catch (\Exception $exception) {
  202. $this->error = $exception->getMessage();
  203. return 'false';
  204. }
  205. }
  206. // 同步回调
  207. public function sync_notify($data)
  208. {
  209. $custOrderNo = $data['orderid'];
  210. $sign = $data['sign'];
  211. unset($data['sign']);
  212. $service = new Pay();
  213. // 查询订单信息
  214. $order = Order::where('order_sn', $custOrderNo)->first();
  215. if (!$order) {
  216. Log::info('同步回调:订单不存在2');
  217. return 'fail';
  218. }
  219. if (!empty($order['pay_at']) || !empty($order['finished_at'])) {
  220. Log::info('同步回调:订单已支付2');
  221. return 'fail';
  222. }
  223. // 验证签名
  224. $verify_sign = $service->sign($data);
  225. if ($verify_sign != strtolower($sign)) {
  226. Log::info('同步回调:签名错误2');
  227. return 'fail';
  228. }
  229. if ($data["returncode"] == "00") {
  230. $str = "交易成功!订单号:" . $data["orderid"];
  231. exit($str);
  232. } else {
  233. exit('Fail');
  234. }
  235. }
  236. }