DoiAMPay.php 4.6 KB

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