RefillPhone.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace refill\yifa;
  3. require_once(BASE_HELPER_RAPI_PATH . '/yifa/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 $amount ,string $order_sn)
  13. {
  14. $params['app_id'] = config::APP_ID;
  15. $params['nonce_str'] = strtoupper($this->createNoncestr());
  16. $params['timestamp'] = time();
  17. $params['order_sn'] = $order_sn;
  18. $params['phone'] = $phone;
  19. $params['amount'] = $amount;
  20. $params['notify_url'] = config::NOTIFY_URL;
  21. return $params;
  22. }
  23. public function add($card_no, $card_type,$amount,$params)
  24. {
  25. $order_sn = $params['order_sn'];
  26. $params = $this->req_params($card_no,$amount,$order_sn);
  27. $sign = $this->sign($params);
  28. $params['sign'] = $sign;
  29. $resp = http_request(config::ORDER_URL,$params,'POST',false,config::ExtHeaders);
  30. if (empty($resp)) {
  31. return [false,'系统错误',true];
  32. }
  33. $resp = json_decode($resp,true);
  34. if (empty($resp)) {
  35. return [false, '系统错误', true];
  36. } elseif ($resp['code'] == 10000 && $resp['message'] == 'success') {
  37. $result = $resp['result'];
  38. return [true, $result['order_sn'], false];
  39. } else {
  40. return [false, $resp['message'], false];
  41. }
  42. }
  43. public function query($refill_info)
  44. {
  45. }
  46. /**
  47. * 作用:产生随机字符串,不长于32位
  48. */
  49. public function createNoncestr( $length = 32 )
  50. {
  51. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  52. $str ="";
  53. for ( $i = 0; $i < $length; $i++ ) {
  54. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  55. }
  56. return $str;
  57. }
  58. private function sign($params)
  59. {
  60. ksort($params);
  61. $app_secret = config::APP_SECRET;
  62. $content = $app_secret;
  63. foreach ($params as $key => $val)
  64. {
  65. if(empty($val)){
  66. continue;
  67. }
  68. $content .= "{$key}{$val}";
  69. }
  70. $content .= $app_secret;
  71. return strtoupper(md5($content));
  72. }
  73. }