RefillPhone.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace refill\huafutong_doubi;
  3. require_once(BASE_HELPER_RAPI_PATH . '/huafutong_doubi/config.php');
  4. use refill;
  5. use Log;
  6. class RefillPhone extends refill\IRefillPhone
  7. {
  8. public function __construct($cfgs)
  9. {
  10. parent::__construct($cfgs);
  11. }
  12. public function goods($quality, int $amount, int $card_type, $regin_no, $other)
  13. {
  14. [$goods_id, $price] = parent::goods($quality, $amount, $card_type, $regin_no, $other);
  15. if ($goods_id <= 0) return [0, 0];
  16. $store_id = $this->mStoreID;
  17. $pcode = $other['product_code'];
  18. $thrid_refill = Model('thrid_refill');
  19. $product = $thrid_refill->getProviderProduct($store_id, $goods_id, $pcode);
  20. if (empty($product)) {
  21. Log::record("cannot find provider's produce where name={$this->mName}, goods_id = {$goods_id} pcode={$pcode}", Log::ERR);
  22. return [0, 0];
  23. } else {
  24. return [$goods_id, ncPriceFormat($product['channel_amount'])];
  25. }
  26. }
  27. private function req_params(int $phone, int $amount, string $order_sn)
  28. {
  29. $data['ChargeAccount'] = $phone;
  30. $data['SupBuyNum'] = 1;
  31. $data['ChargeValue'] = $amount;
  32. $data['OrderType'] = 1;
  33. $data['ChargeType'] = 10;
  34. $data['Id'] = $order_sn;
  35. $params['mchid'] = config::MCH_ID;
  36. $params['notifyurl'] = config::NOTIFY_URL;
  37. $params['data'] = $this->aes_encrypt($data);
  38. return $params;
  39. }
  40. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  41. {
  42. $params = $this->req_params($card_no, $amount, $params['order_sn']);
  43. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  44. if (empty($resp)) {
  45. return [false, '系统错误', true];
  46. }
  47. else
  48. {
  49. Log::record($resp, Log::DEBUG);
  50. $resp = json_decode($resp, true);
  51. if (empty($resp)) {
  52. return [false, '系统错误', true];
  53. } elseif ($resp['code'] === 'success') {
  54. return [true, $resp['data']['trans_sn'], false];
  55. } else {
  56. return [false, $resp['msg'], false];
  57. }
  58. }
  59. }
  60. public function query($refill_info)
  61. {
  62. $params['mchid'] = config::MCH_ID;
  63. $params['order_id'] = $refill_info['order_sn'];
  64. $params['time'] = time();
  65. $content = "{$params['mchid']}{$params['order_id']}{$params['time']}".config::UserKey;
  66. $params['sign'] = md5($content) ;
  67. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  68. if (empty($resp)) {
  69. return [false, '系统错误'];
  70. }
  71. else
  72. {
  73. Log::record($resp, Log::DEBUG);
  74. $resp = json_decode($resp, true);
  75. if (empty($resp)) {
  76. return [false, '系统错误'];
  77. }
  78. elseif ($resp['code'] === '3')
  79. {
  80. $status = $resp['data']['status'];
  81. if ($status === 3) {
  82. Model('refill_order')->edit($refill_info['order_id'], ['official_sn' => $resp['data']['certificate']]);
  83. $order_state = ORDER_STATE_SUCCESS;
  84. } elseif (in_array($status, [0, 2], true)) {
  85. $order_state = ORDER_STATE_CANCEL;
  86. } elseif ($status === 1) {
  87. $order_state = ORDER_STATE_SEND;
  88. } else {
  89. return [false, $status];
  90. }
  91. return [true, $order_state];
  92. }
  93. elseif ($resp['code'] === '2' && time() - $refill_info['commit_time'] > 600) {
  94. return [true, ORDER_STATE_NOEXIST];
  95. }
  96. else {
  97. return [false, $resp['msg']];
  98. }
  99. }
  100. }
  101. public function balance()
  102. {
  103. $params['mchid'] = config::MCH_ID;
  104. $params['time'] = time();
  105. $content = "{$params['mchid']}{$params['time']}".config::UserKey;
  106. $params['sign'] =md5($content) ;
  107. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  108. Log::record($resp, Log::DEBUG);
  109. $resp = json_decode($resp, true);
  110. if (empty($resp)) {
  111. return [false, '系统错误'];
  112. } elseif ($resp['code'] === '3') {
  113. return [true, $resp['data']['money']];
  114. } else {
  115. return [false, $resp['msg']];
  116. }
  117. }
  118. private function aes_encrypt($params): string
  119. {
  120. $encrypt = json_encode($params);
  121. return base64_encode(openssl_encrypt($encrypt, 'AES-128-CBC', config::UserKey, OPENSSL_RAW_DATA, config::aesIV));
  122. }
  123. }