You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
311 lines
11 KiB
311 lines
11 KiB
<?php
|
|
|
|
namespace app\appapi\controller\v1;
|
|
|
|
use app\appapi\ApiController;
|
|
use think\facade\Db;
|
|
use think\facade\Log;
|
|
use app\common\payment\Payment as PaymentStrategy;
|
|
|
|
|
|
class Payment extends ApiController
|
|
{
|
|
public function getPayments()
|
|
{
|
|
|
|
$res = Db::name('payment')
|
|
->where('enabled', 1)
|
|
->select();
|
|
|
|
|
|
if (!$res) {
|
|
return $this->Error('錯誤請求');
|
|
}
|
|
|
|
return $this->Success($res);
|
|
}
|
|
|
|
public function process()
|
|
{
|
|
//從order info 取出訂單資料
|
|
$rtn = Db::name('order_info')
|
|
->where('order_sn', input('order_sn'))
|
|
->find();
|
|
|
|
if (!$rtn) {
|
|
return $this->Error('錯誤請求');
|
|
}
|
|
$rtn['address'] = json_decode($rtn['address'], true);
|
|
$order = [
|
|
'order_sn' => input('order_sn'),
|
|
'order_amount' => $rtn['order_amount'],
|
|
'goods_amount' => $rtn['goods_amount'],
|
|
'mobile' => $rtn['mobile'],
|
|
'consignee' => $rtn['consignee'],
|
|
'email' => $rtn['email'],
|
|
'zipcode' => isset($rtn['address']['zipcode']) ? $rtn['address']['zipcode'] : '',
|
|
'goods_list' => Db::name('order_goods')->where('order_id', $rtn['order_id'])->select()->toArray(),
|
|
];
|
|
|
|
$payment = new PaymentStrategy(input('pay_code'));
|
|
|
|
$result = $payment->pay($order);
|
|
|
|
return $this->Success($result);
|
|
}
|
|
|
|
//金流回傳網址
|
|
public function response()
|
|
{
|
|
$data = input();
|
|
|
|
$payment = new PaymentStrategy(input('paycode'));
|
|
/* 傳入回傳資料,返回result
|
|
** $result['code'] = 200
|
|
** $result['msg'] = '付款成功'
|
|
** $result['order_sn'] = '訂單編號'
|
|
** $result['paycode'] = '金流代碼'
|
|
*/
|
|
|
|
$result = $payment->response($data);
|
|
|
|
switch ($result['paycode']) {
|
|
case 'eccredit':
|
|
if ($result['code'] == 200) {
|
|
//判斷是否為超商取貨付款
|
|
$order = Db::name('order_info')
|
|
->where('order_sn', $result['order_sn'])
|
|
->find();
|
|
|
|
$shipping_code = Db::name('shipping')->where('shipping_id', $order['shipping_id'])->value('shipping_code');
|
|
|
|
if ($shipping_code == 'ecpay') {
|
|
$rtn = \app\common\shipping\Shipping::createShipping('ecpay', $order['order_sn']);
|
|
if ($rtn['code'] != 200) {
|
|
$code = 200;
|
|
$result['msg'] .= ',建立物流單失敗';
|
|
//TODO: 通知管理員物流單建立失敗
|
|
}
|
|
}
|
|
|
|
//判斷是否為SlashCard商品
|
|
$is_main = Db::name('order_goods')
|
|
->where('order_id', $order['order_id'])
|
|
->where('goods_id', 1)
|
|
->find();
|
|
|
|
if ($is_main) {
|
|
$rtn = \app\service\Card::addUser([
|
|
'order_sn' => $order['order_sn'],
|
|
'user_id' => Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'),
|
|
]);
|
|
if ($rtn['code'] != 200) {
|
|
$code = 200;
|
|
$result['msg'] .= ',建立卡片失敗';
|
|
//TODO: 通知管理員建立卡片失敗
|
|
} else {
|
|
//更改會員的狀態
|
|
Db::name('users')->where('user_id', $order['user_id'])->update(['is_validated' => 1]);
|
|
$code = 200;
|
|
}
|
|
}
|
|
|
|
//分潤獎金計算
|
|
//取得介紹人user_id
|
|
//分銷金額
|
|
try {
|
|
//找出第一二三四層介紹人
|
|
$sso_user_id = Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id');
|
|
$parents = $this->getAffiliate($sso_user_id, 1);
|
|
|
|
foreach ($parents as $key => $value) {
|
|
//TODO: 金額改為後台設定
|
|
$money = [400, 50, 30, 20];
|
|
|
|
//寫入affiliate_log
|
|
$affiliate_log = [
|
|
'user_id' => Db::name('users')->where('sso_user_id', $value)->value('user_id'),
|
|
'order_id' => $order['order_id'],
|
|
'time' => time(),
|
|
'user_name' => Db::name('users')->where('sso_user_id', $value)->value('real_name'),
|
|
'money' => $money[$key],
|
|
];
|
|
Db::name('affiliate_log')->insert($affiliate_log);
|
|
//增加介紹人的獎金
|
|
Db::name('users')->where('sso_user_id', $value)->inc('frozen_money', $money[$key])->update();
|
|
}
|
|
} catch (\Exception $e) {
|
|
print_r($e->getMessage());
|
|
$code = 200;
|
|
$result['msg'] .= ',分潤失敗';
|
|
}
|
|
}else{
|
|
//付款失敗
|
|
$code = $result['code'];
|
|
$result['msg'] .= ',付款失敗';
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
//回傳成功並且不為超商支付及atm
|
|
|
|
$query_string = http_build_query($result);
|
|
return redirect(getUrl() . '/m/cartFinish/?' . $query_string);
|
|
}
|
|
|
|
//金流回傳網址
|
|
public function callback()
|
|
{
|
|
Log::write(json_encode(input()));
|
|
$data = input('post.');
|
|
|
|
$payment = new PaymentStrategy(input('paycode'));
|
|
|
|
/* 傳入回傳資料,返回result
|
|
** $result['code'] = 200
|
|
** $result['msg'] = '付款成功'
|
|
** $result['order_sn'] = '訂單編號'
|
|
** $result['paycode'] = '金流代碼'
|
|
*/
|
|
|
|
$result = $payment->callback($data);
|
|
|
|
if ($result['code'] == 200) {
|
|
//判斷是否為超商取貨付款
|
|
$order = Db::name('order_info')
|
|
->where('order_sn', $result['order_sn'])
|
|
->find();
|
|
|
|
$shipping_code = Db::name('shipping')->where('shipping_id', $order['shipping_id'])->value('shipping_code');
|
|
|
|
if ($shipping_code == 'ecpay') {
|
|
$rtn = \app\common\shipping\Shipping::createShipping('ecpay', $order['order_sn']);
|
|
if ($rtn['code'] != 200) {
|
|
$code = 200;
|
|
$result['msg'] .= ',建立物流單失敗';
|
|
//TODO: 通知管理員物流單建立失敗
|
|
}
|
|
}
|
|
|
|
//判斷是否為SlashCard商品
|
|
$is_main = Db::name('order_goods')
|
|
->where('order_id', $order['order_id'])
|
|
->where('goods_id', 1)
|
|
->find();
|
|
|
|
if ($is_main) {
|
|
$rtn = \app\service\Card::addUser([
|
|
'order_sn' => $order['order_sn'],
|
|
'user_id' => Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'),
|
|
]);
|
|
if ($rtn['code'] != 200) {
|
|
$code = 200;
|
|
$result['msg'] .= ',建立卡片失敗';
|
|
//TODO: 通知管理員建立卡片失敗
|
|
} else {
|
|
//更改會員的狀態
|
|
Db::name('users')->where('user_id', $order['user_id'])->update(['is_validated' => 1]);
|
|
$code = 200;
|
|
}
|
|
}
|
|
|
|
//分潤獎金計算
|
|
//取得介紹人user_id
|
|
//分銷金額
|
|
try {
|
|
//找出第一二三四層介紹人
|
|
$sso_user_id = Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id');
|
|
$parents = $this->getAffiliate($sso_user_id, 1);
|
|
|
|
foreach ($parents as $key => $value) {
|
|
//TODO: 金額改為後台設定
|
|
$money = [400, 50, 30, 20];
|
|
|
|
//寫入affiliate_log
|
|
$affiliate_log = [
|
|
'user_id' => Db::name('users')->where('sso_user_id', $value)->value('user_id'),
|
|
'order_id' => $order['order_id'],
|
|
'time' => time(),
|
|
'user_name' => Db::name('users')->where('sso_user_id', $value)->value('real_name'),
|
|
'money' => $money[$key],
|
|
];
|
|
Db::name('affiliate_log')->insert($affiliate_log);
|
|
//增加介紹人的獎金
|
|
Db::name('users')->where('sso_user_id', $value)->inc('frozen_money', $money[$key])->update();
|
|
}
|
|
} catch (\Exception $e) {
|
|
$code = 200;
|
|
$result['msg'] .= ',分潤失敗';
|
|
}
|
|
}
|
|
return '1|OK';
|
|
}
|
|
|
|
public function cancel()
|
|
{
|
|
Log::write('金流取消');
|
|
echo "金流取消";
|
|
}
|
|
|
|
public function otpError(){
|
|
$data = input();
|
|
$order_sn = $data['order_sn'];
|
|
|
|
$order = Db::name('order_info')
|
|
->where('order_sn', $order_sn)
|
|
->find();
|
|
|
|
$result['pay_status'] = '3';
|
|
$code = 500;
|
|
$message = 'OTP驗證失敗';
|
|
|
|
try {
|
|
//更新訂單狀態
|
|
Db::name('order_info')
|
|
->where('order_sn', $order_sn)
|
|
->update($result);
|
|
|
|
//更新訂單操縱紀錄
|
|
$order_action = [
|
|
'order_id' => $order['order_id'],
|
|
'action_user' => '綠界科技',
|
|
'order_status' => $order['order_status'],
|
|
'shipping_status' => $order['shipping_status'],
|
|
'pay_status' => $order['pay_status'],
|
|
'action_note' => '綠界金流: 信用卡支付,' . $message,
|
|
'log_time' => time(),
|
|
];
|
|
Db::name('order_action')->insert($order_action);
|
|
} catch (\Exception $e) {
|
|
throw new \Exception($e->getMessage());
|
|
}
|
|
|
|
|
|
$result = [
|
|
'code' => $code,
|
|
'paycode' => 'eccredit',
|
|
'msg' => $message,
|
|
'order_sn' => $order_sn,
|
|
];
|
|
|
|
$query_string = http_build_query($result);
|
|
return redirect(getUrl() . '/m/cartFinish/?' . $query_string);
|
|
}
|
|
|
|
private function getAffiliate($sso_user_id, $level = 1)
|
|
{
|
|
$parents = [];
|
|
$parent_id = Db::name('users')->where('sso_user_id', $sso_user_id)->value('parent_id');
|
|
|
|
if ($parent_id) {
|
|
$parents[$level] = $parent_id;
|
|
$level++;
|
|
$return = $this->getAffiliate($parent_id, $level);
|
|
$parents = array_merge($parents, $return);
|
|
}
|
|
|
|
return $parents;
|
|
}
|
|
}
|