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.

238 lines
8.0 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_slash' => 1]);
$code = 200;
}
}
} 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;
}
}
}
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);
}
}