SmkPay.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Libs\Pay\Merchants;
  3. use GuzzleHttp\Client;
  4. use Log;
  5. /**
  6. * 市民卡支付
  7. */
  8. class SmkPay {
  9. private $merchant_key;
  10. private $merchant_id;
  11. private $appid;
  12. private $private_key_path;
  13. private $private_key_pass;
  14. private $notify_cer_path;
  15. function __construct($config)
  16. {
  17. $this->appid = $config['appid'];
  18. $this->merchant_id = $config['merCode'];
  19. $this->private_key_path = $config['private_key_path'];
  20. $this->private_key_pass = $config['private_key_pass'];
  21. $this->notify_cer_path = $config['notify_cer_path'];
  22. }
  23. //订单发送
  24. function send($data)
  25. {
  26. $client = new client(['base_uri'=>'https://hpay.96225.com/','timeout'=>3]);
  27. //订单金额最小10分
  28. $price = max($data['price'],10);
  29. $data = [
  30. 'version'=>'2.0.0',
  31. 'transCode'=>'AGGP0001',
  32. 'merCode'=>$this->merchant_id,
  33. 'chainNo'=>'004',
  34. 'orderNo'=>$data['trade_no'],
  35. 'dateTime'=>date('YmdHis'),
  36. 'timeOut'=>date('YmdHis',time()+1800),
  37. 'currency'=>'156',
  38. 'txName'=>'novel',
  39. 'amount'=>(string)sprintf("%.2f",$price/100),
  40. 'deviceType'=>'MW',
  41. 'payType'=>'WECHAT-OA',
  42. 'goods'=>$data['body'],
  43. 'notifyUrl'=>env('SMK_PAY_CALL_BACK_URL'),
  44. 'returnUrl'=>env('SMK_PAY_CALL_BACK_URL'),
  45. 'promoParams'=>'{"storeIdType":"1"}',
  46. 'ip'=>$data['create_ip'],
  47. 'outputType'=>'',
  48. 'merCustId'=>'',
  49. 'merAppname'=>'novel',
  50. 'merAppid'=>'http://www.imycmh.com',
  51. 'remark1'=>'',
  52. 'remark2'=>'{"openid":"'.$data['openid'].'","appid":"'.$this->appid.'"}'
  53. ];
  54. $data['sign'] = $this->sign($data);
  55. try {
  56. $header = ['Content-Type'=>'application/x-www-form-urlencoded;charset=utf-8'];
  57. $response = $client->request('POST', "HzsmkAggPay/api/aggpay", ['form_params' => $data,'headers'=>$header])->getBody()->getContents();
  58. Log::info($data);
  59. Log::info($response);
  60. return $this->getPayInfo($response);
  61. }catch (\Exception $e)
  62. {
  63. Log::ERROR('smkpay REQUEST FAIL :'.json_encode($data));
  64. Log::ERROR($e->getMessage());
  65. }
  66. }
  67. //RSA签名
  68. public function sign($data)
  69. {
  70. $sign_str = '';
  71. foreach ($data as $k=>$v)
  72. {
  73. $sign_str.= $k.'='.$v.'&';
  74. }
  75. Log::info('sign_str');
  76. Log::info($sign_str);
  77. $sign_str = rtrim($sign_str, '&');
  78. $sign_str = iconv('UTF-8','GBK', $sign_str);
  79. $pfxpath = storage_path($this->private_key_path);
  80. $cer_key = file_get_contents($pfxpath); //获取密钥内容
  81. openssl_pkcs12_read($cer_key, $certs, $this->private_key_pass);
  82. openssl_sign($sign_str, $signMsg, $certs['pkey'],OPENSSL_ALGO_SHA1); //注册生成加密信息
  83. return base64_encode($signMsg); //base64转码加密信息
  84. }
  85. //验签
  86. public function checkSign($data, $signature)
  87. {
  88. $to_sign_data = [
  89. 'reqSeq'=>$data['reqSeq'],
  90. 'merCode'=>$data['merCode'],
  91. 'serialNo'=>$data['serialNo'],
  92. 'orderNo'=>$data['orderNo'],
  93. 'amount'=>$data['amount'],
  94. 'status'=>$data['status'],
  95. 'respCode'=>$data['respCode'],
  96. 'respDesc'=>$data['respDesc']
  97. ];
  98. $sign_str = '';
  99. foreach ($to_sign_data as $k=>$v)
  100. {
  101. $sign_str.= $k.'='.$v.'&';
  102. }
  103. $sign_str = rtrim($sign_str, '&');
  104. $sign_str = iconv('UTF-8','GBK', $sign_str);
  105. $signature = iconv('UTF-8','GBK', $signature);
  106. $cer_path = storage_path($this->notify_cer_path);
  107. $cer_key = openssl_get_publickey(file_get_contents($cer_path)); //获取密钥内容
  108. return (bool)openssl_verify($sign_str, base64_decode($signature),$cer_key);
  109. }
  110. function getPayInfo($response)
  111. {
  112. $res = json_decode($response, 1);
  113. if ($res['respCode'] == '00') {
  114. $pay_info = json_decode($res['tn'], 1);
  115. return [
  116. 'appId' => $pay_info['appId'],
  117. 'package' => $pay_info['package'],
  118. 'nonceStr' => $pay_info['nonceStr'],
  119. 'timeStamp' => $pay_info['timeStamp'],
  120. 'signType' => $pay_info['signType'],
  121. 'paySign' => $pay_info['paySign'],
  122. ];
  123. }else{
  124. Log::ERROR('smkpay FAIL');
  125. Log::ERROR($response);
  126. }
  127. }
  128. }