RefillPhone.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace refill\legou;
  3. require_once(BASE_HELPER_RAPI_PATH . '/legou/config.php');
  4. use mtopcard\cards_helper;
  5. use refill;
  6. use Log;
  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,$card_type, int $amount, string $order_sn)
  14. {
  15. $params['username'] = config::USERNAME;
  16. $params['orderNumber'] = $order_sn;
  17. $params['cardNumber'] = $phone;
  18. $params['cardExt'] = $amount;
  19. $params['productNo'] = config::ProductIDS[$card_type];
  20. $params['timestamp'] = $this->getMillisecond();
  21. $params['notifyUrl'] = config::NOTIFY_URL;
  22. return $params;
  23. }
  24. public function add($card_no, $card_type, $amount, $input,&$net_errno = 0)
  25. {
  26. $params = $this->req_params($card_no,$card_type, $amount, $input['order_sn']);
  27. [$success,$card] = cards_helper::reserve($card_type,$amount,$input['buyer_id'],$card_no,$card_type,$input['order_id'],config::StoreIDS);
  28. if(!$success) {
  29. return [false, '没有可用卡密', false];
  30. }
  31. $rcard_no = $card->card_no();
  32. $rcard_key = $card->card_key();
  33. $params['rechargeNum'] = $rcard_no;
  34. $params['rechargePwd'] = $rcard_key;
  35. $sign = $this->sign($params);
  36. $params['sign'] = $sign;
  37. $params['rechargeNum'] = $this->encryptWithOpenssl($rcard_no);
  38. $params['rechargePwd'] = $this->encryptWithOpenssl($rcard_key);
  39. $resp = http_request(config::ORDER_URL, $params , 'POST' , false , config::ExtHeaders , $net_errno);
  40. $order_id = $params['order_id'];
  41. if (empty($resp)) {
  42. cards_helper::reuse($order_id);
  43. return [false, '网络错误', true];
  44. }
  45. else
  46. {
  47. Log::record($resp, Log::DEBUG);
  48. $resp = json_decode($resp ,true);
  49. if (empty($resp)) {
  50. cards_helper::reuse($order_id);
  51. return [false, '网络错误', true];
  52. }
  53. $status = intval($resp['code']['status']);
  54. if ($status == 100000) {
  55. return [true, $resp['obj'], false];
  56. } else {
  57. cards_helper::reuse($order_id);
  58. Log::record("refill {$card_no} err: {$resp['code']['desc']}", Log::DEBUG);
  59. return [false, $resp['code']['desc'], false];
  60. }
  61. }
  62. }
  63. public function query($refill_info)
  64. {
  65. $params['orderNumber'] = $refill_info['order_sn'];
  66. $params['username'] = config::USERNAME;
  67. $content = $params['username'] . $params['orderNumber'] . config::KEY;
  68. $params['sign'] = md5($content);
  69. $resp = http_request(config::QUERY_URL, $params , 'POST' , false , config::ExtHeaders);
  70. if (empty($resp)) {
  71. return [false, '网络错误'];
  72. }
  73. else
  74. {
  75. Log::record($resp, Log::DEBUG);
  76. $resp = json_decode($resp, true);
  77. if (empty($resp)) {
  78. return [false, '网络错误'];
  79. }
  80. $oper_status = intval($resp['code']['status']);
  81. if ($oper_status == 100000)
  82. {
  83. $obj = $resp['obj'];
  84. $code = $obj['resultCode'];
  85. $order_id = $refill_info['order_id'];
  86. if ($code == 2) { //充值成功
  87. $order_state = ORDER_STATE_SUCCESS;
  88. $updata['official_sn'] = strtolower($obj['rechargeVoucher']) == 'null' ? '' : $obj['rechargeVoucher'];
  89. Model('refill_order')->edit($refill_info['order_id'], $updata);
  90. cards_helper::assign($order_id, '');
  91. } elseif ($code == 3) { //充值失败
  92. $order_state = ORDER_STATE_CANCEL;
  93. $this->onError($order_id, $obj['rechargeDesc']);
  94. } elseif($code == 4) { //订单存疑,需要人工确认
  95. return [false, $resp['code']['desc']];
  96. }
  97. else {
  98. $order_state = ORDER_STATE_SEND;
  99. }
  100. return [true, $order_state];
  101. }
  102. else {
  103. return [false, $resp['code']['desc']];
  104. }
  105. }
  106. }
  107. private function onError($order_id, $desc)
  108. {
  109. $desc = json_decode($desc, true);
  110. $status = $desc['rechargeStatus'];
  111. $desc = $desc['rechargeDesc'];
  112. if (in_array($status, [600400, 600401, 600402, 600403, 600404, 600405])) {
  113. cards_helper::freeze($order_id,$desc);
  114. } else {
  115. cards_helper::reuse($order_id);
  116. }
  117. }
  118. private function sign($params)
  119. {
  120. $key = config::KEY;
  121. $content = $params['username'] . $params['orderNumber'] . $params['cardNumber'] . $params['cardExt'] . $params['rechargeNum'] . $params['rechargePwd'] . $params['productNo'];
  122. $content .= $params['timestamp'] . $key;
  123. return md5($content);
  124. }
  125. /**
  126. * 获取毫秒级别的时间戳
  127. */
  128. private function getMillisecond()
  129. {
  130. $cur = microtime (true);
  131. $cur = intval($cur * 1000);
  132. return $cur;
  133. }
  134. private function encryptWithOpenssl($data = '')
  135. {
  136. $key = substr(config::KEY , 0 ,16);
  137. $iv = substr(config::KEY , -16);
  138. return base64_encode(openssl_encrypt($data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv));
  139. }
  140. }