RefillPhone.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace refill\amingjd;
  3. require_once(BASE_HELPER_RAPI_PATH . '/amingjd/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['mchid'] = config::MCHID;
  16. $params['tel'] = $phone;
  17. $params['orderid'] = $order_sn;
  18. $params['price'] = $amount;
  19. $params['teltype'] = $this->phone_type($phone);
  20. $params['timeout'] = config::TIMEOUT;
  21. $params['notify'] = config::NOTIFY_URL;
  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);
  32. if ($resp === false) {
  33. return [false, '系统错误', true];
  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'], false];
  39. } else {
  40. return [false, $resp['msg'], false];
  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);
  51. if ($resp === false) {
  52. return [false, '系统错误'];
  53. }
  54. else
  55. {
  56. Log::record($resp, Log::DEBUG);
  57. $resp = json_decode($resp, true);
  58. if ($resp['code'] == 100)
  59. {
  60. $order_state = -1;
  61. if ($resp['status'] == 3) {
  62. $order_state = ORDER_STATE_SUCCESS;
  63. $save['official_sn'] = strtolower($resp['out_order_id']) == 'null' ? '' : $resp['out_order_id'];
  64. Model('refill_order')->edit($refill_info['order_id'], $save);
  65. } elseif ($resp['status'] == 4) {
  66. $order_state = ORDER_STATE_CANCEL;
  67. } elseif (in_array($resp['status'] , [1 , 2])) {
  68. $order_state = ORDER_STATE_SEND;
  69. }
  70. if ($order_state == -1) {
  71. return [false, $resp['msg']];
  72. }
  73. return [true, $order_state];
  74. } else {
  75. return [false, $resp['msg']];
  76. }
  77. }
  78. }
  79. private function sign($params)
  80. {
  81. $content = $params['mchid'] . $params['tel'] . $params['price'] . $params['orderid'] . $params['teltype'] . $params['timeout'] . $params['notify'];
  82. $content .= $params['time'] . $params['rand'] . config::KEY;
  83. return md5($content);
  84. }
  85. private function phone_type($phone)
  86. {
  87. $card_type = mtopcard\card_type($phone);
  88. if ($card_type == mtopcard\ChinaMobileCard) {
  89. return 0;
  90. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  91. return 1;
  92. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  93. return 2;
  94. }
  95. }
  96. }