CreditCard::class, //maps GatewayType => Implementation class ]; const SYSTEM_LOG_TYPE = SystemLog::TYPE_SQUARE; public function init() { $this->square = new \Square\SquareClient([ 'accessToken' => $this->company_gateway->getConfigField('accessToken'), 'environment' => $this->company_gateway->getConfigField('testMode') ? \Square\Environment::SANDBOX : \Square\Environment::PRODUCTION, ]); return $this; /* This is where you boot the gateway with your auth credentials*/ } /* Returns an array of gateway types for the payment gateway */ public function gatewayTypes(): array { $types = []; $types[] = GatewayType::CREDIT_CARD; return $types; } /* Sets the payment method initialized */ public function setPaymentMethod($payment_method_id) { $class = self::$methods[$payment_method_id]; $this->payment_method = new $class($this); return $this; } public function authorizeView(array $data) { return $this->payment_method->authorizeView($data); //this is your custom implementation from here } public function authorizeResponse($request) { return $this->payment_method->authorizeResponse($request); //this is your custom implementation from here } public function processPaymentView(array $data) { return $this->payment_method->paymentView($data); //this is your custom implementation from here } public function processPaymentResponse($request) { return $this->payment_method->paymentResponse($request); //this is your custom implementation from here } public function refund(Payment $payment, $amount, $return_client_response = false) { $this->init(); $amount_money = new \Square\Models\Money(); $amount_money->setAmount($this->convertAmount($amount)); $amount_money->setCurrency($this->client->currency()->code); $body = new \Square\Models\RefundPaymentRequest(\Illuminate\Support\Str::random(32), $amount_money, $payment->transaction_reference); /** @var ApiResponse */ $response = $this->square->getRefundsApi()->refund($body); } public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) { $this->init(); $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total; $amount = $this->convertAmount($amount); $invoice = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->first(); if ($invoice) { $description = "Invoice {$invoice->number} for {$amount} for client {$this->client->present()->name()}"; } else { $description = "Payment with no invoice for amount {$amount} for client {$this->client->present()->name()}"; } $amount_money = new \Square\Models\Money(); $amount_money->setAmount($amount); $amount_money->setCurrency($this->client->currency()->code); $body = new \Square\Models\CreatePaymentRequest($cgt->token, \Illuminate\Support\Str::random(32), $amount_money); $body->setCustomerId($cgt->gateway_customer_reference); /** @var ApiResponse */ $response = $this->square->getPaymentsApi()->createPayment($body); $body = json_decode($response->getBody()); if ($response->isSuccess()) { $amount = array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total; $payment_record = []; $payment_record['amount'] = $amount; $payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER; $payment_record['gateway_type_id'] = GatewayType::CREDIT_CARD; $payment_record['transaction_reference'] = $body->payment->id; $payment = $this->createPayment($payment_record, Payment::STATUS_COMPLETED); SystemLogger::dispatch( ['response' => $response, 'data' => $payment_record], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_CHECKOUT, $this->client, $this->client->company, ); return $payment; } $this->unWindGatewayFees($payment_hash); $this->sendFailureMail($body->errors[0]->detail); $message = [ 'server_response' => $response, 'data' => $payment_hash->data, ]; SystemLogger::dispatch( $message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_SQUARE, $this->client, $this->client->company, ); return false; } public function createWebhooks() { $this->init(); $event_types = ['payment.created', 'payment.updated']; $subscription = new \Square\Models\WebhookSubscription(); $subscription->setName('Invoice Ninja Webhook Subscription'); $subscription->setEventTypes($event_types); $subscription->setNotificationUrl($this->company_gateway->webhookUrl()); // $subscription->setApiVersion('2021-12-15'); $body = new \Square\Models\CreateWebhookSubscriptionRequest($subscription); $body->setIdempotencyKey(\Illuminate\Support\Str::uuid()); $api_response = $this->square->getWebhookSubscriptionsApi()->createWebhookSubscription($body); if ($api_response->isSuccess()) { $result = $api_response->getResult(); } else { $errors = $api_response->getErrors(); } } public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null) { // header('Content-Type: text/plain'); // $webhook_payload = file_get_contents('php://input'); // if($request->header('cko-signature') == hash_hmac('sha256', $webhook_payload, $this->company_gateway->company->company_key)) { // CheckoutWebhook::dispatch($request->all(), $request->company_key, $this->company_gateway->id)->delay(10); // } else { // nlog("Hash Mismatch = {$request->header('cko-signature')} ".hash_hmac('sha256', $webhook_payload, $this->company_gateway->company->company_key)); // nlog($request->all()); // } // return response()->json(['success' => true]); } public function convertAmount($amount) { $precision = $this->client->currency()->precision; if ($precision == 0) { return $amount; } if ($precision == 1) { return $amount * 10; } if ($precision == 2) { return $amount * 100; } return $amount; } }