DoiAMPay.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Utils;
  3. /**
  4. * Made By Leo
  5. * 黛米付接口
  6. * 2.1.4版本
  7. * 适用于 V1 API 黛米付面板V1
  8. * copyright all rights reserved
  9. */
  10. use App\Services\View;
  11. use App\Services\Auth;
  12. use App\Services\Config;
  13. use App\Models\User;
  14. use App\Models\Code;
  15. use App\Models\Paylist;
  16. use App\Models\Payback;
  17. class DoiAMPay
  18. {
  19. public static function render()
  20. {
  21. return View::getSmarty()->assign('enabled', Config::get('doiampay')['enabled'])->fetch('user/doiam.tpl');
  22. }
  23. public function handle($request, $response, $args)
  24. {
  25. $type = $request->getParam('type');
  26. $price = $request->getParam('price');
  27. if (Config::get('doiampay')['enabled'][$type] == 0) {
  28. return json_encode(['errcode' => -1, 'errmsg' => '非法的支付方式.']);
  29. }
  30. if ($price <= 0) {
  31. return json_encode(['errcode' => -1, 'errmsg' => '非法的金额.']);
  32. }
  33. $user = Auth::getUser();
  34. $settings = Config::get('doiampay')['mchdata'][$type];
  35. $pl = new Paylist();
  36. $pl->userid = $user->id;
  37. $pl->total = $price;
  38. $pl->save();
  39. $data = [
  40. 'trade' => $pl->id,
  41. 'price' => $price,
  42. 'phone' => $settings['phone'],
  43. 'mchid' => $settings['mchid'],
  44. 'subject' => Config::get('appName') . '充值' . $price . '元',
  45. 'body' => Config::get('appName') . '充值' . $price . '元',
  46. 'type' => 'Mod',
  47. ];
  48. $data = DoiAM::sign($data, $settings['token']);
  49. $ret = DoiAM::post('https://api.daimiyun.cn/v2/' . $type . '/create', $data);
  50. $result = json_decode($ret, true);
  51. if ($result and $result['errcode'] == 0) {
  52. $result['pid'] = $pl->id;
  53. return json_Encode($result);
  54. }
  55. return json_encode([
  56. 'errcode' => -1,
  57. 'errmsg' => '接口调用失败!' . $ret,
  58. ]);
  59. }
  60. public function status($request, $response, $args)
  61. {
  62. return json_encode(Paylist::find($_POST['pid']));
  63. }
  64. public function handle_return($request, $response, $args)
  65. {
  66. $money = $_GET['money'];
  67. echo "您已经成功支付 $money 元,正在跳转..";
  68. echo <<<HTML
  69. <script>
  70. location.href="/user/code";
  71. </script>
  72. HTML;
  73. }
  74. public function handle_callback($request, $response, $args)
  75. {
  76. $order_data = $_POST;
  77. $status = $order_data['status']; //获取传递过来的交易状态
  78. $invoiceid = $order_data['out_trade_no']; //订单号
  79. $transid = $order_data['trade_no']; //转账交易号
  80. $amount = $order_data['money']; //获取递过来的总价格
  81. if (!DoiAM::checksign($_POST, Config::get('doiampay')['mchdata'][$args['type']]['token'])) {
  82. return json_encode(array('errcode' => 2333));
  83. }
  84. if ($status == 'success') {
  85. $p = Paylist::find($invoiceid);
  86. if ($p->status == 1) {
  87. return json_encode(['errcode' => 0]);
  88. }
  89. $p->status = 1;
  90. $p->save();
  91. $user = User::find($p->userid);
  92. $user->money += $p->total;
  93. $user->save();
  94. $codeq = new Code();
  95. $codeq->code = ['wepay' => '微信', 'qqpay' => 'QQ支付', 'alipay' => '支付宝'][$args['type']] . '充值';
  96. $codeq->isused = 1;
  97. $codeq->type = -1;
  98. $codeq->number = $p->total;
  99. $codeq->usedatetime = date('Y-m-d H:i:s');
  100. $codeq->userid = $user->id;
  101. $codeq->save();
  102. if ($user->ref_by != '' && $user->ref_by != 0 && $user->ref_by != null) {
  103. $gift_user = User::where('id', '=', $user->ref_by)->first();
  104. $gift_user->money += ($codeq->number * (Config::get('code_payback') / 100));
  105. $gift_user->save();
  106. $Payback = new Payback();
  107. $Payback->total = $codeq->number;
  108. $Payback->userid = $user->id;
  109. $Payback->ref_by = $user->ref_by;
  110. $Payback->ref_get = $codeq->number * (Config::get('code_payback') / 100);
  111. $Payback->datetime = time();
  112. $Payback->save();
  113. }
  114. return json_encode(['errcode' => 0]);
  115. }
  116. return '';
  117. }
  118. }
  119. class DoiAM
  120. {
  121. public static function sort(&$array)
  122. {
  123. ksort($array);
  124. }
  125. public static function getsign($array, $key)
  126. {
  127. unset($array['sign']);
  128. self::sort($array);
  129. $sss = http_build_query($array);
  130. $sign = hash('sha256', $sss . $key);
  131. $sign = sha1($sign . hash('sha256', $key));
  132. return $sign;
  133. }
  134. public static function sign($array, $key)
  135. {
  136. $array['sign'] = self::getSign($array, $key);
  137. return $array;
  138. }
  139. public static function checksign($array, $key)
  140. {
  141. $new = $array;
  142. $new = self::sign($new, $key);
  143. if (!isset($array['sign'])) {
  144. return false;
  145. }
  146. return $array['sign'] == $new['sign'];
  147. }
  148. public static function post($url, $data = null)
  149. {
  150. $curl = curl_init();
  151. curl_setopt($curl, CURLOPT_URL, $url);
  152. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  153. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  154. if (!empty($data)) {
  155. curl_setopt($curl, CURLOPT_POST, 1);
  156. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  157. }
  158. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  159. $output = curl_exec($curl);
  160. curl_close($curl);
  161. return $output;
  162. }
  163. }