PHP code example of pavelzanek / laravel-gopay-sdk
1. Go to this page and download the library: Download pavelzanek/laravel-gopay-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
Event::listen(\PavelZanek\LaravelGoPaySDK\Events\PaymentCreated::class, function ($event) {
dd($event->payment);
});
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