Infonotify.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\AllwinInfo;
  13. use app\allwin\model\AllwinInfoOrder;
  14. use app\allwin\model\AllwinConfig;
  15. use app\common\facade\WechatPay;
  16. use filter\Filter;
  17. use Exception;
  18. class Infonotify extends Base{
  19. /**
  20. * 发布同城通知
  21. * @return void
  22. */
  23. public function index(){
  24. try {
  25. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
  26. $order = AllwinInfo::where(['is_pay'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  27. if (empty($order)) {
  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($order->order_no);
  34. if ($ispay['return_code'] === 'SUCCESS') {
  35. if ($ispay['result_code'] === 'SUCCESS') {
  36. if ($ispay['trade_state'] === 'SUCCESS') {
  37. //修改发布状态
  38. AllwinInfo::where(['id' => $order->id])->update(['is_pay' => 1,'is_top' => 1,'is_pay_time' => strtotime($ispay['time_end'])]);
  39. //开通收益计算
  40. BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => 0,'money' => -money($order->top_money/100),'uid' => $order->user_id,'pay_uid' => $order->user_id], '发布信息');
  41. //积分奖励
  42. Bank::rePoints($this->miniapp_id, $order->user_id, intval($order->top_money/100)); //增加积分
  43. //分享给推荐人
  44. AllwinInfo::sendinfo($order);
  45. return true;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. return $fail('通信失败,请稍后再通知我');
  52. });
  53. $response->send();
  54. }catch (Exception $e) {
  55. $this->error('页面不存在');
  56. }
  57. }
  58. /**
  59. * 订购
  60. * @return void
  61. */
  62. public function order(){
  63. try {
  64. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
  65. $order = AllwinInfoOrder::where(['paid_at'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  66. if (empty($order)) {
  67. return true;
  68. }
  69. if ($message['return_code'] === 'SUCCESS') {
  70. if($message['result_code'] === 'SUCCESS'){
  71. $sub_mchid = $message['sub_mch_id'] ?? '';
  72. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($order->order_no);
  73. if ($ispay['return_code'] === 'SUCCESS') {
  74. if ($ispay['result_code'] === 'SUCCESS') {
  75. if ($ispay['trade_state'] === 'SUCCESS') {
  76. $setting = AllwinConfig::getConfig($this->miniapp_id);
  77. $default_charges = $setting->default_charges/1000; //默认手续费
  78. $wechat_charges_amout = money(abs($order->amount*$default_charges)); //微信扣除的服务费(厘四舍五入到分)
  79. $amount = money($order->amount-$wechat_charges_amout);
  80. Bank::recharge($this->miniapp_id,$order->uid,$amount); //增加收益
  81. BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => 0,'money' => $amount,'uid' => $order->uid,'pay_uid' => $order->uid], '城市号用户下单');
  82. AllwinInfoOrder::where(['id' => $order->id])->update(['paid_at' => 1,'paid_no' => $ispay['transaction_id'],'paid_time' => strtotime($ispay['time_end'])]);
  83. return true;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return $fail('通信失败,请稍后再通知我');
  90. });
  91. $response->send();
  92. }catch (Exception $e) {
  93. $this->error('页面不存在');
  94. }
  95. }
  96. }