RefillPhone.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace refill\yuke_dx;
  3. require_once(BASE_HELPER_RAPI_PATH . '/yuke_dx/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. private function req_params(int $phone, int $card_type, int $amount, string $order_sn)
  13. {
  14. $params['rechargeAccount'] = $phone;
  15. $params['rechargeAmount'] = $amount;
  16. $params['tPfChannelId'] = config::CHANNEL_ID;
  17. $params['orderType'] = 0;
  18. $params['outOrderId'] = $order_sn;
  19. $params['randomNumber'] = $this->createNoncestr(10);
  20. return $params;
  21. }
  22. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  23. {
  24. $params = $this->req_params($card_no, $card_type, $amount, $params['order_sn']);
  25. $sign = $this->sign($params);
  26. $params['sign'] = $sign;
  27. $params = json_encode($params);
  28. $resp = http_post_data(config::ORDER_URL, $params , config::ExtHeaders, $net_errno);
  29. if (empty($resp)) {
  30. return [false, '系统错误', true];
  31. }
  32. else
  33. {
  34. Log::record($resp, Log::DEBUG);
  35. $resp = json_decode($resp, true);
  36. if (empty($resp)) {
  37. return [false, '系统错误', true];
  38. } elseif ($resp['code'] === 1) {
  39. return [true, $resp['data']['order_sn'], false];
  40. } else {
  41. return [false, $resp['message'], false];
  42. }
  43. }
  44. }
  45. public function query($refill_info)
  46. {
  47. $params['tPfChannelId'] = config::CHANNEL_ID;
  48. $params['outOrderId'] = $refill_info['order_sn'];
  49. $params = json_encode($params);
  50. $resp = http_post_data(config::QUERY_URL, $params , config::ExtHeaders);
  51. if (empty($resp)) {
  52. return [false, '系统错误'];
  53. }
  54. else
  55. {
  56. Log::record($resp, Log::DEBUG);
  57. $resp = json_decode($resp, true);
  58. if (empty($resp)) {
  59. return [false, '系统错误'];
  60. }
  61. elseif($resp['code'] === 1)
  62. {
  63. $status = $resp['data']['orderInfo']['orderStatus'];
  64. if ($status === 2) {
  65. $order_state = ORDER_STATE_SUCCESS;
  66. } elseif ($status === 3 || $status === 4) {
  67. $order_state = ORDER_STATE_CANCEL;
  68. } elseif ($status === 1) {
  69. $order_state = ORDER_STATE_SEND;
  70. } else {
  71. return [false, $status];
  72. }
  73. return [true, $order_state];
  74. }
  75. elseif ($resp['code'] === -2004 && (time() - $refill_info['commit_time'] > 600))
  76. {
  77. return [true, ORDER_STATE_NOEXIST];
  78. }
  79. else
  80. {
  81. return [false, $resp['message']];
  82. }
  83. }
  84. }
  85. public function balance()
  86. {
  87. $params['tPfChannelId'] = config::CHANNEL_ID;
  88. $params['tunnelType'] = 0;
  89. $key = config::SECRET_KEY;
  90. $content = "tPfChannelId={$params['tPfChannelId']}&tunnelType={$params['tunnelType']}&secret_key={$key}";
  91. $params['sign'] = strtoupper(md5($content));
  92. $params = json_encode($params);
  93. $resp = http_post_data(config::BALANCE_URL, $params , config::ExtHeaders);
  94. if (empty($resp)) {
  95. return [false, '系统错误'];
  96. }
  97. else
  98. {
  99. Log::record($resp, Log::DEBUG);
  100. $resp = json_decode($resp, true);
  101. if (empty($resp)) {
  102. return [false, '系统错误'];
  103. } elseif ($resp['code'] == 1) {
  104. return [true, $resp['data']['amountOfMoney']];
  105. } else {
  106. return [false, $resp['message']];
  107. }
  108. }
  109. }
  110. private function createNoncestr( $length = 32 )
  111. {
  112. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  113. $str ="";
  114. for ( $i = 0; $i < $length; $i++ ) {
  115. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  116. }
  117. return $str;
  118. }
  119. private function sign($params)
  120. {
  121. $key = config::SECRET_KEY;
  122. $content = "orderType={$params['orderType']}&tPfChannelId={$params['tPfChannelId']}&rechargeAccount={$params['rechargeAccount']}&rechargeAmount={$params['rechargeAmount']}";
  123. $content .= "&randomNumber={$params['randomNumber']}&secret_key={$key}";
  124. return strtoupper(md5($content));
  125. }
  126. }