123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- #新版本的异步支付
- namespace Act\Lib;
- use Dever;
- class Pay
- {
- private $key = 'jmss_2018';
- # 退款操作
- public function order($id, $name, $data)
- {
- $status = Dever::param('status', $data);
- if ($status == 5 && $id > 0) {
- $send = array();
- $info = Dever::db('act/order')->one($id);
- if ($info['system'] == 1) {
- if ($info['cate_id'] == 1) {
- $account_id = 1;
- }
- if ($info['cate_id'] == 2) {
- $account_id = 4;
- }
- if ($info['cate_id'] == 3) {
- $account_id = 5;
- }
- } elseif ($info['system'] == 2) {
- $account_id = 2;
- } else {
- $account_id = 3;
- }
- $send['pay_project_id'] = 3;
- $send['pay_uid'] = $info['uid'];
- $send['pay_order_id'] = $info['order_id'] ? $info['order_id'] : Dever::load('act/order')->createOrderId($info, $info['id']);
- $send['pay_tk_pic'] = Dever::param('tk_pic', $data);
- $send['pay_tk_time'] = Dever::param('tk_time', $data);
- $send['pay_tk_desc'] = Dever::param('tk_desc', $data);
- $send['pay_account_id'] = $account_id;
- $send['pay_cash'] = $info['cash'];
- $send['pay_status'] = $status;
- $send['dever_token'] = $this->key;
- Dever::load('pay/lib/set.updateStatus', $send);
- if ($info['buy_id'] > 0) {
- $buy = Dever::db('journal/buy_num')->one($info['buy_id']);
- if ($buy) {
- if ($info['buy_num'] > 0) {
- $num = $info['buy_num'];
- } else {
- $num = $buy['num'];
- }
- # 减少积分
- $score = $num * 20;
- $user = Dever::db('score/user')->one(array('uid' => $info['uid']));
- if ($user) {
- $user_id = $user['id'];
- $user_score = $user['score'];
- $user_score = $user_score - $score;
- if ($user_score < 0) {
- $user_score = 0;
- }
- Dever::db('score/user')->update(array('where_id' => $user_id, 'score' => $user_score));
- }
- # 减少排行榜
- $journal_num = Dever::db('act/journal_num')->one(array('uid' => $info['uid'], 'journal_id' => $info['product_id']));
- if ($journal_num) {
- $journal_num_id = $journal_num['id'];
- $journal_num = $journal_num['num'];
- $journal_num = $journal_num - $num;
- if ($journal_num < 0) {
- $journal_num = 0;
- }
- Dever::db('act/journal_num')->update(array('where_id' => $journal_num_id, 'num' => $journal_num));
- }
- # 减少订阅本数 这个定时跑就行了 不管了
- $journal = Dever::db('journal/info')->one($info['product_id']);
- if ($journal) {
- $journal_id = $journal['id'];
- $journal_num = $journal['num_ding'];
- $journal_num = $journal_num - $num;
- if ($journal_num < 0) {
- $journal_num = 0;
- }
- Dever::db('journal/info')->update(array('where_id' => $journal_id, 'num_ding' => $journal_num));
- }
- }
- }
- }
- return 'reload';
- }
- /**
- * 支付成功后,调取的接口 这里的安全以后再升级吧,升级成和pay/lib/set.updateStatus一样的
- *
- * @return mixed
- */
- public function act($param = array())
- {
- $this->act_action($param);
- return 'ok';
- $send = Dever::preInput('pay_');
- $send = $param;
- $key = md5($this->key);
- ksort($send);
- $send['signature'] = md5($key . '&' . http_build_query($send));
- $signature = Dever::input('signature');
- if ($send['signature'] == $signature) {
- $this->act_action($send);
- }
- return 'ok';
- }
- public function act_action($send)
- {
- //$product_id = $send['pay_product_id'];
- //$uid = $send['pay_uid'];
- //$cash = $send['pay_cash'];
- $order_id = $send['pay_order_id'];
- $status = $send['pay_status'];
- $msg = $send['pay_msg'];
- $temp = explode('D', $order_id);
- $id = $temp[1];
- //$order = Dever::db('act/order_temp')->one(array('order_id' => $order_id, 'uid' => $uid));
- $order = Dever::db('act/order_temp')->one($id);
- if ($send['pay_status'] == 2 && $order) {
- $update = $order;
- //$update['where_id'] = $order['id'];
- $update['status'] = 2;
- if ($order['type'] == 3 && !$order['code']) {
- if ($order['system'] == 2) {
- $code = Dever::load('code/lib/core')->createCodeByOrder($order);
- } else {
- $code = Dever::load('code/lib/core')->createCodeByOrder($order, -1);
- }
-
- if ($code) {
- $update['code'] = $code;
- }
- }
- Dever::db('act/order')->insert($update);
- $score = false;
- $num = false;
- $journal = Dever::load('act/order')->getJournal($order['product_id']);
- if ($order['buy_id'] > 0) {
- $buy = Dever::load('act/order')->getBuy($order['buy_id']);
- if ($buy && $buy['score'] > 0) {
- $score = $buy['num'] * $buy['score'];
- } else {
- if ($order['type'] == 1) {
- # 直接购买
- $col = 'score';
- } elseif ($order['type'] == 3) {
- $col = 'dh_score';
- }
- if ($journal && $buy && $journal[$col] > 0) {
- $score = $buy['num'] * $journal[$col];
- } elseif ($journal && $buy) {
- $num = $buy['num'];
- }
- }
- if ($buy && $buy['num']) {
- $active = Dever::load('act/order')->getActive($order['product_id']);
- $active_state = true;
- if ($active && $active['status'] == 1 && time() > $active['end']) {
- $active_state = false;
- }
- if ($active_state) {
- # 增加本数
- Dever::load('act/lib/num')->submit($order['product_id'], $order['uid'], $buy['num']);
- }
- # 增加订阅数
- //Dever::load('act/lib/num')->setCounter($order['product_id'], $buy['num']);
- }
- }
- # 积分
- Dever::score($order['uid'], 'buy_journal', '购买小刊', '', $score, $num);
- if ($order['system'] == 2) {
- return;
- }
- # 发消息
- $journal['id'] = $order['product_id'];
- $journal['name'] = $order['name'];
- if (strstr('-', $journal['name'])) {
- $name = explode('-', $journal['name']);
- $journal['name'] = $name[0];
- }
-
- //$journal = Dever::db('journal/info')->one($order['product_id']);
- if (Dever::project('message')) {
- Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买成功,您获得了 '.$journal['name'].' 的阅读资格!', 11, $order['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
- }
- $user = Dever::db('passport/user')->one($order['uid']);
- # 发短信
- /*
- if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms') && $order['cate_id'] == 1) {
- $send = array();
- $send['name'] = $journal['name'];
- Dever::load('sms/api.send', 'buy_journal', $user['mobile'], $send);
- }
- */
- # 发模板消息
- $wechat = Dever::db('passport/wechat')->one(array('uid' => $order['uid'], 'type' => 1, 'system_id' => $order['cate_id']));
- if ($wechat && Dever::project('wechat_applet')) {
- $send['key'] = 'buy_journal';
- $send['project_id'] = $order['cate_id'];
- $send['touser'] = $wechat['openid'];
- $send['page'] = Dever::config('base')->applet_index . '?scene=' . $order['uid'] . ',' . '4,' . $order['product_id'];
- $send['data'] = array
- (
- 'keyword1' => array('value' => date('Y年m月d日 H:i', $order['cdate'])),
- 'keyword2' => array('value' => '购买成功,您获得了 '.$journal['name'].' 的阅读资格!'),
- );
- $send['data'] = json_encode($send['data']);
- $send['form_id'] = Dever::load('act/lib/form')->get($order['uid'], 2, $order['cate_id']);
- if ($send['form_id']) {
- Dever::load('wechat_applet/msg.send', $send);
- }
- }
- } else {
- Dever::db('act/order_temp')->update(array('where_id' => $order['id'], 'status' => 3));
- }
- }
- public function send_api()
- {
- $uid = Dever::input('uid', 8);
- # 发模板消息
- $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid));
- if ($wechat && Dever::project('wechat_applet')) {
- $send['key'] = 'buy_journal';
- $send['project_id'] = 2;
- $send['touser'] = $wechat['openid'];
- $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($uid) . ',' . '4,' . $order['product_id'];
- $send['data'] = array
- (
- 'keyword1' => array('value' => date('Y年m月d日 H:i', $order['cdate'])),
- 'keyword2' => array('value' => '购买成功,您获得了 '.$journal['name'].' 的阅读资格!'),
- );
- $send['data'] = json_encode($send['data']);
- $send['form_id'] = Dever::load('act/lib/form')->get($uid, 2, $order['cate_id']);
- if ($send['form_id']) {
- Dever::load('wechat_applet/msg.send', $send);
- }
- }
- }
- }
|