Vipnotify.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\allwin\model\Vip;
  11. use app\allwin\model\Bank;
  12. use app\allwin\model\BankBill;
  13. use app\allwin\model\CouponUser;
  14. use app\allwin\widget\Vip as VipIncome;
  15. use app\common\facade\WechatPay;
  16. use app\common\facade\Inform;
  17. use filter\Filter;
  18. use Exception;
  19. class Vipnotify extends Base{
  20. public function index(){
  21. try {
  22. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  23. $result = Vip::where(['state' => 0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  24. if (empty($result)) {
  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($result->order_no);
  31. if ($ispay['return_code'] === 'SUCCESS') {
  32. if ($ispay['result_code'] === 'SUCCESS') {
  33. if ($ispay['trade_state'] === 'SUCCESS'){
  34. //锁定以前的会员
  35. Vip::where(['user_id' => $result->user_id,'state' => 1])->update(['is_lock' => 1]);
  36. //修改当前会员开通成功
  37. Vip::where(['id' => $result->id])->update(['state' => 1,'paid_time' => time(),'paid_no' => $ispay['transaction_id']]);
  38. //领取的优惠券
  39. CouponUser::addUserCoupon($result->coupon_ids,$result->user_id);
  40. //消费日志
  41. BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'money' => -$result->amount,'uid'=>$result->user_id,'pay_uid' => $result->user_id], '微信支付[会员开通]');
  42. if(!empty($result->from_ucode)){
  43. VipIncome::register($result->user_id,$result->from_ucode); //增加邀请关系
  44. }
  45. VipIncome::income($result->toArray());
  46. Bank::rePoints($this->miniapp_id, $result->user_id, intval($result->amount)); //分配消费积分
  47. //微信通知到申请者
  48. if(!empty($result->vipcard)){
  49. Inform::sms($result->user_id,$this->miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为'.$result->vipcard['name']]);
  50. }
  51. return true;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. return $fail('通信失败,请稍后再通知我');
  58. });
  59. $response->send();
  60. }catch (Exception $e) {
  61. $this->error('页面不存在');
  62. }
  63. }
  64. }