|
@@ -0,0 +1,183 @@
|
|
|
+<?php namespace Porder\Lib;
|
|
|
+use Dever;
|
|
|
+class Notify
|
|
|
+{
|
|
|
+ # 支付回调开始操作 无状态 一般用来做环境处理
|
|
|
+ public function handle_start($sector_id, $place_id, $type, $order_num)
|
|
|
+ {
|
|
|
+ //Dever::input('authorization', 'is_string', '访问码', $place);
|
|
|
+ //Dever::input('authorization', 'set', $place);
|
|
|
+ Dever::load(\Manage\Lib\Util::class)->setAuth('sector', 'sector_place', $sector_id, $place_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ # 支付回调结束操作
|
|
|
+ public function handle_end_commit(){}
|
|
|
+ public function handle_end($sector_id, $place_id, $type, $order_num, $status, $body = [])
|
|
|
+ {
|
|
|
+ $order = Dever::db('porder/' . $type)->find(['order_num' => $order_num]);
|
|
|
+ if ($order && $order['status'] == 1) {
|
|
|
+ if ($status == 1) {
|
|
|
+ # 抵扣
|
|
|
+ $this->deduct($type, $order);
|
|
|
+ }
|
|
|
+ $method = $type . '_callback';
|
|
|
+ $this->$method($order, $status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 抵扣
|
|
|
+ private function deduct($order)
|
|
|
+ {
|
|
|
+ # 使用优惠券
|
|
|
+ if (isset($order['user_coupon_id']) && $order['user_coupon_id'] > 0) {
|
|
|
+ Dever::load(\Puser\Lib\Coupon::class)->use($type, $order, $this->set['score']);
|
|
|
+ }
|
|
|
+ # 使用礼品卡
|
|
|
+ if (isset($order['user_gift_id']) && $order['user_gift_id'] > 0) {
|
|
|
+ Dever::load(\Puser\Lib\Gift::class)->use($type, $order, $this->set['score']);
|
|
|
+ }
|
|
|
+ # 使用钱包支付
|
|
|
+ if (isset($order['wallet_cash']) && $order['wallet_cash'] > 0) {
|
|
|
+ $name = Dever::db('porder/' . $type . '_detail')->columns(['order_id' => $order['id']], 'name');
|
|
|
+ Dever::load(\Pscore\Lib\Log::class)->action('购买', $order['score_id'])->add($order['uid'], implode('、', $name), $order['wallet_cash']*-1);
|
|
|
+ }
|
|
|
+ # 返利
|
|
|
+ $rebate = Dever::db('porder/rebate')->select(['table' => $type, 'table_id' => $order['id']]);
|
|
|
+ if ($rebate) {
|
|
|
+ $status = 1;
|
|
|
+ if ($type == 'source') {
|
|
|
+ $status = 2;
|
|
|
+ }
|
|
|
+ foreach ($rebate as $k => $v) {
|
|
|
+ if ($v['level'] == 0) {
|
|
|
+ # 给自己返利
|
|
|
+ Dever::load(\Pscore\Lib\Log::class)->action('返利', $v['score_id'])->add($order['uid'], $v['name'], $v['value'], $status, $type . '_order_' . $order['id']);
|
|
|
+ } else {
|
|
|
+ # 给上级返利
|
|
|
+ $parent = Dever::load(\Invite\Lib\Relation::class)->getParent($order['uid'], $v['level']);
|
|
|
+ if ($parent) {
|
|
|
+ Dever::load(\Pscore\Lib\Log::class)->action('返利', $v['score_id'])->add($parent['uid'], $v['name'], $v['value'], $status, $type . '_order_' . $order['id']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 资源支付成功回调
|
|
|
+ protected function source_callback($order, $status)
|
|
|
+ {
|
|
|
+ if ($status == 1) {
|
|
|
+ $update['status'] = 2;
|
|
|
+ $msg = '支付成功';
|
|
|
+ } else {
|
|
|
+ $update['status'] = 7;
|
|
|
+ $msg = '支付失败';
|
|
|
+ }
|
|
|
+ $update['pdate'] = time();
|
|
|
+ $state = Dever::db('porder/source')->update($order['id'], $update);
|
|
|
+ if (!$state) {
|
|
|
+ return '订单更新失败';
|
|
|
+ }
|
|
|
+ if ($order['pay_cash'] > 0) {
|
|
|
+ Dever::load(\Porder\Lib\Source\Log::class)->up(1, $order['uid'], $order['id'], $msg);
|
|
|
+ }
|
|
|
+ if ($state) {
|
|
|
+ if ($update['status'] == 7) {
|
|
|
+ # 恢复库存
|
|
|
+ Dever::load(\Pstock\Lib\Info::class)->refundAll($order);
|
|
|
+ } elseif ($order['status'] == 2) {
|
|
|
+ # 自动发货
|
|
|
+ $num = Dever::db('porder/source_detail')->count(['order_id' => $order['id'], 'source_type' => 10]);
|
|
|
+ if ($num) {
|
|
|
+ $total = Dever::db('porder/source_detail')->count(['order_id' => $order['id']]);
|
|
|
+ if ($total == $num) {
|
|
|
+ # 不发货,订单直接完成即可
|
|
|
+ Dever::load(\Porder\Lib\Source\Order::class)->finish(1, $order, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 角色支付回调
|
|
|
+ protected function role_callback($order, $status)
|
|
|
+ {
|
|
|
+ if ($status == 1) {
|
|
|
+ $update['status'] = 2;
|
|
|
+ $msg = '支付成功';
|
|
|
+ } else {
|
|
|
+ $update['status'] = 3;
|
|
|
+ $msg = '支付失败';
|
|
|
+ }
|
|
|
+
|
|
|
+ $update['pdate'] = time();
|
|
|
+ $state = Dever::db('porder/role')->update($order['id'], $update);
|
|
|
+ if (!$state) {
|
|
|
+ return '订单更新失败';
|
|
|
+ }
|
|
|
+ if ($order['pay_cash'] > 0) {
|
|
|
+ Dever::load(\Porder\Lib\Role\Log::class)->up(1, $order['uid'], $order['id'], $msg);
|
|
|
+ }
|
|
|
+ if ($status == 1) {
|
|
|
+ $detail = Dever::db('porder/role_detail')->select(['order_id' => $order['id']], ['order' => 'level asc']);
|
|
|
+ if ($detail) {
|
|
|
+ foreach ($detail as $v) {
|
|
|
+ Dever::load(\Prole\Lib\User::class)->up($order['uid'], $v['role_id'], $v['level_id'], $order['sales_type'], $order['sales_id'], 1, $order['id'], '订单号:' . $order['order_num']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->sales($order, $this->type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 发放销售渠道收益
|
|
|
+ public function sales($order, $type = 'source')
|
|
|
+ {
|
|
|
+ if ($order['sales_type'] && $order['sales_id']) {
|
|
|
+ $sales = Dever::load(\Psales\Lib\Info::class)->get($order['sales_type'], $order['sales_id']);
|
|
|
+ if ($sales && $sales['uid']) {
|
|
|
+ $benefit = Dever::db('pbenefit/sales')->find(['sales_cate_id' => $sales['cate_id'], 'status' => 1]);
|
|
|
+ if ($benefit && $benefit['score_id']) {
|
|
|
+ # 平台发货
|
|
|
+ $table = 'sales_rebate_platform';
|
|
|
+ if (isset($order['method']) && $order['method'] >= 2) {
|
|
|
+ # 店铺发货
|
|
|
+ $table = 'sales_rebate';
|
|
|
+ }
|
|
|
+ $rebate = Dever::db('pbenefit/' . $table)->select(['sales_id' => $benefit['id']]);
|
|
|
+ if ($rebate) {
|
|
|
+ $detail = Dever::db('porder/' . $type . '_detail')->select(['order_id' => $order['id']]);
|
|
|
+ $score = Dever::load(\Pscore\Lib\Info::class)->get($order['score_id']);
|
|
|
+ foreach ($rebate as $k => $v) {
|
|
|
+ # 先检测购买人等级
|
|
|
+ if ($v['role']) {
|
|
|
+ $t = explode(',', $v['role']);
|
|
|
+ $check = Dever::load(\Prole\Lib\User::class)->check($order['uid'], $t[0], $t[1]);
|
|
|
+ if (!$check) {
|
|
|
+ unset($rebate[$k]);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $value = 0;
|
|
|
+ foreach ($detail as $k => $v) {
|
|
|
+ if ($v['pay_cash'] > 0) {
|
|
|
+ if (isset($v['refund_cash']) && $v['refund_cash'] > 0) {
|
|
|
+ $v['pay_cash'] -= $v['refund_cash'];
|
|
|
+ }
|
|
|
+ if ($v['pay_cash'] > 0) {
|
|
|
+ $money = Dever::math('mul', $v['pay_cash'], $score['exp']);
|
|
|
+ $value += Dever::load(\Pbenefit\Lib\Item::class)->load('sales')->rebate($order['uid'], $v['scope'], $v['promotion_id'], $rebate, $money);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($value > 0) {
|
|
|
+ # 给关联用户发收益
|
|
|
+ Dever::load(\Pscore\Lib\Log::class)->action('店铺收益', $benefit['score_id'])->add($sales['uid'], $sales['name'] . '订单:' . $order['order_num'], $value, 1, $type . '_order_' . $order['id'], $order['sales_type'], $order['sales_id']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|