dggzichahu/application/api/controller/Payment.php

208 lines
5.7 KiB
PHP
Executable File

<?php
// +----------------------------------------------------------------------
// | 宏驰云科技开发团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | Author: HcyShop-kiki
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\api\model\Order;
use app\common\model\Order as CommonOrder;
use app\common\model\Client_;
use app\common\server\AliPayServer;
use app\common\server\WeChatPayServer;
use app\common\server\WeChatServer;
use app\common\logic\PaymentLogic;
use app\common\model\Pay;
use think\Db;
/**
* 支付逻辑
* Class Payment
* @package app\api\controller
*/
class Payment extends ApiBase
{
public $like_not_need_login = ['aliNotify', 'notifyMnp', 'notifyOa', 'notifyApp'];
/**
* Notes: 预支付
* @author kiki
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function prepay()
{
$post = $this->request->post();
switch ($post['from']) {
case 'order':
$order = Order::get($post['order_id']);
if ($order['order_status'] == CommonOrder::STATUS_CLOSE || $order['del'] == 1) {
$this->_error('订单已关闭');
}
break;
case 'recharge':
$order = Db::name('recharge_order')->where(['id' => $post['order_id']])->find();
break;
}
//找不到订单
if (empty($order)) {
$this->_error('订单不存在');
}
//已支付
if ($order['pay_status'] == Pay::ISPAID || $order['order_amount'] == 0) {
$this->_success('支付成功', ['order_id' => $order['id']], 10001);
}
$result = PaymentLogic::pay($post['from'], $order, $post['order_source']);
if (false === $result) {
$this->_error(PaymentLogic::getError(), ['order_id' => $order['id']], PaymentLogic::getReturnCode());
}
if (PaymentLogic::getReturnCode() != 0) {
$this->_success('', $result, PaymentLogic::getReturnCode());
}
$this->_success('', $result);
}
/**
* Notes: pc端预支付 NATIVE
* @author kiki
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function pcPrepay()
{
$post = $this->request->post();
$pay_way = $post['pay_way'];
$order = Order::get($post['order_id']);
$return_msg = ['order_id' => $order['id'], 'order_amount' => $order['order_amount']];
//找不到订单
if (empty($order)) {
$this->_error('订单不存在');
}
if ($order['order_status'] == CommonOrder::STATUS_CLOSE || $order['del'] == 1) {
$this->_error('订单已关闭');
}
//已支付
if ($order['pay_status'] == Pay::ISPAID || $order['order_amount'] == 0) {
$this->_success('支付成功', $return_msg, 10001);
}
$result = PaymentLogic::pcPay($order, $pay_way, $post['order_source']);
if (false === $result) {
$this->_error(PaymentLogic::getError(), $return_msg, PaymentLogic::getReturnCode());
}
if ($pay_way == Pay::BALANCE_PAY) {
$this->_success('余额支付成功', $return_msg, PaymentLogic::getReturnCode());
}
$return_msg['data'] = $result;
if (PaymentLogic::getReturnCode() != 0) {
$this->_success('', $return_msg, PaymentLogic::getReturnCode());
}
$this->_success('', $return_msg);
}
/**
* Notes: 小程序回调
* @author kiki
*/
public function notifyMnp()
{
$config = WeChatServer::getPayConfig(Client_::mnp);
return WeChatPayServer::notify($config);
}
/**
* Notes: 公众号回调
* @author kiki
*/
public function notifyOa()
{
$config = WeChatServer::getPayConfig(Client_::oa);
return WeChatPayServer::notify($config);
}
/**
* Notes: APP回调
* @author kiki
*/
public function notifyApp()
{
$config = WeChatServer::getPayConfig(Client_::ios);
return WeChatPayServer::notify($config);
}
/**
* Notes: 支付宝回调
* @author kiki
*/
public function aliNotify()
{
$data = $this->request->post();
return (new AliPayServer())->verifyNotify($data);
}
/**
* Notes:
* @author kiki
* @return \think\Model[]
*/
public function payway()
{
$payModel = new Pay();
$pay = $payModel->where(['status' => 1])->order('sort')->hidden(['config'])->select()->toArray();
foreach ($pay as $k => &$item) {
if ($item['code'] == 'wechat') {
$item['extra'] = '微信快捷支付';
$item['pay_way'] = Pay::WECHAT_PAY;
}
if ($item['code'] == 'balance') {
$user_money = Db::name('user')->where(['id' => $this->user_id])->value('user_money');
$item['extra'] = '可用余额:'.$user_money;
$item['pay_way'] = Pay::BALANCE_PAY;
}
if ($item['code'] == 'alipay') {
$item['extra'] = '';
$item['pay_way'] = Pay::ALI_PAY;
}
if (in_array($this->client, [Client_::mnp, Client_::oa]) && $item['code'] == 'alipay') {
unset($pay[$k]);
}
}
$this->_success('', array_values($pay));
}
}