From d3b29d8ae25d6e5834349452ea6dd3b1b93f46f6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 14 May 2020 21:33:29 +1000 Subject: [PATCH] Fixes for Stripe (#3702) * Fixes for tests * Fixes for Stripe * Mail jobs --- app/Jobs/Mail/EntitySentEmail.php | 76 +++++++++++++++++++ .../Invoice/InvoiceEmailedNotification.php | 4 +- app/PaymentDrivers/StripePaymentDriver.php | 9 ++- app/Utils/Traits/Payment/Refundable.php | 5 +- tests/Feature/CreditTest.php | 48 ++---------- 5 files changed, 94 insertions(+), 48 deletions(-) create mode 100644 app/Jobs/Mail/EntitySentEmail.php diff --git a/app/Jobs/Mail/EntitySentEmail.php b/app/Jobs/Mail/EntitySentEmail.php new file mode 100644 index 000000000000..bbd48d59c7da --- /dev/null +++ b/app/Jobs/Mail/EntitySentEmail.php @@ -0,0 +1,76 @@ +company = $company; + + $this->user = $user; + + $this->invitation = $invitation; + + $this->entity = $invitation->{$entity_type}; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + MultiDB::setDb($this->company->db); + + //if we need to set an email driver do it now + $this->setMailDriver($this->entity->client->getSetting('email_sending_method')); + + } + + private function setMailDriver(string $driver) + { + switch ($driver) { + case 'default': + break; + case 'gmail': + $this->setGmailMailer(); + break; + default: + + break; + } + if($driver == 'default') + return; + } + + private function setGmailMailer() + { + $sending_user = $this->entity->client->getSetting('gmail_sending_user_id'); + + } +} diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index 28700b09b751..37031f9a7f70 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -55,7 +55,9 @@ class InvoiceEmailedNotification implements ShouldQueue //Fire mail notification here!!! //This allows us better control of how we - //handle the mailer + //handle the mailer + + EntitySentEmail::dispatch($invitation, 'invoice', $user, $invitation->company); } $notification->method = $methods; diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 037cb3b6046f..4353cdf2faae 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -289,6 +289,9 @@ class StripePaymentDriver extends BasePaymentDriver $customer = $payment_intent->customer; if ($payment_status == 'succeeded') { + + $charge_id = $payment_intent->charges->data[0]->id; + $this->init(); $stripe_payment_method = \Stripe\PaymentMethod::retrieve($payment_method); $stripe_payment_method_obj = $stripe_payment_method->jsonSerialize(); @@ -333,7 +336,7 @@ class StripePaymentDriver extends BasePaymentDriver $data = [ - 'payment_method' => $payment_method, + 'payment_method' => $charge_id, 'payment_type' => $payment_type, 'amount' => $server_response->amount, ]; @@ -471,12 +474,12 @@ class StripePaymentDriver extends BasePaymentDriver return $customer; } - public function refund(Payment $payment, $amount = null) + public function refund(Payment $payment, $amount) { $this->gateway(); $response = $this->gateway - ->refund(['transactionReference' => $payment->transaction_reference, 'amount' => $amount ?? $payment->amount]) + ->refund(['transactionReference'=>$payment->transaction_reference, 'amount' => $amount, 'currency' => $payment->client->getCurrencyCode()]) ->send(); if ($response->isSuccessful()) { diff --git a/app/Utils/Traits/Payment/Refundable.php b/app/Utils/Traits/Payment/Refundable.php index 1ece7ac11fed..df54f35d043c 100644 --- a/app/Utils/Traits/Payment/Refundable.php +++ b/app/Utils/Traits/Payment/Refundable.php @@ -165,12 +165,11 @@ trait Refundable $credit_note->number = $this->client->getNextCreditNumber($this->client); $credit_note->save(); - if ($data['gateway_refund'] !== false) { + if ($data['gateway_refund'] !== false && $total_refund > 0) { $gateway = CompanyGateway::find($this->company_gateway_id); if ($gateway) { - $amount = request()->has('amount') ? request()->amount : null; - $response = $gateway->driver($this->client)->refund($this, $amount); + $response = $gateway->driver($this->client)->refund($this, $total_refund); if (!$response) { throw new PaymentRefundFailed(); diff --git a/tests/Feature/CreditTest.php b/tests/Feature/CreditTest.php index 4b06c5c18607..adaeb097db71 100644 --- a/tests/Feature/CreditTest.php +++ b/tests/Feature/CreditTest.php @@ -34,63 +34,29 @@ class CreditTest extends TestCase public function testCreditsList() { - Account::all()->each(function($account) { - $account->delete(); - }); - - $data = [ - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, - 'password' => 'ALongAndBrilliantPassword123', - '_token' => csrf_token(), - 'privacy_policy' => 1, - 'terms_of_service' => 1 - ]; - - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - ])->post('/api/v1/signup?include=account', $data); - - $acc = $response->json(); - - $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - - $company_token = $account->default_company->tokens()->first(); - $token = $company_token->token; - $company = $company_token->company; - - $user = $company_token->user; - - $this->assertNotNull($company_token); - $this->assertNotNull($token); - $this->assertNotNull($user); - $this->assertNotNull($company); - - factory(Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) { + factory(Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) { factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, + 'user_id' => $this->user->id, 'client_id' => $c->id, - 'company_id' => $company->id, + 'company_id' => $this->company->id, 'is_primary' => 1 ]); factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, + 'user_id' => $this->user->id, 'client_id' => $c->id, - 'company_id' => $company->id + 'company_id' => $this->company->id ]); }); $client = Client::all()->first(); - factory(Credit::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + factory(Credit::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/credits'); $response->assertStatus(200);