BsPayClient.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace BsPaySdk\core;
  3. use Exception;
  4. use ReflectionClass;
  5. class BsPayClient extends BsPay
  6. {
  7. /**
  8. * @var string $merChantKey
  9. */
  10. protected $merChantKey = "default";
  11. public function __construct($merChantKey = 'default')
  12. {
  13. $this->merChantKey = $merChantKey;
  14. }
  15. /**
  16. * 请求接口
  17. *
  18. * @param object | array $request 请求实例对象 | 请求参数数组
  19. * @param mixed $tag 请求类型标识
  20. * boolean $tag true (页面接口请求) | false (非页面接口请求)
  21. * CURLFile $tag 待上传的文件
  22. */
  23. public function postRequest($request, $tag = false) {
  24. try {
  25. // 请求接口获取应答
  26. if (is_object($request)) {
  27. $data = $this->objectToArray($request);
  28. } else {
  29. $data = $request;
  30. }
  31. // 处理文件上传接口
  32. if (is_object($tag)) {
  33. $data['params']['needSign'] = false; // 请求不加签名
  34. $data['params']['needVerfySign'] = false; // 返回数据不验证签名
  35. return self::post($data['funcCode'], $data['params'], $tag, $this->merChantKey);
  36. }
  37. // 处理页面接口请求
  38. if ($tag) {
  39. $data['params']['needSign'] = true; // 请求加签名
  40. $data['params']['needVerfySign'] = false; // 返回数据不验证签名
  41. }
  42. return self::post($data['funcCode'], $data['params'], "", $this->merChantKey);
  43. } catch (Exception $e) {
  44. echo $e->getMessage();
  45. return null;
  46. }
  47. }
  48. /**
  49. * 对象转换数组
  50. * @param object $object 待转化参数对象
  51. */
  52. public function objectToArray($object) {
  53. $class = new ReflectionClass($object);
  54. $properties = $class->getProperties();
  55. $arrayData = array();
  56. $extendInfos = array();
  57. // 转换接口参数
  58. foreach ($properties as $key => $value) {
  59. $attrName = $value->getName();
  60. $method = 'get'.ucfirst($attrName);
  61. if ( $class->hasMethod($method) ) {
  62. $attrValue = $class->getMethod($method)->invoke($object);
  63. if (isset($attrValue)) {
  64. if ($attrName != "extendInfos") {
  65. $newKey = strtolower(preg_replace("/([a-z])([A-Z])/", "$1_$2", $attrName));
  66. $arrayData[$newKey] = $attrValue;
  67. } else {
  68. $extendInfos = $attrValue;
  69. }
  70. }
  71. }
  72. }
  73. // 获取接口方法
  74. $funcCode = $class->getMethod('getFunctionCode')->invoke($object);
  75. $data = array(
  76. 'params' => array_merge($arrayData, $extendInfos),
  77. 'funcCode' => $funcCode ? $funcCode : '',
  78. );
  79. return $data;
  80. }
  81. }