Couponnotify.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Bank;
  11. use app\allwin\model\BankBill;
  12. use app\allwin\model\CouponUser;
  13. use app\allwin\model\CouponOrder;
  14. use app\common\facade\WechatPay;
  15. use Exception;
  16. class couponNotify extends Base{
  17. /**
  18. * 付费领取优惠券
  19. */
  20. public function index(){
  21. try{
  22. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  23. $result = CouponOrder::where(['state'=>0,'order_no' => $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. $result->state = 1;
  35. $result->paid_time = strtotime($ispay['time_end']);;
  36. $result->paid_no = $ispay['transaction_id'];
  37. $result->save();
  38. CouponUser::addUserCoupon($result->coupon_ids,$result->user_id); //领取的优惠券入库
  39. //增加日志
  40. BankBill::add(['miniapp_id'=>$this->miniapp_id,'store_id' => 0,'money' => -$result->amount,'uid'=>$result->user_id,'pay_uid' => $result->user_id], '微信支付[领取优惠券]');
  41. //开通收益计算
  42. Bank::rePoints($this->miniapp_id, $result->user_id,intval($result->amount)); //增加积分
  43. return true;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. return $fail('通信失败,请稍后再通知我');
  50. });
  51. $response->send();
  52. }catch (Exception $e) {
  53. $this->error('页面不存在');
  54. }
  55. }
  56. }