RefillPhone.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  43. $code = intval($resp['code']);
  44. if ($code === 0) {
  45. return [true, $resp['pt_order_id'], false];
  46. } else {
  47. return [false, $resp['msg'], false];
  48. }
  49. }
  50. }
  51. public function query($refill_info)
  52. {
  53. $params['mch_order_id'] = $refill_info['order_sn'];
  54. $params['mchid'] = config::MCHID;
  55. $params['pt_order_id'] = $refill_info['ch_trade_no'];
  56. $params['mch_order_id'] = $refill_info['order_sn'];
  57. $params['op_order_id'] = $refill_info['official_sn'];
  58. $params['tel'] = $refill_info['card_no'];
  59. $params['time'] = time();
  60. $params['rand'] = rand(100000,999999);
  61. $content = $params['mchid'] . $params['pt_order_id'] . $params['mch_order_id'] . $params['op_order_id'] . $params['tel'] . $params['time'] . $params['rand'] . config::KEY;
  62. $params['sign'] = md5($content);
  63. $resp = http_request(config::QUERY_URL, $params , 'POST' , false);
  64. if ($resp === false) {
  65. return [false, '系统错误'];
  66. } else {
  67. Log::record($resp, Log::DEBUG);
  68. $resp = json_decode($resp, true);
  69. if($resp === false) {
  70. return [false, '系统错误'];
  71. }
  72. $code = intval($resp['code']);
  73. if ($code === 0) {
  74. if ($resp['data']['status'] == '已支付') {
  75. $order_state = ORDER_STATE_SUCCESS;
  76. } elseif ($resp['data']['status'] == '支付失败') {
  77. $order_state = ORDER_STATE_CANCEL;
  78. } else {
  79. return [false, $resp['data']];
  80. }
  81. return [true, $order_state];
  82. } else {
  83. return [false, $resp['msg']];
  84. }
  85. }
  86. }
  87. private function sign($params)
  88. {
  89. $key = config::KEY;
  90. $content = $params['mchid'] . $params['tel'] . $params['mch_order_id'] . $params['price'] . $params['teltype'] . $params['timeout'] . $params['notify'];
  91. $content .= $params['time'] . $params['rand'] . $key;
  92. return md5($content);
  93. }
  94. private function phone_type($phone)
  95. {
  96. $card_type = mtopcard\card_type($phone);
  97. if ($card_type == mtopcard\ChinaMobileCard) {
  98. return 2;
  99. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  100. return 1;
  101. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  102. return 3;
  103. }
  104. }
  105. }