赵华亮 / laravel-gopay-sdk
此包已被 废弃 并不再维护。没有建议的替代包。
GoPay SDK for Laravel
v1.0.1
2022-11-12 16:12 UTC
Requires
- php: ^8.0|^8.1
- gopay/payments-sdk-php: ^1.4
- illuminate/cache: ^8.0|^9.0
- illuminate/config: ^8.0|^9.0
- illuminate/container: ^8.0|^9.0
- illuminate/support: ^8.0|^9.0
This package is auto-updated.
Last update: 2024-08-18 11:53:21 UTC
README
要求
GoPay SDK for Laravel 包需要 PHP 8.0+、Laravel 8+。
安装
步骤 1: 安装包
通过执行以下命令在 composer.json 中添加包
composer require pavelzanek/laravel-gopay-sdk
此命令将包安装到 vendor/ 目录中。
步骤 2: 配置
您可以通过运行命令来初始化配置文件
php artisan vendor:publish --provider="PavelZanek\LaravelGoPaySDK\Providers\GoPayServiceProvider" --tag="config"
接下来,您可以在 config
文件夹中看到新创建的文件 - gopay.php
。
默认情况下,配置文件看起来像这样
<?php return [ 'goid' => env('GOPAY_ID'), 'clientId' => env('GOPAY_CLIENT_ID'), 'clientSecret' => env('GOPAY_CLIENT_SECRET'), 'defaultScope' => env('GOPAY_DEFAULT_SCOPE', 'ALL'), 'gatewayUrl' => env('GOPAY_PRODUCTION_ENV', true) ? 'https://gate.gopay.cz/' : 'https://gw.sandbox.gopay.com/', 'languages' => [ 'en' => 'ENGLISH', 'sk' => 'SLOVAK', 'cs' => 'CZECH' ], 'timeout' => 30 ];
基本变量可以在 .env 文件中设置。
特性
语言
当您创建新的支付时,您可以设置支付网关界面语言。
通过 GoPay 定义
\GoPaySDK::lang(GoPay\Definition\Languages::CZECH)
通过语言代码
\GoPaySDK::lang('cs')
通过字符串
\GoPaySDK::lang('CZECH')
作用域
通过 GoPay 定义
\GoPaySDK::scope(GoPay\Definition\TokenScope::CREATE_PAYMENT)
通过字符串
\GoPay::scope('CREATE_PAYMENT')
事件
Event::listen(\PavelZanek\LaravelGoPaySDK\Events\PaymentCreated::class, function ($event) { dd($event->payment); });
用法/示例
门面 GoPaySDK
例如,您可以在控制器中通过门面 GoPaySDK
创建标准支付
<?php namespace App\Http\Controllers\Support\Orders; use App\Http\Controllers\Controller; use App\Http\Requests\Orders\StoreOrderRequest; use GoPaySDK; use GoPay\Definition\Payment\Currency; use GoPay\Definition\Payment\PaymentInstrument; class OrdersController extends Controller { public function storeOrder(StoreOrderRequest $request, Order $order): RedirectResponse { // your code GoPaySDk::log(function($request, $response){ \Log::info("{$request->method} {$request->url} -> {$response->statusCode}"); }); // You can use https://doc.gopay.com/#payment-creation $response = GoPaySDK::lang(strtoupper($order->locale))->scope('CREATE_PAYMENT')->createPayment([ 'payer' => [ 'default_payment_instrument' => PaymentInstrument::PAYMENT_CARD, 'allowed_payment_instruments' => [PaymentInstrument::PAYMENT_CARD], 'contact' => [ 'first_name' => $order->customer_name, 'last_name' => $order->customer_surname, 'email' => $order->customer_email, 'phone_number' => $order->customer_telephone, 'city' => $order->customer_city, 'street' => $order->customer_street, 'postal_code' => $order->customer_postal_code, 'country_code' => $order->customer_state_code, ], ], 'amount' => $order->total_price, // Total price (float, two decimal places, without separator - fe. 19900 will be 199,00) 'currency' => Currency::CZECH_CROWNS, 'order_number' => $order->id, 'order_description' => 'Test', 'items' => [ // Only example, you have to do yourself [ 'name' => 'test', 'amount' => 19900 ], ], 'additional_params' => [ array( 'name' => 'invoicenumber', 'value' => $order->invoice_number ) ], 'callback' => [ 'return_url' => route('orders.show', $order), 'notification_url' => route('gopayNotification') ] ]); //dd($response); if ($response->hasSucceed()) { // Logic when the response is successful // For example you can redirect users after some logic to process payment return redirect()->to($response->json['gw_url'], 301); } // rest of your code } }
检查支付状态
这里有一个简单的示例,说明您如何获取支付状态以及如何将状态分配给订单。
use GoPaySDK; use App\Enums\Orders\OrderStatus; // your code $response = GoPaySDK::getStatus($order->gopay_payment_id); if(isset($response->json['state'])){ $status = match($response->json['state']) { 'CREATED' => OrderStatus::Waiting, 'PAID' => OrderStatus::Paid, 'CANCELED' => OrderStatus::Canceled, 'TIMEOUTED' => OrderStatus::Timeouted, 'REFUNDED' => OrderStatus::Refunded, default => $response->json['state'] }; $order->update([ 'status' => $status, ]); } // rest of your code
许可证
版权所有 (c) Pavel Zaněk。MIT 许可证,有关详细信息请参阅 LICENSE。