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 = "true" . $aop->alipayrsaPublicKey . "";
} else {
$response_xml = "falseVERIFY_FAILED" . $aop->alipayrsaPublicKey . "";
}
$mysign = $aop->alonersaSign($response_xml, $aop->rsaPrivateKey, "RSA2");
$return_xml = "" . $response_xml . "" . $mysign . "RSA2";
\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();
}
}
}
}