RefillPhone.php 2.5 KB

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