From e5d56f70b51b5c27faf6992a9fb963da744cf4be Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 11 Oct 2021 18:15:34 +0200 Subject: [PATCH] Added base classes --- app/PaymentDrivers/Stripe/BACS.php | 163 +++++++++++++++++++++ app/PaymentDrivers/Stripe/BECS.php | 163 +++++++++++++++++++++ app/PaymentDrivers/StripePaymentDriver.php | 22 +++ 3 files changed, 348 insertions(+) create mode 100644 app/PaymentDrivers/Stripe/BACS.php create mode 100644 app/PaymentDrivers/Stripe/BECS.php diff --git a/app/PaymentDrivers/Stripe/BACS.php b/app/PaymentDrivers/Stripe/BACS.php new file mode 100644 index 000000000000..160653b43e7b --- /dev/null +++ b/app/PaymentDrivers/Stripe/BACS.php @@ -0,0 +1,163 @@ +stripe = $stripe; + } + + public function authorizeView($data) + { + return render('gateways.stripe.bacs.authorize', $data); + } + + public function paymentView(array $data) { + $data['gateway'] = $this->stripe; + $data['payment_method_id'] = GatewayType::BACS; + $data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()); + $data['client'] = $this->stripe->client; + $data['customer'] = $this->stripe->findOrCreateCustomer()->id; + $data['country'] = $this->stripe->client->country->iso_3166_2; + $data['payment_hash'] = $this->stripe->payment_hash->hash; + + $intent = \Stripe\PaymentIntent::create([ + 'amount' => $data['stripe_amount'], + 'currency' => 'eur', + 'payment_method_types' => ['bacs_debit'], + 'customer' => $this->stripe->findOrCreateCustomer(), + 'description' => $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')), + + ]); + + $data['pi_client_secret'] = $intent->client_secret; + + $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]); + $this->stripe->payment_hash->save(); + + return render('gateways.stripe.bacs.pay', $data); + } + + public function paymentResponse(PaymentResponseRequest $request) + { + + $gateway_response = json_decode($request->gateway_response); + + $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all()); + $this->stripe->payment_hash->save(); + + if (property_exists($gateway_response, 'status') && $gateway_response->status == 'processing') { + + $this->stripe->init(); + $this->storePaymentMethod($gateway_response); + + return $this->processSuccessfulPayment($gateway_response->id); + } + + return $this->processUnsuccessfulPayment(); + + } + + public function processSuccessfulPayment(string $payment_intent) + { + $this->stripe->init(); + + $data = [ + 'payment_method' => $payment_intent, + 'payment_type' => PaymentType::BACS, + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), + 'transaction_reference' => $payment_intent, + 'gateway_type_id' => GatewayType::BACS, + ]; + + $this->stripe->createPayment($data, Payment::STATUS_PENDING); + + SystemLogger::dispatch( + ['response' => $this->stripe->payment_hash->data, 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + return redirect()->route('client.payments.index'); + } + + public function processUnsuccessfulPayment() + { + $server_response = $this->stripe->payment_hash->data; + + PaymentFailureMailer::dispatch( + $this->stripe->client, + $server_response, + $this->stripe->client->company, + $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()) + ); + + $message = [ + 'server_response' => $server_response, + 'data' => $this->stripe->payment_hash->data, + ]; + + SystemLogger::dispatch( + $message, + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + throw new PaymentFailed('Failed to process the payment.', 500); + } + + + private function storePaymentMethod($intent) + { + try { + + $method = $this->stripe->getStripePaymentMethod($intent->payment_method); + + $payment_meta = new \stdClass; + $payment_meta->brand = (string) \sprintf('%s (%s)', $method->sepa_debit->bank_code, ctrans('texts.bacs')); + $payment_meta->last4 = (string) $method->sepa_debit->last4; + $payment_meta->state = 'authorized'; + $payment_meta->type = GatewayType::BACS; + + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $intent->payment_method, + 'payment_method_id' => GatewayType::BACS, + ]; + + $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $method->customer]); + } catch (\Exception $e) { + return $this->stripe->processInternallyFailedPayment($this->stripe, $e); + } + } +} diff --git a/app/PaymentDrivers/Stripe/BECS.php b/app/PaymentDrivers/Stripe/BECS.php new file mode 100644 index 000000000000..178749565297 --- /dev/null +++ b/app/PaymentDrivers/Stripe/BECS.php @@ -0,0 +1,163 @@ +stripe = $stripe; + } + + public function authorizeView($data) + { + return render('gateways.stripe.becs.authorize', $data); + } + + public function paymentView(array $data) { + $data['gateway'] = $this->stripe; + $data['payment_method_id'] = GatewayType::BECS; + $data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()); + $data['client'] = $this->stripe->client; + $data['customer'] = $this->stripe->findOrCreateCustomer()->id; + $data['country'] = $this->stripe->client->country->iso_3166_2; + $data['payment_hash'] = $this->stripe->payment_hash->hash; + + $intent = \Stripe\PaymentIntent::create([ + 'amount' => $data['stripe_amount'], + 'currency' => 'eur', + 'payment_method_types' => ['au_becs_debit'], + 'customer' => $this->stripe->findOrCreateCustomer(), + 'description' => $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')), + + ]); + + $data['pi_client_secret'] = $intent->client_secret; + + $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]); + $this->stripe->payment_hash->save(); + + return render('gateways.stripe.becs.pay', $data); + } + + public function paymentResponse(PaymentResponseRequest $request) + { + + $gateway_response = json_decode($request->gateway_response); + + $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all()); + $this->stripe->payment_hash->save(); + + if (property_exists($gateway_response, 'status') && $gateway_response->status == 'processing') { + + $this->stripe->init(); + $this->storePaymentMethod($gateway_response); + + return $this->processSuccessfulPayment($gateway_response->id); + } + + return $this->processUnsuccessfulPayment(); + + } + + public function processSuccessfulPayment(string $payment_intent) + { + $this->stripe->init(); + + $data = [ + 'payment_method' => $payment_intent, + 'payment_type' => PaymentType::BECS, + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), + 'transaction_reference' => $payment_intent, + 'gateway_type_id' => GatewayType::BECS, + ]; + + $this->stripe->createPayment($data, Payment::STATUS_PENDING); + + SystemLogger::dispatch( + ['response' => $this->stripe->payment_hash->data, 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + return redirect()->route('client.payments.index'); + } + + public function processUnsuccessfulPayment() + { + $server_response = $this->stripe->payment_hash->data; + + PaymentFailureMailer::dispatch( + $this->stripe->client, + $server_response, + $this->stripe->client->company, + $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()) + ); + + $message = [ + 'server_response' => $server_response, + 'data' => $this->stripe->payment_hash->data, + ]; + + SystemLogger::dispatch( + $message, + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + throw new PaymentFailed('Failed to process the payment.', 500); + } + + + private function storePaymentMethod($intent) + { + try { + + $method = $this->stripe->getStripePaymentMethod($intent->payment_method); + + $payment_meta = new \stdClass; + $payment_meta->brand = (string) \sprintf('%s (%s)', $method->sepa_debit->bank_code, ctrans('texts.becs')); + $payment_meta->last4 = (string) $method->sepa_debit->last4; + $payment_meta->state = 'authorized'; + $payment_meta->type = GatewayType::BECS; + + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $intent->payment_method, + 'payment_method_id' => GatewayType::BECS, + ]; + + $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $method->customer]); + } catch (\Exception $e) { + return $this->stripe->processInternallyFailedPayment($this->stripe, $e); + } + } +} diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index b70ff0715dce..3aaf0f158074 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -38,6 +38,8 @@ use App\PaymentDrivers\Stripe\GIROPAY; use App\PaymentDrivers\Stripe\iDeal; use App\PaymentDrivers\Stripe\EPS; use App\PaymentDrivers\Stripe\Bancontact; +use App\PaymentDrivers\Stripe\BECS; +use App\PaymentDrivers\Stripe\BACS; use App\PaymentDrivers\Stripe\UpdatePaymentMethods; use App\PaymentDrivers\Stripe\Utilities; use App\Utils\Traits\MakesHash; @@ -87,6 +89,8 @@ class StripePaymentDriver extends BaseDriver GatewayType::IDEAL => iDeal::class, GatewayType::EPS => EPS::class, GatewayType::BANCONTACT => Bancontact::class, + GatewayType::BACS => BACS::class, + GatewayType::BECS => BECS::class, ]; const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE; @@ -200,6 +204,20 @@ class StripePaymentDriver extends BaseDriver && in_array($this->client->country->iso_3166_3, ["BEL"])) $types[] = GatewayType::BANCONTACT; + if ($this->client + && $this->client->currency() + && ($this->client->currency()->code == 'GBR') + && isset($this->client->country) + && in_array($this->client->country->iso_3166_3, ["GBP", "DEU"])) + $types[] = GatewayType::BACS; + + if ($this->client + && $this->client->currency() + && ($this->client->currency()->code == 'AUD') + && isset($this->client->country) + && in_array($this->client->country->iso_3166_3, ["AUS", "DEU"])) + $types[] = GatewayType::BECS; + return $types; } @@ -235,6 +253,10 @@ class StripePaymentDriver extends BaseDriver return 'gateways.stripe.eps'; case GatewayType::BANCONTACT: return 'gateways.stripe.bancontact'; + case GatewayType::BACS: + return 'gateways.stripe.bacs'; + case GatewayType::BECS: + return 'gateways.stripe.becs'; default: break; }