Core.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Cmbc\Api;
  3. use Cmbc\Setting;
  4. abstract class Core
  5. {
  6. protected $param = array();
  7. protected $data = array();
  8. protected $setting = null;
  9. protected $error = array();
  10. protected $method = 'post';
  11. abstract protected function init();
  12. abstract public function request($request, $tool = false);
  13. abstract public function response($response);
  14. public function __construct(Setting $setting)
  15. {
  16. $this->setting = $setting;
  17. $this->init();
  18. $appid = explode('|', $this->setting->getAppId());
  19. $this->param['appId'] = $appid[0];
  20. $this->param['mhtSubAppId'] = $appid[1];
  21. //$this->param['mhtSubMchId'] = $this->setting->getSubMchId();
  22. }
  23. protected function setParam($key, $default = false, $isMust = true)
  24. {
  25. if (!$this->data) {
  26. $this->setError('param data is not exists');
  27. } else {
  28. if (!isset($this->data[$key]) && $default) {
  29. $this->data[$key] = $default;
  30. }
  31. if (isset($this->data[$key])) {
  32. if (!$this->data[$key] || $this->data[$key] == 0) {
  33. if ($default) {
  34. $this->data[$key] = $default;
  35. }
  36. }
  37. if ($isMust == true && !$this->data[$key] && $this->data[$key] != 0) {
  38. $this->setError('param data ['.$key.'] is not value');
  39. }
  40. $this->param[$key] = $this->data[$key];
  41. }
  42. }
  43. return $this;
  44. }
  45. protected function createSignature($type = 'MD5')
  46. {
  47. ksort($this->param);
  48. $signature_string = '';
  49. foreach ($this->param as $k => $v) {
  50. if ($v || $v == 0) {
  51. $signature_string .= $k . '=' . $v . '&';
  52. }
  53. }
  54. $signature_string = substr($signature_string, 0, -1);
  55. $method = strtoupper($type);
  56. $secret = $method($this->setting->getAppSecret());
  57. return $method($signature_string . '&' . $secret);
  58. }
  59. protected function curl()
  60. {
  61. $curl = curl_init();
  62. curl_setopt($curl, CURLOPT_URL, $this->api);
  63. curl_setopt($curl, CURLOPT_HEADER, false);
  64. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  65. if ($this->method == 'post' && $this->param) {
  66. curl_setopt($curl, CURLOPT_POST, 1);
  67. $string = '';
  68. foreach ($this->param as $k => $v) {
  69. if ($v || $v == 0) {
  70. $string .= $k . '=' . $v . '&';
  71. }
  72. }
  73. $string = substr($string, 0, -1);
  74. //\Dever::log('招商银行支付请求' . '||' . $this->api . '||' . $string, 'pay');
  75. curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
  76. }
  77. $data = curl_exec($curl);
  78. curl_close($curl);
  79. parse_str($data, $result);
  80. if (isset($result['responseCode']) && $result['responseCode'] == 'A002') {
  81. # 失败
  82. $this->setError($result['responseMsg']);
  83. return false;
  84. }
  85. return $result;
  86. }
  87. protected function setError($msg)
  88. {
  89. $this->error[] = $msg;
  90. }
  91. protected function getError()
  92. {
  93. $number = count($this->error);
  94. if ($number > 0) {
  95. $msg = $this->error[0];
  96. return $msg;
  97. }
  98. return false;
  99. }
  100. }