From d3cd154f275d3455a3659222b10431ed4879ce21 Mon Sep 17 00:00:00 2001 From: weizong song Date: Wed, 15 Oct 2025 01:38:38 +0800 Subject: [PATCH] up --- app/Forms/AlipayAccountForm.php | 1 + .../Controllers/Manager/OrdersController.php | 2 +- app/Libs/AlipayF2F.class.php | 27 ++++++++++------- app/Models/Orders.php | 5 ++++ ...ate_alipay_account_add_app_private_key.php | 30 +++++++++++++++++++ 5 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 database/migrations/2025_10_15_012803_update_alipay_account_add_app_private_key.php diff --git a/app/Forms/AlipayAccountForm.php b/app/Forms/AlipayAccountForm.php index 83e42b8..80898ec 100644 --- a/app/Forms/AlipayAccountForm.php +++ b/app/Forms/AlipayAccountForm.php @@ -12,6 +12,7 @@ class AlipayAccountForm extends Form $this->add("id", Field::HIDDEN); $this->add("name", Field::TEXT, ["label" => "名称", "rules" => "required"]); $this->add("appid", Field::TEXT, ["label" => "应用ID", "rules" => "required"]); + $this->add("private_key", Field::TEXTAREA, ["label" => "应用私钥", "rules" => "required", "attr" => ["rows" => 4]]); $this->add("public_key", Field::TEXTAREA, ["label" => "应用公钥", "rules" => "required", "attr" => ["rows" => 4]]); $this->add("alipay_key", Field::TEXTAREA, ["label" => "支付宝密钥", "rules" => "required", "attr" => ["rows" => 4]]); $this->add('buttons', 'buttongroup', ["splitted" => true, "buttons" => [ diff --git a/app/Http/Controllers/Manager/OrdersController.php b/app/Http/Controllers/Manager/OrdersController.php index d9eb969..9560fa9 100644 --- a/app/Http/Controllers/Manager/OrdersController.php +++ b/app/Http/Controllers/Manager/OrdersController.php @@ -1456,7 +1456,7 @@ class OrdersController extends CommonController case "alipay": $res = (new AlipayF2F())->pay($recharge); $recharge->update([ - "merchant_id" => env("ALI_APP_ID") + "merchant_id" => $order->alipayAccount->appid ]); if ($res["status"]) { return response()->json(true); diff --git a/app/Libs/AlipayF2F.class.php b/app/Libs/AlipayF2F.class.php index 8b803fc..89057a2 100644 --- a/app/Libs/AlipayF2F.class.php +++ b/app/Libs/AlipayF2F.class.php @@ -6,6 +6,7 @@ use Alipay\EasySDK\Kernel\Factory; use Alipay\EasySDK\Kernel\Util\ResponseChecker; use Alipay\EasySDK\Kernel\Config; use App\Events\RechargeSucceed; +use App\Models\AlipayAccount; use App\Models\Recharge; use App\Models\Refund; use Illuminate\Support\Facades\Log; @@ -16,7 +17,9 @@ class AlipayF2F { try { $auth_code = request()->auth_code; - $config = $this->getOptions(); + $project = $recharge->project; + $alipayAccount = $project->alipayAccount; + $config = $this->getOptions($alipayAccount); $result = Factory::setOptions($config)::payment() ->faceToFace() @@ -92,7 +95,11 @@ class AlipayF2F public function refund(Refund $refund) { try { - $config = $this->getOptions(); + $relatedRecharge = $refund->relatedRecharge; + $merchantId = $relatedRecharge->merchant_id; + $alipayAccount = AlipayAccount::where('appid', $merchantId)->withTrashed()->first(); + + $config = $this->getOptions($alipayAccount); $result = Factory::setOptions($config)::payment() ->common() @@ -118,23 +125,23 @@ class AlipayF2F } } - public function getOptions() + public function getOptions(AlipayAccount $alipayAccount) { $options = new Config(); $options->protocol = 'https'; $options->gatewayHost = 'openapi.alipay.com'; $options->signType = 'RSA2'; - // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中 - $options->merchantPrivateKey = env("ALI_APP_PRIVATE_KEY"); - $options->appId = env("ALI_APP_ID"); - //$options->alipayCertPath = env("ALI_CERT_ALIPAY"); - //$options->alipayRootCertPath = env(" ALI_CERT_ROOT"); - //$options->merchantCertPath = env("ALI_CERT_APP"); + #$options->merchantPrivateKey = env("ALI_APP_PRIVATE_KEY"); + #$options->appId = env("ALI_APP_ID"); + #$options->alipayPublicKey = env("ALI_ALIPAY_KEY"); + // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中 + $options->merchantPrivateKey = $alipayAccount->private_key; + $options->appId = $alipayAccount->appid; //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可 - $options->alipayPublicKey = env("ALI_ALIPAY_KEY"); + $options->alipayPublicKey = $alipayAccount->alipay_key; //可设置异步通知接收服务地址(可选) $options->notifyUrl = ""; diff --git a/app/Models/Orders.php b/app/Models/Orders.php index 5559cc6..c9bb790 100755 --- a/app/Models/Orders.php +++ b/app/Models/Orders.php @@ -215,6 +215,11 @@ class Orders extends SoftDeletesModel return $this->hasOneThrough(WechatpayAccount::class, Project::class, "id", "id", "project_id", "wechatpay_account_id"); } + public function alipayAccount() + { + return $this->hasOneThrough(AlipayAccount::class, Project::class, "id", "id", "project_id", "alipay_account_id"); + } + public function bed() { return $this->belongsTo(Bed::class); diff --git a/database/migrations/2025_10_15_012803_update_alipay_account_add_app_private_key.php b/database/migrations/2025_10_15_012803_update_alipay_account_add_app_private_key.php new file mode 100644 index 0000000..135e30e --- /dev/null +++ b/database/migrations/2025_10_15_012803_update_alipay_account_add_app_private_key.php @@ -0,0 +1,30 @@ +string('private_key')->nullable()->comment('应用私钥')->after('appid'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}