JueqiController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\payment\controllers;
  3. use app\common\components\BaseController;
  4. use app\common\helpers\Url;
  5. use app\common\models\AccountWechats;
  6. use app\common\services\Pay;
  7. use app\payment\PaymentController;
  8. use Yunshop\CloudPay\services\CloudPayNotifyService;
  9. class JueqiController extends PaymentController
  10. {
  11. private $data = [];
  12. private $orderSn = '';
  13. public function preAction()
  14. {
  15. parent::preAction();
  16. if (empty(\YunShop::app()->uniacid)) {
  17. $res = $this->getPost();
  18. if($res === true){
  19. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = request()->input('uniacid');
  20. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  21. }
  22. }
  23. }
  24. /**
  25. * JSAPI 回调
  26. */
  27. public function notifyUrl()
  28. {
  29. $this->log($this->data);
  30. \Log::info('-------this->data---------',$this->data);
  31. // 微信
  32. if ($this->data['source'] == 1 && $this->data['state'] ==2) {
  33. \Log::info('------验证成功-----');
  34. $data = [
  35. 'total_fee' => floatval($this->data['truemoney']),
  36. 'out_trade_no' => $this->orderSn,
  37. 'trade_no' => $this->data['oid'],
  38. 'unit' => 'yuan',
  39. 'pay_type' => '云支付',
  40. 'pay_type_id' => 33
  41. ];
  42. $this->payResutl($data);
  43. \Log::info('----结束----');
  44. echo "success";
  45. } else {
  46. echo "fail";
  47. }
  48. }
  49. /**
  50. * 小程序回调
  51. */
  52. public function notifyMiniAppUrl()
  53. {
  54. //todo 验证签名在此方法下进行
  55. // $data = $this->getPost();
  56. $this->log($this->data);
  57. // $this->getSignResult($data['paySign']);
  58. if (($this->data['source'] == 1 || $this->data['source'] == 2 || $this->data['source'] == 3) && $this->data['state'] ==2) {
  59. if($this->getQuery() !== true){
  60. echo 'fail';exit();
  61. }
  62. \Log::debug('------验证成功-----');
  63. $data = [
  64. 'total_fee' => floatval($this->data['truemoney']),
  65. 'out_trade_no' => $this->orderSn,
  66. 'trade_no' => $this->data['oid'],
  67. 'unit' => 'yuan',
  68. 'pay_type' => '云支付',
  69. 'pay_type_id' => 33
  70. ];
  71. $this->payResutl($data);
  72. \Log::debug('----结束----');
  73. echo "success";
  74. } else {
  75. echo "fail";
  76. }
  77. }
  78. public function getPost()
  79. {
  80. //支付回调接口(jsapi支付)
  81. //获取xml
  82. $xmlData = file_get_contents('php://input');
  83. libxml_disable_entity_loader(true);
  84. //转换成数组
  85. $rsdata = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  86. \Log::info($rsdata);
  87. ksort($rsdata);
  88. if(empty($rsdata)){
  89. return true;
  90. }
  91. $this->data = $rsdata;
  92. if($this->data){
  93. $data = explode(':', $this->data['olid']);
  94. $this->orderSn = $data['0'];
  95. \Log::info($data['0']);
  96. \Log::info($data['1']);
  97. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $data['1'];
  98. }
  99. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  100. $buff = '';
  101. foreach ($rsdata as $k => $v){
  102. if($k != 'sign'){
  103. $buff .= $k . '=' . $v . '&';
  104. }
  105. }
  106. $stringSignTemp = $buff .'key='.\Setting::get('plugin.jueqi_pay_set.key'); //支付秘钥
  107. \Log::info('-------key---------',\Setting::get('plugin.jueqi_pay_set.key'));
  108. \Log::info('-------stringSignTemp---------',$stringSignTemp);
  109. $sign = strtoupper(md5($stringSignTemp));//生成签名
  110. \Log::info('-------sign---------',$sign);
  111. //验证签名
  112. if($sign == $rsdata['sign']){
  113. return $rsdata;
  114. }else{
  115. \Log::info("data-no:".$xmlData );
  116. exit('FAIL');
  117. }
  118. }
  119. /**
  120. * 成功跳转页面
  121. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  122. */
  123. public function returnUrl()
  124. {
  125. redirect(Url::absoluteApp('home'))->send();
  126. }
  127. public function frontUrl()
  128. {
  129. $trade = \Setting::get('shop.trade');
  130. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  131. return redirect($trade['redirect_url'])->send();
  132. }
  133. if (0 == $_GET['state'] && $_GET['errorDetail'] == '成功') {
  134. redirect(Url::absoluteApp('member', ['i' => $_GET['attach']]))->send();
  135. } else {
  136. redirect(Url::absoluteApp('member', ['i' => $_GET['attach']]))->send();
  137. }
  138. }
  139. /**
  140. * 支付日志
  141. *
  142. * @param $post
  143. */
  144. public function log($data)
  145. {
  146. //访问记录
  147. Pay::payAccessLog();
  148. //保存响应数据
  149. Pay::payResponseDataLog($data['olid'], '云支付', json_encode($data));
  150. }
  151. private function getQuery()
  152. {
  153. //订单查询接口
  154. $stringSignTemp = 'olid='.$this->data['olid'].'&key='.\Setting::get('plugin.jueqi_pay_set.key'); //支付秘钥
  155. $sign = strtoupper(md5($stringSignTemp));//生成签名
  156. $post_data['olid'] = $this->data['olid']; //唯一标识
  157. $post_data['sign'] = $sign; //签名
  158. $url = \Setting::get('plugin.jueqi_pay_set.order_query');
  159. $ch = curl_init();
  160. curl_setopt($ch, CURLOPT_URL, $url);
  161. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  162. // post数据
  163. curl_setopt($ch, CURLOPT_POST, 1);
  164. // post的变量
  165. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  166. $output = curl_exec($ch);
  167. curl_close($ch);
  168. //获取xml
  169. libxml_disable_entity_loader(true);
  170. $rsdata = json_decode(json_encode(simplexml_load_string($output, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  171. if($rsdata['state'] == 2){
  172. return true;
  173. }else{
  174. \Log::debug($rsdata['errordesc']);
  175. }
  176. }
  177. }