| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\common\controllers;
- use app\models\Option;
- use app\models\Store;
- use app\modules\common\models\AlipayNotifyForm;
- use app\modules\common\models\NotifyForm;
- use app\plugins\food\models\FoodOrder;
- use app\utils\Alipay\Alipay;
- use app\utils\Alipay\AlipayProfit;
- use app\utils\Alipay\AlipayUnion;
- use Yansongda\Pay\Log;
- use yii\helpers\Json;
- use yii\web\Controller;
- use AopClient;
- use yii\log\Logger;
- use app\utils\IotCloudHelper;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- /**
- * 支付宝回调类
- * Class AlipayNotifyController
- * @package app\modules\common\controllers
- */
- class AlipayNotifyController extends Controller
- {
- private $validShopMsgType = [
- 'ant.merchant.expand.shop.save.passed',
- 'ant.merchant.expand.shop.save.rejected'
- ];
- /**
- * 入口文件
- */
- public function actionIndex() {
- $store_id = get_params('store_id');
- if(!empty($store_id)){
- if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming(get_store_id()) && self_mini() === false) {
- Alipay::init($store_id);
- $this->notify(Alipay::$alipay, $store_id);
- }
- if (\Yii::$app->isSaas()) {
- AlipayUnion::init($store_id);
- $this->notify(AlipayUnion::$alipay, $store_id);
- } else {
- Alipay::init('', 1, $store_id);
- $this->notify(Alipay::$alipay, $store_id);
- }
- }else{
- $aop = new AopClient();
- $aop->postCharset="GBK";
- $aop->rsaPrivateKey = Option::get("alipay_app_private_key",0,'saas')['value'];
- $aop->alipayrsaPublicKey=Option::get("alipay_public_key",0,'saas')['value'];
- $res = $aop->rsaCheckV2(post_params(),NULL,"RSA2");
- if ($res) {
- $response_xml = "<success>true</success><biz_content>" . $aop->alipayrsaPublicKey . "</biz_content>";
- } else {
- $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $aop->alipayrsaPublicKey . "</biz_content>";
- }
- $mysign = $aop->alonersaSign($response_xml, $aop->rsaPrivateKey, "RSA2");
- $return_xml = "<?xml version=\"1.0\" encoding=\"GBK\"?><alipay><response>" . $response_xml . "</response><sign>" . $mysign . "</sign><sign_type>RSA2</sign_type></alipay>";
- \Yii::error("response_xml: " . $return_xml);
- echo $return_xml;
- exit ();
- }
- }
- /**
- * @param \Yansongda\Pay\Gateways\Alipay $alipay
- * @param $store_id
- * 回调逻辑处理
- * @return mixed
- */
- private function notify($alipay, $store_id) {
- try{
- $data = $alipay->verify();
- if (!is_array($data)) {
- $data = $data->toArray();
- }
- Log::debug('ALIPAY NOTIFY DATA <==========> ', $data);
- // 蚂蚁门店创建通知消息
- if (isset($data['msg_method']) && in_array($data['msg_method'], $this->validShopMsgType)) {
- $this->shop_notify($data);
- return $alipay->success()->send();
- }
- if ($data['trade_status'] != 'TRADE_SUCCESS') {
- return;
- }
- if (isset($data['total_amount']) && isset($data['gmt_payment'])) {
- $store = Store::findOne($store_id);
- if ($store && !empty($store->device_name)) {
- $total_fee = $data['total_amount'];
- $payment_time = strtotime($data['gmt_payment']);
- IotCloudHelper::sendMessage($store_id, '{"cmd":"voice","msg":"支付宝收款'.$total_fee.'元","msgid":"'.$payment_time.'"}');
- }
- }
- $orderNoHead = substr($data['out_trade_no'], 0, 2);
- $notify = new AlipayNotifyForm();
- switch ($orderNoHead) {
- case 'UN':
- // 合并支付的订单
- $notify->UnionOrderNotify($data);
- break;
- case 'RG':
- // 充值订单
- $notify->RechargeOrderNotify($data);
- break;
- case 'LV':
- // 会员购买
- $notify->LevelOrderNotify($data);
- break;
- case 'ML':
- // 商城订单
- $notify->MallOrderNotify($data);
- break;
- case 'MC':
- // 入住商提现订单
- $notify->batchTransNotify($data);
- break;
- case 'SC':
- // 当面付订单
- $notify->ScanOrderNotify($data);
- break;
- case 'FO':
- // 点餐订单
- $notify->FoodNotify($data);
- break;
- case 'AD':
- // 认养费用订单
- $notify->AdoptCostOrderNotify($data);
- break;
- case 'AL':
- // 商盟订单
- $notify->BusinessNotify($data);
- break;
- default:
- break;
- }
- return $alipay->success()->send();
- } catch (\Exception $e) {
- Log::debug('ALIPAY NOTIFY EXCEPTION <==========> ', [$e->getMessage()]);
- }
- }
- /**
- * 蚂蚁门店审核消息回调
- * @param $result
- */
- private function shop_notify($result) {
- if ($result['msg_method'] == 'ant.merchant.expand.shop.save.passed') {
- $store_id = substr(Json::decode($result['biz_content'])['store_id'], 2) + 0;
- $store = Store::findOne($store_id);
- if ($store) {
- $store->shop_id = Json::decode($result['biz_content'])['shop_id'];
- $store->save();
- }
- }
- }
- }
|