Banknotify.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 支付回调通知
  7. */
  8. namespace app\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\common\facade\WechatPay;
  11. use app\allwin\model\BankRecharge;
  12. use app\allwin\model\BankBill;
  13. use app\allwin\model\Bank;
  14. use Exception;
  15. class bankNotify extends Base{
  16. /**
  17. * 充值成功通知
  18. * @return void
  19. */
  20. public function recharge(){
  21. try{
  22. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
  23. $order = BankRecharge::where(['state'=>0,'order_no' => $message['out_trade_no']])->find();
  24. if (empty($order)){
  25. return true;
  26. }
  27. if ($message['return_code'] === 'SUCCESS') {
  28. if($message['result_code'] === 'SUCCESS'){
  29. $sub_mchid = $message['sub_mch_id'] ?? '';
  30. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($order->order_no);
  31. if ($ispay['return_code'] === 'SUCCESS') {
  32. if ($ispay['result_code'] === 'SUCCESS') {
  33. if ($ispay['trade_state'] === 'SUCCESS') {
  34. //充值入账
  35. Bank::recharge($this->miniapp_id, $order->user_id, $order->money);
  36. //充值记录
  37. BankRecharge::where(['id' => $order->id])->update(['state' => 1,'paid_no' => $ispay['transaction_id'],'paid_time'=>strtotime($ispay['time_end'])]);
  38. //增加日志
  39. BankBill::add(['miniapp_id'=>$this->miniapp_id,'store_id' => 0,'money' => $order->money,'uid'=>$order->user_id,'pay_uid' => $order->user_id], '微信支付[钱包充值]');
  40. return true;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. return $fail('通信失败,请稍后再通知我');
  47. });
  48. $response->send();
  49. }catch (Exception $e) {
  50. $this->error('页面不存在');
  51. }
  52. }
  53. }