RefillPhone.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace refill\baidu;
  3. require_once(BASE_HELPER_RAPI_PATH . '/baidu/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['mch_order_id'] = $order_sn;
  17. $params['mchid'] = config::MCHID;
  18. $params['price'] = $amount;
  19. $params['notify'] = config::NOTIFY_URL;
  20. $params['teltype'] = $this->phone_type($phone);
  21. $params['timeout'] = 50;
  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. $rand = mt_rand(0,1);
  36. return [$rand , '',false];
  37. }
  38. }
  39. public function query($refill_info)
  40. {
  41. $params['mch_order_id'] = $refill_info['order_sn'];
  42. $params['mchid'] = config::MCHID;
  43. $content = $params['mchid'] . $params['mch_order_id'] . config::KEY;
  44. $params['sign'] = md5($content);
  45. $resp = http_request(config::QUERY_URL, $params , 'POST' , false);
  46. if ($resp === false) {
  47. return [false, '系统错误'];
  48. } else {
  49. $rand = mt_rand(0,1);
  50. // Log::record("query return rand : $resp", Log::DEBUG);
  51. return [$rand , ''];
  52. }
  53. }
  54. private function sign($params)
  55. {
  56. $key = config::KEY;
  57. $content = $params['mchid'] . $params['tel'] . $params['mch_order_id'] . $params['price'] . $params['teltype'] . $params['timeout'] . $params['notify'];
  58. $content .= $params['time'] . $params['rand'] . $key;
  59. return md5($content);
  60. }
  61. private function phone_type($phone)
  62. {
  63. $card_type = mtopcard\card_type($phone);
  64. if ($card_type == mtopcard\ChinaMobileCard) {
  65. return 2;
  66. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  67. return 1;
  68. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  69. return 3;
  70. }
  71. }
  72. }