AlipayNotifyController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\controllers;
  8. use app\models\Option;
  9. use app\models\Store;
  10. use app\modules\common\models\AlipayNotifyForm;
  11. use app\modules\common\models\NotifyForm;
  12. use app\plugins\food\models\FoodOrder;
  13. use app\utils\Alipay\Alipay;
  14. use app\utils\Alipay\AlipayProfit;
  15. use app\utils\Alipay\AlipayUnion;
  16. use Yansongda\Pay\Log;
  17. use yii\helpers\Json;
  18. use yii\web\Controller;
  19. use AopClient;
  20. use yii\log\Logger;
  21. use app\utils\IotCloudHelper;
  22. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  23. /**
  24. * 支付宝回调类
  25. * Class AlipayNotifyController
  26. * @package app\modules\common\controllers
  27. */
  28. class AlipayNotifyController extends Controller
  29. {
  30. private $validShopMsgType = [
  31. 'ant.merchant.expand.shop.save.passed',
  32. 'ant.merchant.expand.shop.save.rejected'
  33. ];
  34. /**
  35. * 入口文件
  36. */
  37. public function actionIndex() {
  38. $store_id = get_params('store_id');
  39. if(!empty($store_id)){
  40. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming(get_store_id()) && self_mini() === false) {
  41. Alipay::init($store_id);
  42. $this->notify(Alipay::$alipay, $store_id);
  43. }
  44. if (\Yii::$app->isSaas()) {
  45. AlipayUnion::init($store_id);
  46. $this->notify(AlipayUnion::$alipay, $store_id);
  47. } else {
  48. Alipay::init('', 1, $store_id);
  49. $this->notify(Alipay::$alipay, $store_id);
  50. }
  51. }else{
  52. $aop = new AopClient();
  53. $aop->postCharset="GBK";
  54. $aop->rsaPrivateKey = Option::get("alipay_app_private_key",0,'saas')['value'];
  55. $aop->alipayrsaPublicKey=Option::get("alipay_public_key",0,'saas')['value'];
  56. $res = $aop->rsaCheckV2(post_params(),NULL,"RSA2");
  57. if ($res) {
  58. $response_xml = "<success>true</success><biz_content>" . $aop->alipayrsaPublicKey . "</biz_content>";
  59. } else {
  60. $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $aop->alipayrsaPublicKey . "</biz_content>";
  61. }
  62. $mysign = $aop->alonersaSign($response_xml, $aop->rsaPrivateKey, "RSA2");
  63. $return_xml = "<?xml version=\"1.0\" encoding=\"GBK\"?><alipay><response>" . $response_xml . "</response><sign>" . $mysign . "</sign><sign_type>RSA2</sign_type></alipay>";
  64. \Yii::error("response_xml: " . $return_xml);
  65. echo $return_xml;
  66. exit ();
  67. }
  68. }
  69. /**
  70. * @param \Yansongda\Pay\Gateways\Alipay $alipay
  71. * @param $store_id
  72. * 回调逻辑处理
  73. * @return mixed
  74. */
  75. private function notify($alipay, $store_id) {
  76. try{
  77. $data = $alipay->verify();
  78. if (!is_array($data)) {
  79. $data = $data->toArray();
  80. }
  81. Log::debug('ALIPAY NOTIFY DATA <==========> ', $data);
  82. // 蚂蚁门店创建通知消息
  83. if (isset($data['msg_method']) && in_array($data['msg_method'], $this->validShopMsgType)) {
  84. $this->shop_notify($data);
  85. return $alipay->success()->send();
  86. }
  87. if ($data['trade_status'] != 'TRADE_SUCCESS') {
  88. return;
  89. }
  90. if (isset($data['total_amount']) && isset($data['gmt_payment'])) {
  91. $store = Store::findOne($store_id);
  92. if ($store && !empty($store->device_name)) {
  93. $total_fee = $data['total_amount'];
  94. $payment_time = strtotime($data['gmt_payment']);
  95. IotCloudHelper::sendMessage($store_id, '{"cmd":"voice","msg":"支付宝收款'.$total_fee.'元","msgid":"'.$payment_time.'"}');
  96. }
  97. }
  98. $orderNoHead = substr($data['out_trade_no'], 0, 2);
  99. $notify = new AlipayNotifyForm();
  100. switch ($orderNoHead) {
  101. case 'UN':
  102. // 合并支付的订单
  103. $notify->UnionOrderNotify($data);
  104. break;
  105. case 'RG':
  106. // 充值订单
  107. $notify->RechargeOrderNotify($data);
  108. break;
  109. case 'LV':
  110. // 会员购买
  111. $notify->LevelOrderNotify($data);
  112. break;
  113. case 'ML':
  114. // 商城订单
  115. $notify->MallOrderNotify($data);
  116. break;
  117. case 'MC':
  118. // 入住商提现订单
  119. $notify->batchTransNotify($data);
  120. break;
  121. case 'SC':
  122. // 当面付订单
  123. $notify->ScanOrderNotify($data);
  124. break;
  125. case 'FO':
  126. // 点餐订单
  127. $notify->FoodNotify($data);
  128. break;
  129. case 'AD':
  130. // 认养费用订单
  131. $notify->AdoptCostOrderNotify($data);
  132. break;
  133. case 'AL':
  134. // 商盟订单
  135. $notify->BusinessNotify($data);
  136. break;
  137. default:
  138. break;
  139. }
  140. return $alipay->success()->send();
  141. } catch (\Exception $e) {
  142. Log::debug('ALIPAY NOTIFY EXCEPTION <==========> ', [$e->getMessage()]);
  143. }
  144. }
  145. /**
  146. * 蚂蚁门店审核消息回调
  147. * @param $result
  148. */
  149. private function shop_notify($result) {
  150. if ($result['msg_method'] == 'ant.merchant.expand.shop.save.passed') {
  151. $store_id = substr(Json::decode($result['biz_content'])['store_id'], 2) + 0;
  152. $store = Store::findOne($store_id);
  153. if ($store) {
  154. $store->shop_id = Json::decode($result['biz_content'])['shop_id'];
  155. $store->save();
  156. }
  157. }
  158. }
  159. }