123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- # 支付
- namespace Goods\Lib;
- use Dever;
- class Pay
- {
- # 发起支付
- public function action($parent_uid, $uid, $id, $sku, $num, $source, $type = false, $type_id = false)
- {
- if (!$uid) {
- Dever::alert('错误的用户信息');
- }
- $source = 'android';
- $goods = Dever::load('goods/lib/info')->getPayInfo($id, $sku);
- $user = Dever::db('passport/user')->one($uid);
- $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
- if ($source == 'ios') {
- $method = 'apple';
- $account_id = 3;
- # 使用苹果内购支付
- $receipt = Dever::input('receipt');
- if (!$receipt) {
- Dever::alert('苹果内购支付失败,没有receipt参数');
- }
- } elseif ($source == 'android') {
- $method = 'app';
- $account_id = 3;
- } elseif ($source == 'applet') {
- # 小程序支付
- $method = 'applet';
- $account_id = 2;
- } else {
- # 默认是网页支付
- $method = 'page';
- $account_id = 1;
- }
-
- if ($parent_uid) {
- $order_data['parent_uid'] = $parent_uid;
- }
- if ($type) {
- $order_data['type'] = $type;
- }
- if ($type_id) {
- $order_data['type_id'] = $type_id;
- }
-
- $order_data['uid'] = $uid;
- $order_data['status'] = 1;
- $order_data['info_id'] = $goods['id'];
- $order_data['sku_id'] = $sku;
- $order_data['name'] = $goods['name'];
- $order_data['cash'] = $goods['price'];
- $order_data['num'] = $num;
- $order_data['source'] = $source;
- $order_data['order_id'] = $this->getOrderId();
- $id = Dever::db('goods/order')->insert($order_data);
- if (!$id) {
- Dever::alert('支付失败');
- }
- $refer = 'test';
- //$param参数
- $param = array
- (
- 'account_id' => $account_id,
- 'project_id' => 1,
- 'uid' => $uid,
- 'username' => $user['username'],
- 'name' => $order_data['name'],
- 'cash' => $order_data['cash'] * $order_data['num'],
- //'cash' => '0.01',
- 'openid' => isset($wechat['openid']) ? $wechat['openid'] : '',
- 'product_id' => $goods['id'],
- 'order_id' => $order_data['order_id'],
- 'refer' => $refer
- );
- if ($method == 'apple') {
- $param['other'] = $receipt;
- }
- $result['pay'] = Dever::load('pay/api.' . $method, $param);
- $result['order_id'] = $order_data['order_id'];
- return $result;
- }
- # 支付成功回调 安全加密 设置token
- public function success_secure_api_token()
- {
- $project_id = Dever::input('pay_project_id');
- $info = Dever::db('pay/project')->one($project_id);
- if ($info) {
- return $info['key'];
- }
- return 'goods_dever_2020';
- }
- # 支付成功回调 安全加密
- public function success_secure_api($param = array())
- {
- $this->success($param);
- }
- # 支付成功回调
- public function success($param = array())
- {
- $send = $param ? $param : Dever::preInput('pay_');
- $order_id = $send['pay_order_id'];
- $status = $send['pay_status'];
- $msg = $send['pay_msg'];
- $order = Dever::db('goods/order')->one(array('order_id' => $order_id));
- if ($order && $order['status'] == 1) {
- if ($status == 2) {
- # 减少库存 增加销量
- $update['where_id'] = $order['info_id'];
- $update['sell_num'] = $order['num'];
- Dever::db('goods/info')->updateSell($update);
- if ($order['sku_id'] > 0) {
- $update['where_id'] = $order['sku_id'];
- $update['sell_num'] = $order['num'];
- Dever::db('goods/info_sku')->updateSell($update);
- }
- # 增加积分
- if ($order['parent_uid'] > 0) {
- $uid = $order['parent_uid'] . '_' . $order['uid'];
- } else {
- $uid = $order['uid'];
- }
- Dever::score($uid, 'buy_my_goods', '购买自营商品', false, false, false, $order['type'], $order['type_id']);
- # 发消息
- if (Dever::project('message')) {
- Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买成功', 11);
- }
- }
- $update['status'] = $status;
- $update['where_id'] = $order['id'];
- Dever::db('goods/order')->update($update);
- }
- return 'ok';
- }
- # 生成订单号
- public function getOrderId()
- {
- $where['order_id'] = $this->createOrderId();
- $state = Dever::db('goods/order')->one($where);
- if (!$state) {
- return $where['order_id'];
- } else {
- return $this->getOrderId();
- }
- }
- # 生成订单号
- public function createOrderId()
- {
- if (function_exists('session_create_id')) {
- return strtoupper(session_create_id());
- } else {
- $charid = strtoupper(md5(uniqid(mt_rand(), true)));
- return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
- }
- }
- # 临时订单号 无用
- public function createTmpOrderId($prefix = '')
- {
- return $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
- }
- }
|