RefillPhone.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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($resp === false) {
  31. return [false,'系统错误',true];
  32. }
  33. $resp = json_decode($resp,true);
  34. if($resp['code'] == 10000 && $resp['message'] == 'success') {
  35. $result = $resp['result'];
  36. return [true,$result['order_sn'], false];
  37. }
  38. else {
  39. return [false,$resp['message'], false];
  40. }
  41. }
  42. public function query($refill_info)
  43. {
  44. }
  45. /**
  46. * 作用:产生随机字符串,不长于32位
  47. */
  48. public function createNoncestr( $length = 32 )
  49. {
  50. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  51. $str ="";
  52. for ( $i = 0; $i < $length; $i++ ) {
  53. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  54. }
  55. return $str;
  56. }
  57. private function sign($params)
  58. {
  59. ksort($params);
  60. $app_secret = config::APP_SECRET;
  61. $content = $app_secret;
  62. foreach ($params as $key => $val)
  63. {
  64. if(empty($val)){
  65. continue;
  66. }
  67. $content .= "{$key}{$val}";
  68. }
  69. $content .= $app_secret;
  70. return strtoupper(md5($content));
  71. }
  72. }