StoreaggregateController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/10/27
  6. * Time: 15:21
  7. */
  8. namespace app\payment\controllers;
  9. use app\common\helpers\Url;
  10. use app\common\models\AccountWechats;
  11. use app\common\models\OrderPay;
  12. use app\common\services\Pay;
  13. use app\common\services\PayFactory;
  14. use app\payment\PaymentController;
  15. use Illuminate\Support\Facades\DB;
  16. use Yunshop\StoreAggregatePay\models\AggregatePayAccount;
  17. use Yunshop\StoreAggregatePay\models\StoreAggregatePayLog;
  18. use Yunshop\StoreAggregatePay\services\Client;
  19. class StoreaggregateController extends PaymentController
  20. {
  21. protected $responseParameters;
  22. protected $payLog;
  23. protected $account;
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->head();
  28. }
  29. protected function head()
  30. {
  31. $result = $this->setParameters();
  32. $this->payLog = DB::table('yz_store_aggregate_pay_log')->where('pay_sn', $result['out_no'])->first();
  33. if (is_null($this->payLog)) {
  34. \Log::debug('---门店聚合支付记录不存在---', $result);
  35. echo 'not pay log'; exit();
  36. }
  37. if (empty(\YunShop::app()->uniacid)) {
  38. \YunShop::app()->uniacid = $this->payLog['uniacid'];
  39. \Setting::$uniqueAccountId = \YunShop::app()->uniacid;
  40. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  41. }
  42. if (empty(\YunShop::app()->uniacid)) {
  43. \Log::debug('---------聚合支付回调无法获取公众号------------');
  44. echo '无法获取公众号'; exit();
  45. }
  46. }
  47. protected function setParameters()
  48. {
  49. \Log::debug('-------聚合支付------', $_GET);
  50. //获取返回参数
  51. return $this->responseParameters = $_GET;
  52. }
  53. //微信支付同步通知,支付宝服务窗支付也可使用
  54. public function jumpUrl()
  55. {
  56. $trade = \Setting::get('shop.trade');
  57. if ($this->getParameter('status') != 1) {
  58. redirect(Url::absoluteApp('member/payErr', ['i' => \YunShop::app()->uniacid]))->send();
  59. exit();
  60. }
  61. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  62. redirect($trade['redirect_url'].'&outtradeno='. $this->getParameter('out_no'))->send();
  63. exit();
  64. }
  65. redirect(Url::absoluteApp('member/payYes'))->send();
  66. }
  67. //支付
  68. public function notifyUrl()
  69. {
  70. $this->log($this->getParameter('out_no'), $this->getAllParameter(), '微信支付--聚合支付');
  71. if ($this->checkSign($this->getAllParameter())) {
  72. $result = $this->thirdPartyOrder();
  73. if ($result['status'] !== 200) {
  74. \Log::debug('<----------微信支付--聚合支付回调查询订单状态失败--->', $result);
  75. echo '查询第三方订单失败:'.$result['msg']; exit();
  76. }
  77. $selectData = $result['data']['data'];
  78. if ($selectData['status'] == '1') {
  79. $data = [
  80. 'total_fee' =>$selectData['trade_amount'],
  81. 'out_trade_no' => $this->getParameter('out_no'),
  82. 'trade_no' => $this->getParameter('ord_no'),
  83. 'unit' => 'fen',
  84. 'pay_type' => '微信支付--聚合支付',
  85. 'pay_type_id' => PayFactory::STORE_AGGREGATE_WECHAT,
  86. ];
  87. $this->payResutl($data);
  88. echo 'notify_success'; exit();
  89. }
  90. \Log::debug('<----------微信支付--聚合支付回调--交易未成功--->'. $this->getParameter('out_no'), $this->getAllParameter());
  91. echo 'fail';exit();
  92. }
  93. \Log::debug('---------微信支付--聚合支付回调--支付失败---'. $this->getParameter('out_no'), $this->getAllParameter());
  94. echo 'fail';exit();
  95. }
  96. //支付宝
  97. public function alipayUrl()
  98. {
  99. $this->log($this->getParameter('out_no'), $this->getAllParameter(), '支付宝支付--聚合支付');
  100. if ($this->checkSign($this->getAllParameter())) {
  101. $result = $this->thirdPartyOrder();
  102. if ($result['status'] !== 200) {
  103. \Log::debug('<----------支付宝支付--聚合支付回调查询订单状态失败--->', $result);
  104. echo '查询第三方订单失败:'.$result['msg']; exit();
  105. }
  106. $selectData = $result['data']['data'];
  107. if ($selectData['status'] == '1') {
  108. $data = [
  109. 'total_fee' =>$selectData['trade_amount'],
  110. 'out_trade_no' => $this->getParameter('out_no'),
  111. 'trade_no' => $this->getParameter('ord_no'),
  112. 'unit' => 'fen',
  113. 'pay_type' => '支付宝支付--聚合支付',
  114. 'pay_type_id' => PayFactory::STORE_AGGREGATE_ALIPAY,
  115. ];
  116. $this->payResutl($data);
  117. echo 'notify_success'; exit();
  118. }
  119. \Log::debug('<----------支付宝支付--聚合支付回调--交易未成功--->'. $this->getParameter('out_no'), $this->getAllParameter());
  120. echo 'fail';exit();
  121. }
  122. \Log::debug('---------支付宝支付--聚合支付回调--支付失败---'. $this->getParameter('out_no'), $this->getAllParameter());
  123. echo 'fail';exit();
  124. }
  125. protected function thirdPartyOrder()
  126. {
  127. $account = $this->getStoreAccount();
  128. $requestData = [
  129. 'open_id' => $account['open_id'],
  130. 'timestamp' => time(),
  131. 'data' => ['out_no' => $this->getParameter('out_no'),]
  132. ];
  133. return (new Client($account))->post('paystatus', $requestData);
  134. }
  135. /**
  136. * 获取参数值
  137. * @param string $key
  138. * @return string
  139. */
  140. public function getParameter($key)
  141. {
  142. return array_get($this->responseParameters, $key, '');
  143. }
  144. public function getAllParameter()
  145. {
  146. return $this->responseParameters;
  147. }
  148. protected function getStoreAccount()
  149. {
  150. if (isset($this->account)) {
  151. return $this->account;
  152. }
  153. $this->account = AggregatePayAccount::select('store_id', 'open_id', 'open_key')->where('store_id', $this->payLog['store_id'])->first();
  154. return $this->account;
  155. }
  156. /**
  157. * @param $array
  158. * @return bool
  159. */
  160. public function checkSign($array)
  161. {
  162. $sign = $array['sign'];#得到返回签名字符串
  163. unset($array['sign']);#去掉sign节点
  164. $array['open_key'] = $this->getStoreAccount()['open_key'];#加上open_key节点
  165. ksort($array);#排序
  166. $arr_temp = array();
  167. foreach ($array as $key => $val) {
  168. $arr_temp[] = $key . '=' . $val;
  169. }
  170. $sign_str = implode('&', $arr_temp);
  171. $sign_str = md5(sha1($sign_str));
  172. if ($sign != $sign_str) {
  173. return false;
  174. } else {
  175. return true;
  176. }
  177. }
  178. /**
  179. * 支付日志
  180. * @param $out_trade_no
  181. * @param $data
  182. * @param string $msg
  183. */
  184. public function log($out_trade_no, $data, $msg = '聚合支付')
  185. {
  186. //访问记录
  187. Pay::payAccessLog();
  188. //保存响应数据
  189. Pay::payResponseDataLog($out_trade_no, $msg, json_encode($data));
  190. }
  191. }