RefillPhone.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace refill\afand;
  3. require_once(BASE_HELPER_RAPI_PATH . '/afand/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. //[$state, $errmsg,$neterr]
  27. public function add($card_no, $card_type, $amount, $params)
  28. {
  29. $params = $this->req_params($card_no, $amount, $params['order_sn']);
  30. $sign = $this->sign($params);
  31. $params['sign'] = $sign;
  32. $resp = http_request(config::ORDER_URL, $params , 'POST' , false);
  33. if ($resp === false) {
  34. return [false, '系统错误', true];
  35. }
  36. else
  37. {
  38. Log::record($resp, Log::DEBUG);
  39. $resp = json_decode($resp, true);
  40. if ($resp === false) {
  41. return [false, '系统错误', true];
  42. } elseif ($resp['code'] == 0) {
  43. return [true, $resp['pt_order_id'], false];
  44. } else {
  45. return [false, $resp['msg'], false];
  46. }
  47. }
  48. }
  49. public function query($refill_info)
  50. {
  51. $params['mch_order_id'] = $refill_info['order_sn'];
  52. $params['mchid'] = config::MCHID;
  53. $params['pt_order_id'] = $refill_info['ch_trade_no'];
  54. $params['mch_order_id'] = $refill_info['order_sn'];
  55. $params['op_order_id'] = $refill_info['official_sn'];
  56. $params['tel'] = $refill_info['card_no'];
  57. $params['time'] = time();
  58. $params['rand'] = rand(100000,999999);
  59. $content = $params['mchid'] . $params['pt_order_id'] . $params['mch_order_id'] . $params['op_order_id'] . $params['tel'] . $params['time'] . $params['rand'] . config::KEY;
  60. $params['sign'] = md5($content);
  61. $resp = http_request(config::QUERY_URL, $params , 'POST' , false);
  62. if ($resp === false) {
  63. return [false, '系统错误'];
  64. } else {
  65. Log::record($resp, Log::DEBUG);
  66. $resp = json_decode($resp, true);
  67. if ($resp['code'] == 0) {
  68. $order_state = -1;
  69. if ($resp['data']['status'] == '已支付') {
  70. $order_state = ORDER_STATE_SUCCESS;
  71. } elseif ($resp['data']['status'] == '支付失败') {
  72. $order_state = ORDER_STATE_CANCEL;
  73. }
  74. if ($order_state == -1) {
  75. return [false, $resp['data']];
  76. }
  77. return [true, $order_state];
  78. } else {
  79. return [false, $resp['msg']];
  80. }
  81. }
  82. }
  83. private function sign($params)
  84. {
  85. $key = config::KEY;
  86. $content = $params['mchid'] . $params['tel'] . $params['mch_order_id'] . $params['price'] . $params['teltype'] . $params['timeout'] . $params['notify'];
  87. $content .= $params['time'] . $params['rand'] . $key;
  88. return md5($content);
  89. }
  90. private function phone_type($phone)
  91. {
  92. $card_type = mtopcard\card_type($phone);
  93. if ($card_type == mtopcard\ChinaMobileCard) {
  94. return 2;
  95. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  96. return 1;
  97. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  98. return 3;
  99. }
  100. }
  101. }