diff --git a/app/Libs/AlipayF2F.class.php b/app/Libs/AlipayF2F.class.php index 69bb1a7..fb32a74 100644 --- a/app/Libs/AlipayF2F.class.php +++ b/app/Libs/AlipayF2F.class.php @@ -7,6 +7,7 @@ use Alipay\EasySDK\Kernel\Util\ResponseChecker; use Alipay\EasySDK\Kernel\Config; use App\Events\RechargeSucceed; use App\Models\Recharge; +use App\Models\Refund; use Illuminate\Support\Facades\Log; class AlipayF2F @@ -51,6 +52,33 @@ class AlipayF2F } } + public function refund(Refund $refund) { + try { + $config = $this->getOptions(); + $result = Factory::setOptions($config)::payment() + ->common() + ->refund($refund->serial,$refund->money); + $responseChecker = new ResponseChecker(); + //处理响应或异常 + if ($responseChecker->success($result)) { + $transaction_id = $result->tradeNo; + $update = [ + "paid_at" => date("Y-m-d H:i:s"), + "payment_serial" => $transaction_id + ]; + $refund->update($update); + } else { + Log::info("支付宝退款失败:".$result->code.";".$result->msg); + $refund->increment("try_times"); + return false; + } + } catch (\Exception $exception) { + Log::info("支付宝退款失败:".$exception->getCode().";".$exception->getMessage()); + $refund->increment("try_times"); + return false; + } + } + public function getOptions() { $options = new Config(); diff --git a/app/Models/Refund.php b/app/Models/Refund.php index 5cdc85d..4a3a892 100755 --- a/app/Models/Refund.php +++ b/app/Models/Refund.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Customer; +use App\Libs\AlipayF2F; use App\Manager; use Illuminate\Support\Facades\Log; @@ -43,8 +44,9 @@ class Refund extends SoftDeletesModel return $this->belongsTo(Orders::class); } - public function patient() { - return $this->hasOneThrough(Patient::class, Orders::class,"id","id","order_id","patient_id"); + public function patient() + { + return $this->hasOneThrough(Patient::class, Orders::class, "id", "id", "order_id", "patient_id"); } public function relatedRecharge() @@ -76,12 +78,17 @@ class Refund extends SoftDeletesModel $refund->weixinRefund(); break; case "alipay": - //todo: + $refund->alipayRefund(); break; } } } + public function alipayRefund() + { + return (new AlipayF2F())->refund($this); + } + /** * * 申请微信退款,WxPayRefund中out_trade_no、transaction_id至少填一个且