123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\common\service;
- use addons\epay\library\Service;
- use app\common\model\Payment;
- use com\unionpay\acp\sdk\AcpService;
- use fast\Arr;
- use think\App;
- use think\Model;
- use Yansongda\Pay\Exceptions\BusinessException;
- use Yansongda\Pay\Exceptions\GatewayException;
- class OrderRefundService{
- /** @var Payment */
- protected $payment;
- /** @var Model */
- protected $refund;
- protected $body='退款';
- /**
- * @param Payment $payment
- */
- public function setPayment(Payment $payment): void
- {
- $this->payment = $payment;
- }
- /**
- * @param string $body
- */
- public function setBody(string $body): void
- {
- $this->body = $body;
- }
- /**
- * @param Model $refund
- */
- public function setRefund(Model $refund): void
- {
- $this->refund = $refund;
- }
- /**
- * @return mixed
- */
- public function getAmount($unit='y')
- {
- return moneyFormat($this->refund->getRefundAmount(),$unit);
- }
- public function companyBank(){
- require __DIR__.'/../library/upacp_demo_b2c/demo/api_01_gateway/Form_6_4_Refund.php';
- list($succ,$msg)=uniCompanyRefund($this->refund->order_no,$this->payment->pay_no,$this->getAmount('f'),$this->notifyUrl());
- if(!$succ){
- throw_user($msg);
- }
- $this->refund->refundResult(true);
- }
- public function wechat(){
- try {
- Service::getApp('wechat','')->refund([
- 'out_trade_no' => $this->payment->order_no,
- 'out_refund_no' => $this->refund->order_no,
- 'total_fee' => moneyFormat($this->payment->amount,'f'),
- 'refund_fee' => $this->getAmount('f'),
- 'refund_desc' => $this->body,
- ]);
- $this->refund->refundResult(true);
- }catch (GatewayException|BusinessException $e){
- throw_user($e->getMessage());
- }
- }
- public function alipay(){
- try {
- Service::getApp('alipay','')->refund([
- 'out_trade_no' => $this->payment->order_no,
- 'refund_amount' => $this->getAmount(),
- ]);
- $this->refund->refundResult(true);
- }catch (GatewayException $e){
- throw_user($e->raw['alipay_trade_refund_response']['sub_msg']);
- }
- }
- protected function notifyUrl(){
- return request()->root(true)."/index/payment/refund/order_no/".$this->refund->order_no;
- }
- public function bankUnion(){
- require __DIR__.'/../library/upacp_demo_b2c/sdk/acp_service.php';
- list($bool,$msg)=AcpService::refund($this->refund->order_no,$this->payment->pay_no,$this->getAmount('f'),$this->notifyUrl());
- user_log("OrderRefundService/bankUnion/{$this->refund->order_no}",compact('bool','msg'));
- if(!$bool){
- throw_user($msg);
- }
- $this->refund->refundResult(true);
- }
- public function otherUser(){}
- public function offline(){}
- public function pay(){
- $method=Arr::get(OrderPayService::$methods,$this->payment->pay_type);
- return App::invokeMethod([$this,$method]);
- }
- }
|