feat : Bridge pay
This commit is contained in:
23
app/Controller/Test/BridgeController.php
Normal file
23
app/Controller/Test/BridgeController.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Test;
|
||||
|
||||
use App\Service\Test\Bridge\PayService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
|
||||
#[Controller(prefix: 'bridge/test')]
|
||||
class BridgeController
|
||||
{
|
||||
/**
|
||||
* 桥接消费
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: 'pay', methods: 'GET')]
|
||||
public function pay(): array
|
||||
{
|
||||
return (new PayService)->handle();
|
||||
}
|
||||
}
|
||||
18
app/Interface/Test/Bridge/PaymentInterface.php
Normal file
18
app/Interface/Test/Bridge/PaymentInterface.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Interface\Test\Bridge;
|
||||
|
||||
interface PaymentInterface
|
||||
{
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function pay(float $amount): array;
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function refund(float $amount): array;
|
||||
}
|
||||
45
app/Service/Test/Bridge/Pay/Lib/AlipayPayment.php
Normal file
45
app/Service/Test/Bridge/Pay/Lib/AlipayPayment.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Bridge\Pay\Lib;
|
||||
|
||||
use App\Interface\Test\Bridge\PaymentInterface;
|
||||
|
||||
class AlipayPayment implements PaymentInterface
|
||||
{
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function pay(float $amount): array
|
||||
{
|
||||
return [
|
||||
'type' => 'pay',
|
||||
'price' => $amount,
|
||||
'pay_type' => 'alipay',
|
||||
'msg' => 'Processing credit card payment of ' . $amount . PHP_EOL,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function refund(float $amount): array
|
||||
{
|
||||
return [
|
||||
'type' => 'refund',
|
||||
'price' => $amount,
|
||||
'pay_type' => 'alipay',
|
||||
'msg' => 'Processing credit card refund of ' . $amount . PHP_EOL,
|
||||
];
|
||||
}
|
||||
}
|
||||
45
app/Service/Test/Bridge/Pay/Lib/CreditCardPayment.php
Normal file
45
app/Service/Test/Bridge/Pay/Lib/CreditCardPayment.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Bridge\Pay\Lib;
|
||||
|
||||
use App\Interface\Test\Bridge\PaymentInterface;
|
||||
|
||||
class CreditCardPayment implements PaymentInterface
|
||||
{
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function pay(float $amount): array
|
||||
{
|
||||
return [
|
||||
'type' => 'pay',
|
||||
'price' => $amount,
|
||||
'pay_type' => 'credit_card',
|
||||
'msg' => 'Processing credit card payment of ' . $amount . PHP_EOL,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function refund(float $amount): array
|
||||
{
|
||||
return [
|
||||
'type' => 'refund',
|
||||
'price' => $amount,
|
||||
'pay_type' => 'credit_card',
|
||||
'msg' => 'Processing credit card refund of ' . $amount . PHP_EOL,
|
||||
];
|
||||
}
|
||||
}
|
||||
53
app/Service/Test/Bridge/Pay/PaymentGateway.php
Normal file
53
app/Service/Test/Bridge/Pay/PaymentGateway.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Bridge\Pay;
|
||||
|
||||
use App\Interface\Test\Bridge\PaymentInterface;
|
||||
use App\Interface\Test\Decorator\LoggerInterface;
|
||||
|
||||
abstract class PaymentGateway
|
||||
{
|
||||
/**
|
||||
* @var PaymentInterface
|
||||
*/
|
||||
protected PaymentInterface $payment;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param PaymentInterface $payment
|
||||
* @param LoggerInterface $logger
|
||||
*/
|
||||
public function __construct(PaymentInterface $payment, LoggerInterface $logger)
|
||||
{
|
||||
$this->payment = $payment;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽象支付类
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
abstract public function processPayment(float $amount): array;
|
||||
|
||||
/**
|
||||
* 抽象退款类
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
abstract public function processRefund(float $amount): array;
|
||||
}
|
||||
37
app/Service/Test/Bridge/Pay/PremiumPaymentService.php
Normal file
37
app/Service/Test/Bridge/Pay/PremiumPaymentService.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Bridge\Pay;
|
||||
|
||||
class PremiumPaymentService extends PaymentGateway
|
||||
{
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function processPayment(float $amount): array
|
||||
{
|
||||
$fee = $amount * 0.02; // 2%手续费
|
||||
$this->logger->debug('Premium payment processing with fee: ' . $fee);
|
||||
return $this->payment->pay($amount + $fee);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function processRefund(float $amount): array
|
||||
{
|
||||
$fee = $amount * 0.01; // 1%手续费
|
||||
$this->logger->debug('Premium payment processing with refund: ' . $fee);
|
||||
return $this->payment->refund($amount - $fee);
|
||||
}
|
||||
}
|
||||
35
app/Service/Test/Bridge/Pay/StandardPaymentService.php
Normal file
35
app/Service/Test/Bridge/Pay/StandardPaymentService.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Bridge\Pay;
|
||||
|
||||
class StandardPaymentService extends PaymentGateway
|
||||
{
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function processPayment(float $amount): array
|
||||
{
|
||||
$this->logger->debug('Standard payment processing');
|
||||
return $this->payment->pay($amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
* @return array
|
||||
*/
|
||||
public function processRefund(float $amount): array
|
||||
{
|
||||
$this->logger->debug('Standard refund processing');
|
||||
return $this->payment->refund($amount);
|
||||
}
|
||||
}
|
||||
55
app/Service/Test/Bridge/PayService.php
Normal file
55
app/Service/Test/Bridge/PayService.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Bridge;
|
||||
|
||||
use App\Service\Test\Bridge\Pay\PaymentGateway;
|
||||
use App\Service\Test\TestBaseService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class PayService extends TestBaseService
|
||||
{
|
||||
/**
|
||||
* @var PaymentGateway
|
||||
*/
|
||||
#[Inject('standard_payment')]
|
||||
protected PaymentGateway $standardPaymentGateway;
|
||||
|
||||
/**
|
||||
* @var PaymentGateway
|
||||
*/
|
||||
#[Inject('premium_payment')]
|
||||
protected PaymentGateway $premiumPaymentGateway;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$amount = $this->request->input('amount',100);
|
||||
$type = $this->request->input('type','standard');
|
||||
|
||||
$payment = $type === 'premium' ? $this->premiumPaymentGateway : $this->standardPaymentGateway;
|
||||
|
||||
$payRes = $payment->processPayment($amount);
|
||||
$refundRes = $payment->processRefund($amount);
|
||||
|
||||
$refundPreRes = $this->premiumPaymentGateway->processRefund($amount);
|
||||
$payPreRes = $this->premiumPaymentGateway->processPayment($amount);
|
||||
|
||||
return $this->return->success('success',[
|
||||
'pay_res' => $payRes,
|
||||
'refund_res' => $refundRes,
|
||||
'refund_pre_res' => $refundPreRes,
|
||||
'pay_pre_res' => $payPreRes,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user