Paymentnotify.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Order;
  12. use app\allwin\model\Bank;
  13. use app\allwin\model\BankBill;
  14. use app\allwin\model\AllwinGiftData;
  15. use app\allwin\model\CouponUser;
  16. use app\allwin\model\Vip;
  17. use app\allwin\widget\Order as OrderIncome;
  18. use filter\Filter;
  19. use Exception;
  20. class Paymentnotify extends Base{
  21. /**
  22. * 微信买单消费通知
  23. * @return void
  24. */
  25. public function index(){
  26. try {
  27. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  28. $result = Order::where(['state'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  29. if (empty($result)) {
  30. return true;
  31. }
  32. if ($message['return_code'] === 'SUCCESS') {
  33. if ($message['result_code'] === 'SUCCESS') {
  34. $sub_mchid = $message['sub_mch_id'] ?? '';
  35. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($result->order_no);
  36. if ($ispay['return_code'] === 'SUCCESS') {
  37. if ($ispay['result_code'] === 'SUCCESS') {
  38. if ($ispay['trade_state'] === 'SUCCESS'){
  39. //订单状态
  40. $result->state = 1;
  41. $result->paid_time = strtotime($ispay['time_end']);
  42. $result->paid_no = $ispay['transaction_id'];
  43. $result->save();
  44. //收益计算
  45. OrderIncome::income($result->toArray());
  46. //消费日志
  47. BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'money' => -$result->order_amount,'uid' => $result->uid,'pay_uid' => $result->uid],'微信支付[消费]');
  48. //支付有礼
  49. AllwinGiftData::giveGift($result,$this->miniapp_id);
  50. //积分奖励
  51. Bank::rePoints($this->miniapp_id,$result->uid,intval($result->order_amount));
  52. //领取优惠券
  53. CouponUser::addUserCoupon($result->coupon_ids,$result->uid); //领取的优惠券
  54. //使用优惠券
  55. if ($result->coupon_user_id > 0) {
  56. CouponUser::userCouponPay($result); //优惠券减少
  57. OrderIncome::fund($result->toArray()); //采购基金补贴
  58. }
  59. //买单即开通会员
  60. Vip::regVip($result);
  61. return true;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. return $fail('通信失败,请稍后再通知我');
  68. });
  69. $response->send();
  70. }catch (Exception $e) {
  71. $this->error('页面不存在');
  72. }
  73. }
  74. }