PHP code example of officeguy / laravel-sumit-gateway
1. Go to this page and download the library: Download officeguy/laravel-sumit-gateway 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/ */
officeguy / laravel-sumit-gateway example snippets
// במודל Payable שלכם
public function getVatRate(): ?float
{
return 17.0; // 17% מע"מ
}
public function isTaxEnabled(): bool
{
return true;
}
// הפקת קבלה ידנית לתשלום PayPal
DocumentService::createReceiptForExternalPayment($order, 'paypal', $transactionId);
// קבלת מספר תשלומים מקסימלי לסכום מסוים
$maxPayments = PaymentService::getMaximumPayments($amount);
// חיוב עם תשלומים
$result = PaymentService::processCharge($order, $paymentsCount = 6);
// תפיסת מסגרת
$result = PaymentService::authorizePayment($order, $amount);
// חיוב מאוחר יותר
$result = PaymentService::capturePayment($transactionId, $finalAmount);
// בדיקה אם במצב טסט
$isTest = app(SettingsService::class)->get('testing', false);
// שמירת טוקן לאחר חיוב
$token = OfficeGuyToken::createFromApiResponse($customer, $response);
// חיוב עם טוקן שמור
$result = PaymentService::processCharge($order, $payments, false, false, $token);
// קבלת טוקנים של לקוח
$tokens = OfficeGuyToken::where('owner_type', get_class($user))
->where('owner_id', $user->id)
->get();
use OfficeGuy\LaravelSumitGateway\Services\SubscriptionService;
// יצירת מנוי חדש
$subscription = SubscriptionService::create(
$user, // הלקוח
'תוכנית חודשית', // שם המנוי
99.00, // סכום
'ILS', // מטבע
1, // אינטרוול בחודשים
12, // מספר חיובים (null = ללא הגבלה)
$tokenId // טוקן לתשלום
);
// חיוב ראשוני
$result = SubscriptionService::processInitialCharge($subscription);
// חיוב ידני
$result = SubscriptionService::processRecurringCharge($subscription);
// השהיית מנוי
SubscriptionService::pause($subscription);
// חידוש מנוי
SubscriptionService::resume($subscription);
// ביטול מנוי
SubscriptionService::cancel($subscription);
use Illuminate\Support\Facades\Schedule;
// חיוב יומי בשעה 8:00
Schedule::command('sumit:process-recurring-payments')->dailyAt('08:00');
// או חיוב כל שעה
Schedule::command('sumit:process-recurring-payments')->hourly();
// עם דיווח על כשלונות
Schedule::command('sumit:process-recurring-payments')
->daily()
->emailOutputOnFailure('[email protected]');
use OfficeGuy\LaravelSumitGateway\Services\DonationService;
// בדיקה אם עגלה מכילה תרומות ומוצרים רגילים
$validation = DonationService::validateCart($order);
if (!$validation['valid']) {
return redirect()->back()->with('error', $validation['message']);
}
// קבלת סוג המסמך המתאים
$docType = DonationService::getDocumentType($order);
use OfficeGuy\LaravelSumitGateway\Services\UpsellService;
// חיוב עם טוקן ידוע
$result = UpsellService::processUpsellCharge($upsellOrder, $token, $parentOrderId);
// חיוב עם זיהוי אוטומטי של הטוקן
$result = UpsellService::processUpsellWithAutoToken($upsellOrder, $parentOrderId, $customer);
use OfficeGuy\LaravelSumitGateway\Models\WebhookEvent;
// קבלת כל האירועים שנכשלו
$failedEvents = WebhookEvent::failed()->get();
// קבלת אירועים של לקוח ספציפי
$customerEvents = WebhookEvent::forCustomer('[email protected]')->get();
// קבלת אירועים מסוג מסוים
$paymentEvents = WebhookEvent::ofType('payment_completed')
->with(['transaction', 'document'])
->get();
// אירועים מוכנים לשליחה חוזרת
$pendingRetries = WebhookEvent::readyForRetry()->get();
// אירועים ממוינים לפי תאריך
$recentEvents = WebhookEvent::orderBy('created_at', 'desc')
->limit(100)
->get();
// לכל אירוע יש גישה למשאבים הקשורים אליו
foreach ($paymentEvents as $event) {
// גישה לטרנזקציה
$transaction = $event->transaction;
if ($transaction) {
echo "Transaction ID: {$transaction->payment_id}";
echo "Amount: {$transaction->amount}";
}
// גישה למסמך
$document = $event->document;
if ($document) {
echo "Document Number: {$document->document_number}";
echo "Document URL: {$document->url}";
}
// גישה לטוקן
$token = $event->token;
if ($token) {
echo "Card: ****{$token->last_digits}";
}
// גישה למנוי
$subscription = $event->subscription;
if ($subscription) {
echo "Subscription: {$subscription->name}";
echo "Next Charge: {$subscription->next_charge_at}";
}
// גישה להזמנה (polymorphic)
$order = $event->order;
if ($order) {
echo "Order ID: {$order->id}";
}
}
use OfficeGuy\LaravelSumitGateway\Services\WebhookService;
// שליחה חוזרת של אירוע בודד
$event = WebhookEvent::find(123);
if ($event->canRetry()) {
$webhookService = app(WebhookService::class);
$success = $webhookService->send($event->event_type, $event->payload);
if ($success) {
$event->markAsSent(200);
} else {
$event->scheduleRetry(5); // retry in 5 minutes
}
}
// שליחה חוזרת לכל האירועים שנכשלו
$failedEvents = WebhookEvent::failed()->get();
foreach ($failedEvents as $event) {
if ($event->canRetry()) {
$event->scheduleRetry();
}
}
use OfficeGuy\LaravelSumitGateway\Models\WebhookEvent;
// יצירת אירוע חדש
$event = WebhookEvent::createEvent('payment_completed', [
'order_id' => 123,
'amount' => 99.00,
'currency' => 'ILS',
'customer_email' => '[email protected]',
], [
'transaction_id' => $transaction->id,
'document_id' => $document->id,
'webhook_url' => 'https://your-site.com/webhook',
]);
// סימון כנשלח
$event->markAsSent(200, ['received' => true]);
// סימון ככישלון
$event->markAsFailed('Connection timeout', 504);
// app/Console/Commands/SyncWebhooksToCrm.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use OfficeGuy\LaravelSumitGateway\Models\WebhookEvent;
use App\Services\CrmService;
class SyncWebhooksToCrm extends Command
{
protected $signature = 'crm:sync-webhooks';
protected $description = 'Sync payment webhooks to CRM';
public function handle(CrmService $crm)
{
// קבלת כל האירועים שטרם סונכרנו
$events = WebhookEvent::ofType('payment_completed')
->sent()
->where('synced_to_crm', false)
->with(['transaction', 'document'])
->get();
foreach ($events as $event) {
$crm->recordPurchase([
'email' => $event->customer_email,
'amount' => $event->amount,
'currency' => $event->currency,
'transaction_id' => $event->transaction?->payment_id,
'invoice_url' => $event->document?->url,
]);
$event->update(['synced_to_crm' => true]);
}
$this->info("Synced {$events->count()} events to CRM");
}
}
// app/Console/Commands/MonitorWebhookFailures.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use OfficeGuy\LaravelSumitGateway\Models\WebhookEvent;
use Illuminate\Support\Facades\Notification;
use App\Notifications\WebhookFailureAlert;
class MonitorWebhookFailures extends Command
{
protected $signature = 'webhooks:monitor';
protected $description = 'Monitor webhook failures and alert';
public function handle()
{
$failedCount = WebhookEvent::failed()
->where('created_at', '>=', now()->subHour())
->count();
if ($failedCount > 10) {
// שליחת התראה
Notification::route('slack', config('services.slack.webhook'))
->notify(new WebhookFailureAlert($failedCount));
$this->error("Alert sent: {$failedCount} failures in the last hour");
} else {
$this->info("All good: {$failedCount} failures in the last hour");
}
}
}
use Illuminate\Support\Facades\Schedule;
// עיבוד webhooks שממתינים לשליחה חוזרת
Schedule::command('sumit:process-webhook-retries')->everyFiveMinutes();
// דוח יומי
Schedule::command('webhooks:daily-report')->dailyAt('09:00');
// ניטור כשלונות
Schedule::command('webhooks:monitor')->everyThirtyMinutes();
// סנכרון עם CRM
Schedule::command('crm:sync-webhooks')->hourly();
// app/Listeners/HandleSumitWebhook.php
namespace App\Listeners;
use OfficeGuy\LaravelSumitGateway\Events\SumitWebhookReceived;
use OfficeGuy\LaravelSumitGateway\Models\SumitWebhook;
class HandleSumitWebhook
{
public function handle(SumitWebhookReceived $event): void
{
$webhook = $event->webhook;
switch ($webhook->event_type) {
case SumitWebhook::TYPE_CARD_CREATED:
$this->handleCardCreated($webhook);
break;
case SumitWebhook::TYPE_CARD_UPDATED:
$this->handleCardUpdated($webhook);
break;
case SumitWebhook::TYPE_CARD_DELETED:
$this->handleCardDeleted($webhook);
break;
case SumitWebhook::TYPE_CARD_ARCHIVED:
$this->handleCardArchived($webhook);
break;
}
}
protected function handleCardCreated(SumitWebhook $webhook): void
{
// טיפול ביצירת כרטיס
$cardType = $webhook->card_type;
$cardId = $webhook->card_id;
$payload = $webhook->payload;
if ($cardType === 'customer') {
// סנכרון לקוח חדש למערכת
Customer::create([
'sumit_id' => $cardId,
'name' => $payload['Name'] ?? '',
'email' => $payload['Email'] ?? '',
'phone' => $payload['Phone'] ?? '',
]);
} elseif ($cardType === 'document') {
// שמירת מסמך חדש
Document::create([
'sumit_id' => $cardId,
'number' => $payload['Number'] ?? '',
'amount' => $payload['Amount'] ?? 0,
]);
}
// סימון כמעובד
$webhook->markAsProcessed('Successfully synced');
}
protected function handleCardUpdated(SumitWebhook $webhook): void
{
// עדכון כרטיס קיים
$cardType = $webhook->card_type;
$cardId = $webhook->card_id;
if ($cardType === 'customer') {
Customer::where('sumit_id', $cardId)->update([
'name' => $webhook->payload['Name'] ?? '',
'email' => $webhook->payload['Email'] ?? '',
]);
}
$webhook->markAsProcessed('Successfully updated');
}
protected function handleCardDeleted(SumitWebhook $webhook): void
{
// מחיקת כרטיס
$cardType = $webhook->card_type;
$cardId = $webhook->card_id;
if ($cardType === 'customer') {
Customer::where('sumit_id', $cardId)->delete();
}
$webhook->markAsProcessed('Successfully deleted');
}
protected function handleCardArchived(SumitWebhook $webhook): void
{
// סימון כרטיס כמאורכב
$cardType = $webhook->card_type;
$cardId = $webhook->card_id;
if ($cardType === 'customer') {
Customer::where('sumit_id', $cardId)
->update(['archived' => true]);
}
$webhook->markAsProcessed('Successfully archived');
}
}
use OfficeGuy\LaravelSumitGateway\Models\SumitWebhook;
// קבלת webhooks שטרם טופלו
$pending = SumitWebhook::received()->get();
// קבלת webhooks לפי סוג אירוע
$createdCards = SumitWebhook::ofType('card_created')->get();
// קבלת webhooks לפי סוג כרטיס
$customerWebhooks = SumitWebhook::ofCardType('customer')->get();
// קבלת webhooks שנכשלו
$failed = SumitWebhook::failed()->get();
// קבלת webhooks של לקוח ספציפי
$customerWebhooks = SumitWebhook::forCustomer('CUST123')->get();
// סימון webhook כמעובד
$webhook->markAsProcessed('Synced to CRM', [
'transaction_id' => $transaction->id,
]);
// סימון webhook כנכשל
$webhook->markAsFailed('API error: 500');
// סימון webhook כמתעלם
$webhook->markAsIgnored('Duplicate webhook');
// מומלץ: עיבוד אסינכרוני
public function handle(Request $request): JsonResponse
{
// שמירה מהירה של ה-webhook
$webhook = SumitWebhook::createFromRequest(...);
// דחיית העיבוד ל-queue
ProcessSumitWebhookJob::dispatch($webhook);
// החזרת תשובה מיידית (תוך 10 שניות!)
return response()->json(['success' => true], 200);
}
// יצירת קישור תשלום להזמנה
$order = Order::find(123);
$checkoutUrl = route('officeguy.public.checkout', ['id' => $order->id]);
// שליחת הקישור ללקוח
Mail::to($order->customer_email)->send(new PaymentLinkEmail($checkoutUrl));
use OfficeGuy\LaravelSumitGateway\Contracts\Payable;
use OfficeGuy\LaravelSumitGateway\Support\Traits\PayableAdapter;
class Order extends Model implements Payable
{
use PayableAdapter;
// או מימוש ידני של המתודות
}
bash
# פרסום כל הקבצים בבת אחת
php artisan vendor:publish --provider="OfficeGuy\LaravelSumitGateway\OfficeGuyServiceProvider"
# או פרסום קבצים ספציפיים לפי תגית (tag)
php artisan vendor:publish --tag=<tag-name>