Paypal.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Paypal extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. }
  7. //定义方法的调用规则 获取URI第二段值
  8. public function _remap($arg,$arg_array)
  9. {
  10. if($arg == 'token')
  11. {
  12. $this->_token();
  13. }
  14. else
  15. {
  16. $this->_index($arg_array);
  17. }
  18. }
  19. public function _index($arg_array)
  20. {
  21. $url = 'https://api.sandbox.paypal.com/v1/customer/disputes/';
  22. $header[] = 'Content-Type: application/json';
  23. $header[] = 'Accept-Language: en_US';
  24. $header[] = 'Accept: */*';
  25. $method = 'POST';
  26. $hf_account = 'paypal-facilitator@supernovahair.com';
  27. $client_id = 'Ae5ZECTwT-JY-GrHW2-XW234yJ4tYT-7RAt3s1mY8GtW1rX470Kr8weXkCH3GMaO-V7mnmnCTsxFvsiy';
  28. $secret_id = 'EOiZjIwRaiK3pvbJgMURKcGy6ULt5YCGLkqN7WngwG-r34brYrfVwS6ECI8cig7l8lOObvF-ukeZxB-3';
  29. $data = array('grant_type' => 'client_credentials');
  30. $data['start_time'] = date('Y-m-d',time()-20*24*3600);
  31. $data['page'] = $arg_array[0];
  32. $data['page_size'] = 50;
  33. $data['next_page_token'] = $this->_token();
  34. $data['dispute_state'] = 'OPEN_INQUIRIES';
  35. $userpwd = $client_id . ':' . $secret_id;
  36. $info = $this->_curl($data,$url,3000,$method,true,$header,$userpwd);
  37. echo "<pre>";
  38. print_r($info);
  39. }
  40. public function _token()
  41. {
  42. $url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
  43. $header[] = 'Content-Type: application/json';
  44. $header[] = 'Accept-Language: en_US';
  45. $header[] = 'Accept: */*';
  46. $data = array('grant_type' => 'client_credentials');//请求头内容
  47. $method = 'POST';
  48. $hf_path = $_SERVER["DOCUMENT_ROOT"] . '/data/hf_access_token.txt';
  49. $hf_access_token = '';
  50. if(file_exists($hf_path))
  51. {
  52. $file_read = fopen($hf_path, 'r');
  53. $access_str = fgets($file_read);
  54. fclose($file_read);
  55. $access_arr = explode(':', $access_str);
  56. if (time() < intval($access_arr[0]))
  57. {
  58. $hf_access_token = isset($access_arr[1]) ? $access_arr[1] : '';
  59. }
  60. }
  61. if(!$hf_access_token)
  62. {
  63. $file = fopen($hf_path, 'w');
  64. $hf_account = 'paypal-facilitator@supernovahair.com';
  65. $client_id = 'Ae5ZECTwT-JY-GrHW2-XW234yJ4tYT-7RAt3s1mY8GtW1rX470Kr8weXkCH3GMaO-V7mnmnCTsxFvsiy';
  66. $secret_id = 'EOiZjIwRaiK3pvbJgMURKcGy6ULt5YCGLkqN7WngwG-r34brYrfVwS6ECI8cig7l8lOObvF-ukeZxB-3';
  67. $userpwd = $client_id . ':' . $secret_id;
  68. $info = $this->_curl($data,$url,3000,$method,true,$header,$userpwd);
  69. $result = json_decode($info,true);
  70. $access_new = (time() + $result['expires_in']) . ':' . $result['access_token'];
  71. fwrite($file, $access_new);
  72. fclose($file);
  73. $hf_access_token = $result['access_token'];
  74. }
  75. return $hf_access_token;
  76. }
  77. public function _curl($data,$url,$timeout=300,$httptype="POST",$date_type=false,$header=array(),$userpwd='')
  78. {
  79. if ($date_type == 'http_build_query')
  80. {
  81. $data = http_build_query($data);
  82. }
  83. else if ($date_type == 'json')
  84. {
  85. $data = json_encode($data);
  86. }
  87. $ch = curl_init();
  88. curl_setopt($ch, CURLOPT_URL, $url);
  89. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  91. curl_setopt($ch, CURLOPT_HEADER, false);
  92. switch ($httptype)
  93. {
  94. case "GET":
  95. curl_setopt($ch, CURLOPT_HTTPGET, true);
  96. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  97. break;
  98. case "POST":
  99. curl_setopt($ch, CURLOPT_POST, true);
  100. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  101. break;
  102. case "PUT":
  103. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  104. break;
  105. case "DELETE":
  106. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  107. break;
  108. }
  109. $isSecure = strpos($url, "https://");
  110. if ($isSecure === 0)
  111. {
  112. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  113. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  114. }
  115. if(!empty($header))
  116. {
  117. curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
  118. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  119. }
  120. if(!empty($userpwd))
  121. {
  122. curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
  123. }
  124. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  125. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  126. $result = curl_exec($ch);
  127. curl_close($ch);
  128. return $result;
  129. }
  130. }