RefillPhone.php 3.8 KB

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