123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace app\client\controller;
- require_once __DIR__ . "/../../../extend/wx/pay/WxPay.Config.php";
- require_once __DIR__ . "/../../../extend/wx/pay/lib/WxPay.Data.php";
- require_once __DIR__ . "/../../../extend/wx/pay/lib/WxPay.Api.php";
- use app\model\ActivityOrder;
- use WxPayApi;
- use WxPayConfig;
- use WxPayNotifyReply;
- use WxPayUnifiedOrder;
- class WxNotify
- {
- private $reply;
- private $config;
- public function __construct()
- {
- $this->reply = new WxPayNotifyReply();
- }
- public function catch($action)
- {
- // 根据action 获取用户配置的商户号
- $config = [];
- $this->config = new WxPayConfig([
- 'app_id' => $config['mini_id'],
- 'mch_id' => $config['mch_id'],
- 'key' => $config['mch_key']
- ]);
- $this->run();
- }
- private function run()
- {
- // 微信支付请求接口配置生成
- $msg = '';
- $result = WxPayApi::notify($this->config, array($this, 'dealLogic'), $msg);
- if ($result == false) {
- $this->reply->SetReturn_code("FAIL");
- $this->reply->SetReturn_msg($msg);
- $this->ReplyNotify(false);
- return;
- } else {
- //该分支在成功回调到NotifyCallBack方法,处理完成之后流程
- $this->reply->SetReturn_code("SUCCESS");
- $this->reply->SetReturn_msg("OK");
- }
- $this->ReplyNotify(true);
- }
- /**
- * 回掉处理逻辑
- * @return boolean
- */
- public function dealLogic($WxPayNotifyResults)
- {
- $data = $WxPayNotifyResults->GetValues();
- $method = $data['attach'];
- if (!method_exists($this, $method)) {
- return false;
- }
- return $this->$method($data);
- }
- /**
- *
- * 回复通知
- * @param bool $needSign 是否需要签名输出
- */
- final private function ReplyNotify($needSign)
- {
- //如果需要签名
- if (
- $needSign == true &&
- $this->reply->GetReturn_code() == "SUCCESS"
- ) {
- $this->reply->SetSign($this->config);
- }
- $xml = $this->reply->ToXml();
- WxPayApi::replyNotify($xml);
- }
- /**
- * 查询订单是否支付成功
- */
- private function orderQuery($orderNo, $appidType = 'mini_appid')
- {
- $input = new WxPayUnifiedOrder();
- $input->SetOut_trade_no($orderNo);
- $response = \WxPayApi::orderQuery($this->config, $input, 12);
- if ($response['return_code'] != 'SUCCESS' || $response['result_code'] != 'SUCCESS' || $response['trade_state'] != 'SUCCESS') {
- trace($response, 'error');
- return false;
- }
- return true;
- }
- /**
- * 活动订单支付
- */
- public function activityOrder($data)
- {
- if (!$this->orderQuery($data['out_trade_no'])) return false;
- // 订单查找
- $order = ActivityOrder::where('order_no', $data['out_trade_no'])->find();
- // 更改订单状态
- $order->state = 1;
- $order->save();
- // 更改线索或客户的状态
- // 发送客户交定通知
- return true;
- }
- }
|