Notify.php 3.3 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\smartbc\controller\api\v1;
  9. use app\smartbc\controller\api\Base;
  10. use app\smartbc\model\SmartbcBill;
  11. use app\smartbc\model\SmartbcCouponUser;
  12. use app\smartbc\model\SmartbcOrder;
  13. use app\common\facade\WechatPay;
  14. use app\smartbc\model\SmartbcStoreBill;
  15. use filter\Filter;
  16. use think\facade\Log;
  17. use Exception;
  18. class Notify extends Base{
  19. /**
  20. * 微信付款买单
  21. * @return void
  22. */
  23. public function storePay(){
  24. try {
  25. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  26. $result = SmartbcOrder::where(['state' => 0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  27. if (empty($result)) {
  28. return true;
  29. }
  30. if ($message['return_code'] === 'SUCCESS') {
  31. if ($message['result_code'] === 'SUCCESS') {
  32. $sub_mchid = $message['sub_mch_id'] ?? '';
  33. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($result->order_no);
  34. if ($ispay['return_code'] === 'SUCCESS') {
  35. if ($ispay['result_code'] === 'SUCCESS') {
  36. if ($ispay['trade_state'] === 'SUCCESS'){
  37. //订单状态
  38. $result->state = 1;
  39. $result->paid_time = strtotime($ispay['time_end']);
  40. $result->paid_no = $ispay['transaction_id'];
  41. //收益计算
  42. $allMoney = money(($ispay['total_fee'] - $ispay['total_fee'] * 6 / 1000) / 100);
  43. $result = SmartbcOrder::income($result,$allMoney);
  44. //消费日志
  45. SmartbcBill::add(['member_miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'money' => -$result->price,'uid' => $result->uid],'微信支付[消费]');
  46. SmartbcStoreBill::add(['member_miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'store_chain_id' => $result->store_chain_id,'money' => $result->real,'pay_uid' => $result->uid],'微信支付[收款]');
  47. if ($result->coupon_user_id > 0) {
  48. SmartbcCouponUser::where(['id' => $result->coupon_user_id])->update(['is_end' => 1,'money' => money($result->amount - $result->price),'update_time' => time()]); //优惠券减少
  49. }
  50. $result->allowField(true)->save();
  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. }