From 83a42cf410ee19444cf736188c34ef1faf64497c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 3 Feb 2021 12:36:10 +0100 Subject: [PATCH 1/3] wip --- .../ClientPortal/PaymentController.php | 24 ------------- app/PaymentDrivers/CheckoutCom/CreditCard.php | 26 +++++++------- app/PaymentDrivers/CheckoutCom/Utilities.php | 1 + cypress.json | 7 ++-- .../checkout_credit_card.spec.js | 21 +++++++++++ cypress/support/account.js | 35 +++++++++++++++++++ cypress/support/artisan.js | 6 ++++ cypress/support/index.js | 4 ++- .../ninja2020/invoices/payment.blade.php | 8 ++--- 9 files changed, 87 insertions(+), 45 deletions(-) create mode 100644 cypress/integration/client_portal/checkout_credit_card.spec.js create mode 100644 cypress/support/account.js create mode 100644 cypress/support/artisan.js diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index adba7e252fb0..3fb7310054fe 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -260,24 +260,12 @@ class PaymentController extends Controller return $this->processCreditPayment($request, $data); } - try { return $gateway ->driver(auth()->user()->client) ->setPaymentMethod($payment_method_id) ->setPaymentHash($payment_hash) ->checkRequirements() ->processPaymentView($data); - } catch (\Exception $e) { - SystemLogger::dispatch( - $e->getMessage(), - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_ERROR, - SystemLog::TYPE_FAILURE, - auth('contact')->user()->client - ); - - throw new PaymentFailed($e->getMessage()); - } } public function response(PaymentResponseRequest $request) @@ -286,24 +274,12 @@ class PaymentController extends Controller $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->payment_hash])->first(); - try { return $gateway ->driver(auth()->user()->client) ->setPaymentMethod($request->input('payment_method_id')) ->setPaymentHash($payment_hash) ->checkRequirements() ->processPaymentResponse($request); - } catch (\Exception $e) { - SystemLogger::dispatch( - $e->getMessage(), - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_FAILURE, - SystemLog::TYPE_FAILURE, - auth('contact')->user()->client - ); - - throw new PaymentFailed($e->getMessage()); - } } /** diff --git a/app/PaymentDrivers/CheckoutCom/CreditCard.php b/app/PaymentDrivers/CheckoutCom/CreditCard.php index 5c4560abfaca..a1deb8422044 100644 --- a/app/PaymentDrivers/CheckoutCom/CreditCard.php +++ b/app/PaymentDrivers/CheckoutCom/CreditCard.php @@ -82,15 +82,6 @@ class CreditCard { $this->checkout->init(); - $cgt = ClientGatewayToken::query() - ->where('id', $this->decodePrimaryKey($request->input('token'))) - ->where('company_id', auth('contact')->user()->client->company->id) - ->first(); - - if (!$cgt) { - throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401); - } - $state = [ 'server_response' => json_decode($request->gateway_response), 'value' => $request->value, @@ -103,12 +94,11 @@ class CreditCard $state = array_merge($state, $request->all()); $state['store_card'] = boolval($state['store_card']); - $state['token'] = $cgt; $this->checkout->payment_hash->data = array_merge((array)$this->checkout->payment_hash->data, $state); $this->checkout->payment_hash->save(); - if ($request->has('token')) { + if ($request->has('token') && !is_null($request->token) && !empty($request->token)) { return $this->attemptPaymentUsingToken($request); } @@ -117,7 +107,16 @@ class CreditCard private function attemptPaymentUsingToken(PaymentResponseRequest $request) { - $method = new IdSource($this->checkout->payment_hash->data->token->token); + $cgt = ClientGatewayToken::query() + ->where('id', $this->decodePrimaryKey($request->input('token'))) + ->where('company_id', auth('contact')->user()->client->company->id) + ->first(); + + if (!$cgt) { + throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401); + } + + $method = new IdSource($cgt->token); return $this->completePayment($method, $request); } @@ -161,7 +160,7 @@ class CreditCard } if ($response->status == 'Pending') { - $this->checkout->confirmGatewayFee($request); + $this->checkout->confirmGatewayFee(); return $this->processPendingPayment($response); } @@ -171,7 +170,6 @@ class CreditCard PaymentFailureMailer::dispatch($this->checkout->client, $response->response_summary, $this->checkout->client->company, $this->checkout->payment_hash->data->value); - return $this->processUnsuccessfulPayment($response); } } catch (CheckoutHttpException $e) { diff --git a/app/PaymentDrivers/CheckoutCom/Utilities.php b/app/PaymentDrivers/CheckoutCom/Utilities.php index 62d5676a3a70..a4d222dbdb54 100644 --- a/app/PaymentDrivers/CheckoutCom/Utilities.php +++ b/app/PaymentDrivers/CheckoutCom/Utilities.php @@ -66,6 +66,7 @@ trait Utilities 'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])), 'amount' => $this->getParent()->payment_hash->data->raw_value, 'transaction_reference' => $_payment->id, + 'gateway_type_id' => GatewayType::CREDIT_CARD, ]; $payment = $this->getParent()->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); diff --git a/cypress.json b/cypress.json index e959015b2688..4e694ea4f01e 100644 --- a/cypress.json +++ b/cypress.json @@ -1,5 +1,8 @@ { "video": false, - "baseUrl": "http://localhost:8000/", - "chromeWebSecurity": false + "baseUrl": "http://localhost:8080/", + "chromeWebSecurity": false, + "env": { + "runningEnvironment": "docker" + } } diff --git a/cypress/integration/client_portal/checkout_credit_card.spec.js b/cypress/integration/client_portal/checkout_credit_card.spec.js new file mode 100644 index 000000000000..0904d9d0aeec --- /dev/null +++ b/cypress/integration/client_portal/checkout_credit_card.spec.js @@ -0,0 +1,21 @@ +context('Checkout.com gateway test', () => { + beforeEach(() => { + cy.viewport('macbook-13'); + }); + + it('should migrate & seed checkout', function () { + // cy.artisan('migrate:fresh --seed'); + // cy.artisan('ninja:create-single-account'); + cy.clientLogin(); + + cy.get('[data-cy=pay-now]').first().click(); + cy.get('[data-cy=pay-now-dropdown]').click(); + cy.get('[data-cy=pay-with-0]').click(); + + cy.getWithinIframe('#checkout-frames-card-number').type('4242424242424242'); + cy.getWithinIframe('#checkout-frames-expiry-date').type('12/30'); + cy.getWithinIframe('#checkout-frames-cvv').type('100'); + + cy.get('#pay-button').click(); + }); +}); diff --git a/cypress/support/account.js b/cypress/support/account.js new file mode 100644 index 000000000000..4be5996c72ff --- /dev/null +++ b/cypress/support/account.js @@ -0,0 +1,35 @@ +import axios from 'axios'; + +const baseUrl = Cypress.config().baseUrl.endsWith('/') + ? Cypress.config().baseUrl.slice(0, -1) + : Cypress.config().baseUrl; + +Cypress.Commands.add('createAdminAccount', () => { + let body = { + first_name: "Cypress", + last_name: "Testing", + email: "cypress_testing@example.com", + password: "password", + terms_of_service: true, + privacy_policy: true, + report_errors: true, + }; + + let headers = { + "Content-Type": "application/json", + "X-Requested-With": "XMLHttpRequest" + }; + + return axios.post(`${baseUrl}/api/v1/signup?first_load=true`, body, headers) + .then(response => { + console.log('Data from the request', response.data.data[0]); + return response.data.data[0]; + }) + .catch(e => { + throw "Unable to create an account for admin."; + }); +}); + +Cypress.Commands.add('createClientAccount', () => { + // .. +}); diff --git a/cypress/support/artisan.js b/cypress/support/artisan.js new file mode 100644 index 000000000000..d183e20ddce0 --- /dev/null +++ b/cypress/support/artisan.js @@ -0,0 +1,6 @@ +Cypress.Commands.add('artisan', (cmd) => { + let environment = Cypress.env('runningEnvironment'); + let prefix = environment === 'docker' ? 'docker-compose run --rm artisan' : 'php artisan'; + + return cy.exec(`${prefix} ${cmd}`); +}); diff --git a/cypress/support/index.js b/cypress/support/index.js index d68db96df269..7874a4e0db1c 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -14,7 +14,9 @@ // *********************************************************** // Import commands.js using ES2015 syntax: -import './commands' +import './commands'; +import './artisan'; +import './account'; // Alternatively you can use CommonJS syntax: // require('./commands') diff --git a/resources/views/portal/ninja2020/invoices/payment.blade.php b/resources/views/portal/ninja2020/invoices/payment.blade.php index 55cc78958f05..eca418b4195f 100644 --- a/resources/views/portal/ninja2020/invoices/payment.blade.php +++ b/resources/views/portal/ninja2020/invoices/payment.blade.php @@ -24,7 +24,7 @@
-