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.
payment/app/Service/PaymentService.php

69 lines
2.4 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Service;
1 year ago
use App\Helper\Efps\Result;
2 years ago
use App\Helper\Redis;
use App\Helper\RedisKey;
use App\Model\App;
use App\Model\Order;
1 year ago
use App\Model\RefundOrder;
2 years ago
class PaymentService extends AbstractService
{
public function createOrder(App $app, array $params) {
$order = new Order();
$order->app_id = $app->app_id;
1 year ago
$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);
2 years ago
return $order;
}
1 year ago
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();
}
}
1 year ago
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();
}
}
2 years ago
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;
}
}