RefillPhone.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace refill\dinghui;
  3. require_once(BASE_HELPER_RAPI_PATH . '/dinghui/config.php');
  4. use refill;
  5. use Log;
  6. class RefillPhone extends refill\IRefillPhone
  7. {
  8. public function __construct($cfgs)
  9. {
  10. parent::__construct($cfgs);
  11. }
  12. //统一请求
  13. private function url_request($message, $method)
  14. {
  15. $params['msgtype'] = 'request_msg';
  16. $params['format'] = 'json';
  17. $params['version'] = '1.0';
  18. $params['app_id'] = config::APP_ID;
  19. $params['timestamp'] = $this->getMillisecond();
  20. $params['method'] = $method;
  21. $params['channel'] = 'wap';
  22. $params['request_id'] = "REQ".$params['timestamp'];
  23. $params['message'] = $message;
  24. $sign = $this->sign($params);
  25. $params['sign'] = $sign;
  26. $url = config::API_URL;
  27. return http_request($url, $params, 'POST', false, []);
  28. }
  29. private function req_params(int $phone, int $card_type, int $amount, string $order_sn)
  30. {
  31. $params['goods_sku'] = config::SKU[$card_type][$amount];
  32. $params['app_order_no'] = $order_sn;
  33. $params['goods_count'] = 1;
  34. $charge_params = [];
  35. $charge_params = (object)$charge_params;
  36. $charge_params->charge_account_number = "{$phone}";
  37. $charge_params->charge_type = 0;
  38. $charge_params->charge_account_type = 'MOBILE';
  39. $charge_params->charge_account_os_type = '';
  40. $params['charge_query_info'] = $charge_params;
  41. $params['notify_url'] = config::NOTIFY_URL;
  42. return $params;
  43. }
  44. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  45. {
  46. $params = $this->req_params($card_no, $card_type, $amount, $params['order_sn']);
  47. $message = $this->pub_message($params);
  48. $req['msgtype'] = 'request_msg';
  49. $req['format'] = 'json';
  50. $req['version'] = '1.0';
  51. $req['app_id'] = config::APP_ID;
  52. $req['timestamp'] = $this->getMillisecond();
  53. $req['method'] = 'order.charge.query';
  54. $req['channel'] = 'wap';
  55. $req['request_id'] = "REQ".$params['timestamp'];
  56. $req['message'] = $message;
  57. $sign = $this->sign($req);
  58. $req['sign'] = $sign;
  59. $resp = http_request(config::API_URL, $req, 'POST', false, [], $net_errno);
  60. if (empty($resp)) {
  61. return [false, '系统错误', true];
  62. }
  63. else
  64. {
  65. Log::record($resp, Log::DEBUG);
  66. $resp = json_decode($resp, true);
  67. if (empty($resp)) {
  68. return [false, '系统错误', true];
  69. } elseif ($resp['code'] === '000000') {
  70. return [true, $resp['result']['trade_no'], false];
  71. } else {
  72. return [false, $resp['msg'], false];
  73. }
  74. }
  75. }
  76. public function query($refill_info)
  77. {
  78. $params['app_order_no'] = $refill_info['order_sn'];
  79. $message = $this->pub_message($params);
  80. $resp = $this->url_request($message,'order.charge.query');
  81. if (empty($resp)) {
  82. return [false, '系统错误'];
  83. }
  84. else
  85. {
  86. Log::record($resp, Log::DEBUG);
  87. $resp = json_decode($resp, true);
  88. if (empty($resp)) {
  89. return [false, '系统错误'];
  90. }
  91. elseif ($resp['code'] === '000000')
  92. {
  93. $status = $resp['result']['charge_result'];
  94. if ($status === 'SUCCESS') {
  95. $order_state = ORDER_STATE_SUCCESS;
  96. } elseif ($status === 'FAILED') {
  97. $order_state = ORDER_STATE_CANCEL;
  98. } elseif ($status === 'WAITTING') {
  99. $order_state = ORDER_STATE_SEND;
  100. } else {
  101. return [false, $resp['msg']];
  102. }
  103. return [true, $order_state];
  104. }
  105. else {
  106. return [false, $resp['msg']];
  107. }
  108. }
  109. }
  110. private function sign($params)
  111. {
  112. $content = '';
  113. ksort($params);
  114. foreach ($params as $key => $val){
  115. $content .= "{$key}={$val}&";
  116. }
  117. $content = rtrim($content,'&');
  118. $res = openssl_pkey_get_private(config::PRIVATE_KEY);
  119. openssl_sign($content, $sign, $res);
  120. return bin2hex($sign);
  121. }
  122. private function getMillisecond() {
  123. list($t1, $t2) = explode(' ', microtime());
  124. return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
  125. }
  126. private function pub_message($params): string
  127. {
  128. $encrypt = json_encode($params, JSON_UNESCAPED_SLASHES);
  129. return openssl_encrypt($encrypt, 'AES-256-ECB', config::AES_KEY, 0, '');
  130. }
  131. }