BaseStruct.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace addons\Sicpay\common\sdk\config;
  3. use addons\Sicpay\common\models\SicpayConfig;
  4. use addons\Sicpay\common\sdk\PaySign;
  5. use addons\Sicpay\common\services\BaseService;
  6. use yii\helpers\BaseInflector;
  7. /**
  8. * 基础配置
  9. */
  10. class BaseStruct extends BaseService implements BaseInterface
  11. {
  12. public $version = "2.0.0";
  13. // 终端好
  14. public $terminal_no;
  15. // 交易类型
  16. public $trade_code = "PAY";
  17. // 商户号
  18. public $agencyId;
  19. // 交易币种
  20. public $currency_type = "CNY";
  21. // 清算币种
  22. public $sett_currency_type = "HKD";
  23. // 请求接口
  24. public $url = "https://gate.sicpay.com/backSecure.do";
  25. // public $url = "https://testpay.sicpay.com/hk-gateway-web/backSecure.do";
  26. // 商户RSA私钥
  27. public $merRsaPrivateKey;
  28. // 平台(我司)RSA公
  29. public $rootRsaPublicKey;
  30. // 交易相关
  31. public $order_no;
  32. public $amount;
  33. public $notify_url;
  34. public $return_url;
  35. public $product_name;
  36. public $client_ip;
  37. public $mall_id;
  38. public $sdk_way;
  39. private $pay_sign;
  40. protected $response;
  41. public function __construct ($config = [])
  42. {
  43. // 获取配置
  44. if(empty($config['mall_id'])) throw new \Exception(" 缺少 mall_id ");
  45. $base = SicpayConfig::getConfig($config['mall_id']);
  46. if(empty($base)) throw new \Exception(" 配置文件为空 ");
  47. $base_config = array_merge($base->toArray(),$config);
  48. foreach ($base_config as $key => $val) {
  49. if (property_exists($this, $key)) {
  50. $this->{$key} = $val;
  51. }
  52. }
  53. $this->pay_sign = new PaySign();
  54. empty($this->notify_url) && $this->setNotifyUrl();
  55. empty($this->return_url) && $this->setReturnUrl();
  56. empty($this->client_ip) && $this->setClientIp();
  57. }
  58. public function setNotifyUrl(){
  59. $this->notify_url = \Yii::$app->services->setting->platform->get('system.api_url').'/notify/sicpay?mall_id='.$this->mall_id;
  60. }
  61. public function setReturnUrl(){
  62. // $this->return_url= \Yii::$app->services->setting->platform->get('system.api_url').'/notify/pay?mall_id='.$this->mall_id;
  63. $this->return_url= \Yii::$app->services->setting->platform->get('system.h5_url')."/#/plugins/pages/schemes/index";
  64. }
  65. public function setClientIp(){
  66. if (!empty($_SERVER['REMOTE_ADDR'])) {
  67. $ip = $_SERVER['REMOTE_ADDR'];
  68. } else {
  69. $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
  70. }
  71. $this->client_ip = filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
  72. }
  73. public function getConfig(){
  74. // TODO: Implement getConfig() method.
  75. $class = new \ReflectionClass($this);
  76. $pro = $class->getProperties();
  77. $config = [];
  78. foreach ($pro as $item) {
  79. if($item->isPublic() ){
  80. $config[$item->getName()] = $item->getValue($this);
  81. }
  82. }
  83. return array_filter($config);
  84. }
  85. public function pay ()
  86. {
  87. // TODO: Implement pay() method.
  88. $param = $this->getConfig();
  89. $res = $this->pay_sign->sendEncodeData($param,$param);
  90. // var_dump($res);
  91. if(!isset($res['resp_code']) || $res['resp_code'] != '0000'){
  92. return $this->error($res['resp_desc']);
  93. }
  94. // 成功
  95. return $this->response = $res;
  96. }
  97. public function query ()
  98. {
  99. // TODO: Implement query() method.
  100. $param = $this->getConfig();
  101. $res = $this->pay_sign->sendEncodeData($param,$param);
  102. if(isset($res['resp_code']) && $res['resp_code'] != '0000'){
  103. return $this->error($res['resp_desc']);
  104. }
  105. // 成功
  106. return $this->response = $res;
  107. }
  108. public function returnData(){
  109. // TODO: Implement pay() method.
  110. return [
  111. 'busi_code'=>"PRE_PAY",
  112. 'merchant_no'=>$this->agencyId,
  113. 'terminal_no'=>$this->terminal_no,
  114. 'order_no'=>$this->order_no,
  115. 'pay_no'=>$this->response['pay_no'] ?? '',
  116. ];
  117. }
  118. }