PHP code example of soap / laravel-omise

1. Go to this page and download the library: Download soap/laravel-omise 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/ */

    

soap / laravel-omise example snippets


return [
    'url' => 'https://api.omise.co',

    'live_public_key' => env('OMISE_LIVE_PUBLIC_KEY', 'pkey_test_xxx'),
    'live_secret_key' => env('OMISE_LIVE_SECRET_KEY', 'skey_test_xxx'),

    'test_public_key' => env('OMISE_TEST_PUBLIC_KEY', ''),
    'test_secret_key' => env('OMISE_TEST_SECRET_KEY', ''),

    'api_version' => env('OMISE_API_VERSION', '2019-05-29'),

    'sanbox_status' => env('OMISE_SANDBOX_STATUS', true),
];

php artisan omise:verify


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Soap\LaravelOmise\Omise;
use Soap\LaravelOmise\Omise\Charge;
use Soap\LaravelOmise\Omise\Error;

class PaymentController extends Controller
{
    public function __construct(protected Omise $omise) {}

    public function create()
    {
        $publicKey = $this->omise->getPublicKey();

        return view('payments.form', compact('publicKey'));
    }
}

php artisan omise:account

php artisan omise:balance

app('omise')->validConfig();

$account = app('omise')->account()->retrieve();

$account->updateWebhookUri('https::mydomain.com/api/omise/webhooks');

$charge = app('omise')->charge()->find($id);
$charge->isPaid(); // is it paid?
$charge->isAwaitCapture(); // is it waiting for capture
$charge->isFailed(); // status == failed
$charge->getAmount(); // get unit amount in the based currency e.g. 100 THB
$charge->getRawAmount(); // get minor amount in based currency e.g. 10000 Satang (THB)
$charge->getMetadata('booking_id'); // get metadata you provide when create a charge


namespace App\Services;

use App\Contracts\PaymentProcessorInterface;
use Soap\LaravelOmise\Omise\Error;

class CreditCardPaymentProcessor implements PaymentProcessorInterface
{
    /**
     * Use token to authorize payment
     */
    public function createPayment(float $amounnt, string $currency = 'THB', array $paymentDetails = []): array
    {
        $charge = app('omise')->charge()->create([
            'amount' => $amounnt * 100,
            'currency' => $currency,
            'card' => $paymentDetails['token'],
            'capture' => $paymentDetails['capture'] ?? true,
            'webhook_endpoints' => $paymentDetails['webhook_endpoints'] ?? null,
            'metadata' => $paymentDetails['metadata'] ?? [],
        ]);

        if ($charge instanceof Error) {
            return [
                'code' => $charge->getCode(),
                'error' => $charge->getMessage(),
            ];
        }

        return [
            'charge_id' => $charge->id,
            'amount' => $charge->amount / 100,
            'currency' => $charge->currency,
            'status' => $charge->status,
            'paid' => $charge->paid,
            'paid_at' => $charge->paid_at,
            'charge' => $charge,
        ];
    }

    /**
     * Process payment using charge id
     */
    public function processPayment(array $paymentData): array
    {
        return [];
    }

    /**
     * Refund payment using charge id
     */
    public function refundPayment(string $chargeId, float $amount): bool
    {
        return true;
    }

    public function hasRefundSupport(): bool
    {
        return true;
    }

    public function isOffline(): bool
    {
        return false;
    }
}



namespace App\Services;

use App\Contracts\PaymentProcessorInterface;
use Soap\LaravelOmise\Omise\Error;

class PromptPayPaymentProcessor implements PaymentProcessorInterface
{
    public function createPayment(float $amount, string $currency = 'THB', array $paymentDetails = []): array
    {
        $source = app('omise')->source()->create([
            'type' => 'promptpay',
            'amount' => $amount * 100,
            'currency' => $currency,

        ]);

        if ($source instanceof Error) {
            return [
                'code' => $source->getCode(),
                'error' => $source->getMessage(),
            ];
        }

        $charge = app('omise')->charge()->create([
            'amount' => $amount * 100,
            'currency' => $currency,
            'source' => $source->id,
            'exprires_at' => $paymentDetails['expires_at'] ?? null,
            'webhook_endpoints' => $paymentDetails['webhook_endpoints'] ?? null,
            'metadata' => isset($paymentDetails['metadata']) ? $paymentDetails['metadata'] : [],
        ]);

        if ($charge instanceof Error) {
            return [
                'code' => $charge->getCode(),
                'error' => $charge->getMessage(),
            ];
        }

        return [
            'charge_id' => $charge->id,
            'amount' => $charge->amount / 100,
            'currency' => $charge->currency,
            'status' => $charge->status,
            'qr_image' => $charge->source['scannable_code']['image']['download_uri'],
            'expires_at' => $charge->expires_at,
        ];
    }

    public function processPayment(array $paymentData): array
    {
        return [];
    }

    public function refundPayment(string $chargeId, float $amount): bool
    {
        return false;
    }

    public function hasRefundSupport(): bool
    {
        return false;
    }

    public function isOffline(): bool
    {
        return true;
    }
}

        $paymentProcessor = $this->paymentProcessorFactory->make($payment_method);

        $result = $paymentProcessor->createPayment($bookingAmount, $bookingCurrency, [
            'return_uri' => route('payment.step5', ['booking' => $booking->id]),
            'capture' => true,
            'token' => $request->omiseToken ?? null,
            'metadata' => ['booking_id' => $booking->id],
        ]);
        if ($paymentProcessor->isOffline()) {
            $payment = Payment::create([
                'amount' => $bookingAmount,
                'payment_status' => 'pending',
                'payment_gateway' => 'omise',
                'payment_details' => $result,
                'payment_method' => $payment_method,
                'currency' => $bookingCurrency,
                'booking_id' => $booking->id,
            ]);
            $booking->update([
                'status' => 'pending', // or confirmed
            ]);
            BookingCreated::dispatch($booking);

            ShoppingCart::destroy();

            return redirect(route('payment.step4', ['booking' => $booking->id]));
        } elseif ($result['status'] === 'successful' && $result['paid']) {
            $payment = Payment::create([
                'amount' => $bookingAmount,
                'payment_status' => 'paid',
                'payment_gateway' => 'omise',
                'payment_details' => $result,
                'payment_method' => $payment_method,
                'currency' => $bookingCurrency,
                'booking_id' => $booking->id,
            ]);

            $booking->update([
                'status' => 'confirmed',
                'workflow_state' => 'confirmed',
            ]);

            BookingCreated::dispatch($booking);
            BookingConfirmed::dispatch($booking);

            ShoppingCart::destroy();

            return redirect(route('payment.step5', ['booking' => $booking->id]));
        } else {
            $payment = Payment::create([
                'amount' => $bookingAmount,
                'payment_status' => 'failed',
                'payment_gateway' => 'omise',
                'payment_details' => $result,
                'payment_method' => $payment_method,
                'currency' => $bookingCurrency,
                'booking_id' => $booking->id,
            ]);
            BookingCreated::dispatch($booking);
            ShoppingCart::destroy();

            // Handle failed payment
            return redirect(route('payment.step4', ['booking' => $booking->id]));
        }
bash
php artisan vendor:publish --tag="omise-config"