RefillPhone.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace refill\tianx;
  3. require_once(BASE_HELPER_RAPI_PATH . '/tianx/config.php');
  4. use refill;
  5. use Log;
  6. use mtopcard;
  7. class RefillPhone extends refill\IRefillPhone
  8. {
  9. public function __construct($cfgs)
  10. {
  11. parent::__construct($cfgs);
  12. }
  13. private function req_params(int $phone, int $amount, string $order_sn)
  14. {
  15. $params['tel'] = $phone;
  16. $params['price'] = $order_sn;
  17. $params['mchid'] = config::MCHID;
  18. $params['orderid'] = $amount;
  19. $params['notify'] = config::NOTIFY_URL;
  20. $params['teltype'] = $this->phone_type($phone);
  21. $params['timeout'] = 180;
  22. $params['time'] = time();
  23. $params['rand'] = rand(100000,999999);
  24. return $params;
  25. }
  26. public function add($card_no, $card_type, $amount, $params)
  27. {
  28. $params = $this->req_params($card_no, $amount, $params['order_sn']);
  29. $sign = $this->sign($params);
  30. $params['sign'] = $sign;
  31. $resp = http_request(config::ORDER_URL, $params , 'POST' , false , config::ExtHeaders);
  32. if ($resp === false) {
  33. return [false, '系统错误'];
  34. } else {
  35. Log::record($resp, Log::DEBUG);
  36. $resp = json_decode($resp, true);
  37. if ($resp['code'] == 0) {
  38. return [true, $resp['order_id']];
  39. } else {
  40. return [false, $resp['msg']];
  41. }
  42. }
  43. }
  44. public function query($refill_info)
  45. {
  46. $params['orderid'] = $refill_info['order_sn'];
  47. $params['mchid'] = config::MCHID;
  48. $content = $params['mchid'] . $params['orderid'] . config::KEY;
  49. $params['sign'] = md5($content);
  50. $resp = http_request(config::QUERY_URL, $params , 'POST' , false , config::ExtHeaders);
  51. if ($resp === false) {
  52. return [false, '系统错误'];
  53. } else {
  54. Log::record($resp, Log::DEBUG);
  55. $resp = json_decode($resp, true);
  56. if ($resp['code'] == 100) {
  57. $order_state = '';
  58. if ($resp['status'] == 3) {
  59. $order_state = ORDER_STATE_SUCCESS;
  60. } elseif ($resp['status'] == 4) {
  61. $order_state = ORDER_STATE_CANCEL;
  62. } elseif ($resp['status'] == 2){
  63. $order_state = ORDER_STATE_SEND;
  64. }
  65. if (empty($order_state)) {
  66. return [false, $resp['data']];
  67. }
  68. return [true, $order_state];
  69. } else {
  70. return [false, $resp['msg']];
  71. }
  72. }
  73. }
  74. private function sign($params)
  75. {
  76. $key = config::KEY;
  77. $content = $params['mchid'] . $params['tel'] . $params['price'] . $params['orderid'] . $params['teltype'] . $params['timeout'] . $params['notify'];
  78. $content .= $params['time'] . $params['rand'] . $key;
  79. return md5($content);
  80. }
  81. private function phone_type($phone)
  82. {
  83. $card_type = mtopcard\card_type($phone);
  84. if ($card_type == mtopcard\ChinaMobileCard) {
  85. return 0;
  86. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  87. return 1;
  88. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  89. return 2;
  90. }
  91. }
  92. }