RefillPhone.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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'] = 300;
  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. } else {
  54. Log::record($resp, Log::DEBUG);
  55. $resp = json_decode($resp, true);
  56. if ($resp['code'] == 100) {
  57. $order_state = -1;
  58. if ($resp['status'] == 3) {
  59. $order_state = ORDER_STATE_SUCCESS;
  60. $save['official_sn'] = strtolower($resp['out_order_id']) == 'null' ? '' : $resp['out_order_id'];
  61. Model('refill_order')->edit($refill_info['order_id'], $save);
  62. } elseif ($resp['status'] == 4) {
  63. $order_state = ORDER_STATE_CANCEL;
  64. } elseif (in_array($resp['status'] , [1 , 2])) {
  65. $order_state = ORDER_STATE_CANCEL;
  66. }
  67. if ($order_state == -1) {
  68. return [false, $resp['msg']];
  69. }
  70. return [true, $order_state];
  71. } else {
  72. return [false, $resp['msg']];
  73. }
  74. }
  75. }
  76. private function sign($params)
  77. {
  78. $content = $params['mchid'] . $params['tel'] . $params['price'] . $params['orderid'] . $params['teltype'] . $params['timeout'] . $params['notify'];
  79. $content .= $params['time'] . $params['rand'] . config::KEY;
  80. return md5($content);
  81. }
  82. private function phone_type($phone)
  83. {
  84. $card_type = mtopcard\card_type($phone);
  85. if ($card_type == mtopcard\ChinaMobileCard) {
  86. return 0;
  87. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  88. return 1;
  89. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  90. return 2;
  91. }
  92. }
  93. }