Notify.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php namespace Api\Api;
  2. use Dever;
  3. use Api\Lib\Api;
  4. class Notify extends Api
  5. {
  6. public function common()
  7. {
  8. $file = file_get_contents("php://input");
  9. if ($file) {
  10. $file = json_decode($file, true);
  11. if ($file) {
  12. $input = array_merge($file, $input);
  13. }
  14. } else {
  15. $input = Dever::input();
  16. }
  17. Dever::log($input, 'notify');
  18. echo 11;die;
  19. //$param = '{"s":"132dcszvteLAx3r5eKrjarbGyJilBFECGu2F7mCX-H6K2_w","appid":"app483393213726","order_id":"Q202401040798155761553480","merchant_order_id":"yk18f2401040253000002","status":2,"cash":1000,"msg":"充值成功","actual_cash":1004.9999999999999,"time":1704308034101,"nonce":"fa0912fcfeb3ddfec4e821c26","signature":"d58295706e069e61a4a4ef9428ae0d155b36fae6"}';
  20. //$input = Dever::json_decode($param);
  21. $input = Dever::input();
  22. if (!isset($input['s'])) {
  23. $this->error('error');
  24. }
  25. $s = \Dever\Helper\Str::decode($input['s']);
  26. if ($s) {
  27. list($connect_id, $api_id, $order_id) = explode('|', $s);
  28. } elseif (isset($input['order_num'])) {
  29. $order_id = Dever::db('order', 'seller')->find(array('order_num' => $input['order_num']));
  30. if (!$order_id) {
  31. $this->error('error');
  32. }
  33. $channel = Dever::db('info', 'channel')->find($order_id['channel_id']);
  34. if (!$channel) {
  35. $this->error('order error');
  36. }
  37. $connect_id = $channel['connect_id'];
  38. $api = Dever::db('api', 'api')->find(array('connect_id' => $connect_id, 'type' => 1));
  39. if (!$api) {
  40. $this->error('order error');
  41. }
  42. $api_id = $api['id'];
  43. } else {
  44. $this->error('error');
  45. }
  46. $this->connect = Dever::db('info', 'api')->find($connect_id);
  47. $this->info = Dever::db('api', 'api')->find($api_id);
  48. unset($input['s']);
  49. unset($input['l']);
  50. if (!$input) {
  51. $input = file_get_contents("php://input");
  52. if ($input) {
  53. $input = json_decode($input, true);
  54. if (is_array($input)) {
  55. if (isset($input['s'])) {
  56. unset($input['s']);
  57. }
  58. if (isset($input['l'])) {
  59. unset($input['l']);
  60. }
  61. } else {
  62. $this->error('error');
  63. }
  64. }
  65. }
  66. if (!$input) {
  67. $this->error('error');
  68. }
  69. $this->load($input, $connect_id, $api_id, $order_id);
  70. }
  71. private function load($input, $connect_id, $api_id, $order_id)
  72. {
  73. //Dever::log($input, 'notify');
  74. if (is_array($order_id)) {
  75. $order = $order_id;
  76. } else {
  77. $order = Dever::db('order', 'seller')->find($order_id);
  78. }
  79. if (!$order || $order['status'] >= 10) {
  80. $this->error('order error');
  81. }
  82. $channel = Dever::db('info', 'channel')->find($order['channel_id']);
  83. if (!$channel) {
  84. $this->error('channel error');
  85. }
  86. $this->connect['appsecret'] = $channel['appsecret'];
  87. $code = Dever::db('api_notify_code', 'api')->select(array('api_id' => $api_id));
  88. $status = 0;
  89. if ($code) {
  90. foreach ($code as $k => $v) {
  91. if (isset($input[$v['key']]) && $input[$v['key']] == $v['value']) {
  92. $status = $v['type'];
  93. }
  94. }
  95. }
  96. $data = $input;
  97. $data['status'] = $status;
  98. $data = $this->service($data);
  99. $msg = '';
  100. if ($status == 1) {
  101. $msg = 'ok';
  102. } elseif ($status == 2) {
  103. $msg = 'error';
  104. }
  105. if ($this->info['notify_sign_col']) {
  106. $this->info['sign_col'] = $this->info['notify_sign_col'];
  107. }
  108. if ($sign = Dever::issets($input, $this->connect['sign_name'])) {
  109. unset($input[$this->connect['sign_name']]);
  110. unset($input['l']);
  111. /*
  112. $string = '';
  113. if ($sign != $this->sign($input, $string)) {
  114. $this->error('sign error');
  115. }*/
  116. }
  117. if ($msg) {
  118. $update = array();
  119. $update['official_msg'] = $data['official_msg'] ?? '';
  120. $update['official_order_num'] = $data['official_order_num'] ?? '';
  121. $update['channel_callback'] = json_encode($input, JSON_UNESCAPED_UNICODE);
  122. $update['channel_callback_date'] = time();
  123. if ($msg != 'ok') {
  124. $channel_num = Dever::db('channel', 'seller')->count(array('seller_id' => $order['seller_id'], 'goods_id' => $order['goods_id'], 'status' => 1));
  125. if ($channel_num > 1) {
  126. # 记录渠道错误信息
  127. $selected = array();
  128. $selected[$order['channel_id']] = true;
  129. $order_error_data = Dever::db('order_error', 'seller')->select(array('order_num' => $order['order_num']));
  130. if ($order_error_data) {
  131. foreach ($order_error_data as $k => $v) {
  132. $selected[$v['channel_id']] = true;
  133. }
  134. }
  135. $num = count($selected);
  136. $channel_num = $channel_num - $num;
  137. if ($channel_num > 0) {
  138. $order_error = array();
  139. $order_error['order_num'] = $order['order_num'];
  140. $order_error['buy_price'] = $order['buy_price'];
  141. $order_error['channel_id'] = $order['channel_id'];
  142. $order_error['channel_goods_id'] = $order['channel_goods_id'];
  143. $order_error['channel_goods_discount'] = $order['channel_goods_discount'];
  144. $order_error['channel_order_date'] = $order['channel_order_date'];
  145. $order_error['channel_order_num'] = $order['channel_order_num'];
  146. $order_error['channel_request'] = $order['channel_request'];
  147. $order_error['channel_response'] = $order['channel_response'];
  148. $order_error['channel_callback'] = $update['channel_callback'];
  149. $order_error['channel_callback_date'] = $update['channel_callback_date'];
  150. Dever::db('order_error', 'seller')->insert($order_error);
  151. Dever::load('order', 'seller')->handleAct($order, $selected);
  152. } else {
  153. Dever::load('order', 'seller')->notify($order, $msg, $update);
  154. }
  155. } else {
  156. Dever::load('order', 'seller')->notify($order, $msg, $update);
  157. }
  158. } else {
  159. Dever::load('order', 'seller')->notify($order, $msg, $update);
  160. }
  161. echo $this->info['notify_success'];die;
  162. }
  163. $this->error('error');
  164. }
  165. private function error($msg)
  166. {
  167. if ($this->info && $this->info['notify_error']) {
  168. if ($this->info['notify_error'] == 500) {
  169. header("HTTP/1.1 500 Internal Server Error");
  170. header("Status: 500 Internal Server Error");
  171. } else {
  172. echo $this->info['notify_error'];die;
  173. }
  174. }
  175. echo $msg;die;
  176. }
  177. # 生成回调
  178. public function callback()
  179. {
  180. //$param = '{"l":"notify.callback","t":"4-5-user_order","userid":"41438","order_id":"24020213181441438abbf5b39f","account":"15810090811","amount":"100","price":"104.1","state":"failed","user_order":"C2024020251094338265393","sign":"0447CC15F7D0218A1C5C09160A12A19A","voucher":""}';
  181. //$input = Dever::json_decode($param);
  182. $input = Dever::input();
  183. if (!isset($input['t'])) {
  184. $this->error('error');
  185. }
  186. $file = file_get_contents("php://input");
  187. if ($file) {
  188. $file = json_decode($file, true);
  189. if ($file) {
  190. $input = array_merge($file, $input);
  191. }
  192. }
  193. $temp = explode('-', $input['t']);
  194. $connect_id = $temp[0];
  195. $api_id = $temp[1];
  196. $order_key = $temp[2];
  197. $this->connect = Dever::db('info', 'api')->find($connect_id);
  198. $this->info = Dever::db('api', 'api')->find($api_id);
  199. if (!isset($input[$order_key])) {
  200. $this->error('error');
  201. }
  202. $order_id = $input[$order_key];
  203. unset($input['t']);
  204. $order = Dever::db('order', 'seller')->find(array('order_num' => $order_id));
  205. if (!$order || $order['status'] >= 10) {
  206. $this->error('order error');
  207. }
  208. $this->load($input, $connect_id, $api_id, $order);
  209. }
  210. }