You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.4 KiB
PHP
69 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Helper\Efps\Result;
|
|
use App\Helper\Redis;
|
|
use App\Helper\RedisKey;
|
|
use App\Model\App;
|
|
use App\Model\Order;
|
|
use App\Model\RefundOrder;
|
|
|
|
class PaymentService extends AbstractService
|
|
{
|
|
public function createOrder(App $app, array $params) {
|
|
$order = new Order();
|
|
$order->app_id = $app->app_id;
|
|
$order->out_order_no = $params['outTradeNo'] ?: '';
|
|
$order->amount = $params['payAmount'] ?: 0;
|
|
$order->notify_url = $params['notifyUrl'] ?: '';
|
|
$order->order_info = json_encode($params['orderInfo'] ?: [], JSON_UNESCAPED_UNICODE);
|
|
return $order;
|
|
}
|
|
|
|
public function handlePayResult(Result $result, Order $order) {
|
|
if ($result->get('payState') === '00') {
|
|
$order->status = Order::STATUS_PAYED;
|
|
$order->fee = $result->get('procedureFee', 0);
|
|
$order->pay_order_no = $result->get('transactionNo', '');
|
|
$order->channel_order_no = $result->get('channelOrder', '');
|
|
$order->payed_at = date('Y-m-d H:i:s');
|
|
$order->save();
|
|
} elseif ($result->get('payState') === '01') {
|
|
$order->status = Order::STATUS_FAILED;
|
|
$order->error_code = '01';
|
|
$order->error_msg = '支付失败';
|
|
$order->payed_at = date('Y-m-d H:i:s');
|
|
$order->save();
|
|
}
|
|
}
|
|
|
|
public function handleRefundResult(Result $result, RefundOrder $order) {
|
|
if ($result->get('refundState') === '00') {
|
|
$order->status = RefundOrder::STATUS_REFUND_SUCCESS;
|
|
$order->fee = $result->get('procedureFee', 0);
|
|
$order->refunded_at = date('Y-m-d H:i:s');
|
|
$order->save();
|
|
} elseif ($result->get('refundState') === '01') {
|
|
$order->status = RefundOrder::STATUS_REFUND_FAILED;
|
|
$order->error_code = '01';
|
|
$order->error_msg = '处理失败';
|
|
$order->refunded_at = date('Y-m-d H:i:s');
|
|
$order->save();
|
|
}
|
|
}
|
|
|
|
private function generateOrderNo() {
|
|
$now = time();
|
|
$key = RedisKey::getGenerateOrderNoKey($now);
|
|
$incrId = Redis::incr($key);
|
|
$incrId = '' . $incrId;
|
|
Redis::expire($key, 5*60);
|
|
$padLength = 6 - strlen($incrId);
|
|
$incrId = str_pad($incrId, $padLength, '0', STR_PAD_LEFT);
|
|
return date('YmdHis', $now) . $incrId;
|
|
}
|
|
}
|