Ecardnotify.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\AllwinEcard;
  11. use app\allwin\model\AllwinEcardOrder;
  12. use app\common\facade\Inform;
  13. use app\common\facade\WechatPay;
  14. use app\allwin\model\Bank;
  15. use app\allwin\model\BankBill;
  16. use filter\Filter;
  17. use Exception;
  18. class Ecardnotify extends Base{
  19. /**
  20. * 购买商品通知
  21. * @return void
  22. */
  23. public function goods(){
  24. try{
  25. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  26. $result = AllwinEcardOrder::where(['status'=>0,'paid_at'=>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. AllwinEcardOrder::where(['id' => $result->id])->update(['status' => 1,'paid_at' => 1,'paid_time' => strtotime($ispay['time_end']),'paid_no' => $ispay['transaction_id']]);
  38. //会员卡
  39. $ecard = AllwinEcard::where(['member_miniapp_id' => $this->miniapp_id])->find();
  40. //增加帐单
  41. BankBill::add(['miniapp_id'=>$this->miniapp_id,'store_id' => 0,'money' => -$result->amount,'uid'=>$result->uid,'pay_uid' => $result->uid], '微信支付[购买会员卡]');
  42. //增加积分
  43. Bank::rePoints($this->miniapp_id, $result->user_id,intval($result->amount));
  44. //申请者微信通知
  45. Inform::sms($result->user_id,$this->miniapp_id,['title' =>'业务进展通知','type' => '会员卡','content' =>'您已成功购买'.$ecard['title']]);
  46. return true;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. return $fail('通信失败,请稍后再通知我');
  53. });
  54. $response->send();
  55. }catch (Exception $e) {
  56. $this->error('页面不存在');
  57. }
  58. }
  59. }