diff --git a/app/Jobs/Mail/PaymentFailedMailer.php b/app/Jobs/Mail/PaymentFailedMailer.php new file mode 100644 index 000000000000..5512c4b1a986 --- /dev/null +++ b/app/Jobs/Mail/PaymentFailedMailer.php @@ -0,0 +1,109 @@ +payment_hash = $payment_hash; + $this->client = $client; + $this->error = $error; + $this->company = $company; + + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + + //Set DB + MultiDB::setDb($this->company->db); + + $settings = $this->client->getMergedSettings(); + $amount = 0; + + if($this->payment_hash) + $amount = array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total; + + //iterate through company_users + $this->company->company_users->each(function ($company_user) use($amount, $settings){ + + //determine if this user has the right permissions + $methods = $this->findCompanyUserNotificationType($company_user, ['payment_failure','all_notifications']); + + //if mail is a method type -fire mail!! + if (($key = array_search('mail', $methods)) !== false) { + unset($methods[$key]); + + $mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $amount, $this->payment_hash))->build(); + + $nmo = new NinjaMailerObject; + $nmo->mailable = new NinjaMailer($mail_obj); + $nmo->company = $this->company; + $nmo->to_user = $company_user->user; + $nmo->settings = $settings; + + NinjaMailerJob::dispatch($nmo); + + } + }); + + //add client payment failures here. + } + + + +} diff --git a/app/Jobs/Mail/PaymentFailureMailer.php b/app/Jobs/Mail/PaymentFailureMailer.php index a6051962e1d1..c54fb042377a 100644 --- a/app/Jobs/Mail/PaymentFailureMailer.php +++ b/app/Jobs/Mail/PaymentFailureMailer.php @@ -86,7 +86,7 @@ class PaymentFailureMailer implements ShouldQueue if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); - $mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $this->amount))->build(); + $mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $this->amount, null))->build(); $nmo = new NinjaMailerObject; $nmo->mailable = new NinjaMailer($mail_obj); diff --git a/app/Mail/Admin/PaymentFailureObject.php b/app/Mail/Admin/PaymentFailureObject.php index 45068871bcf6..9094536ed1ff 100644 --- a/app/Mail/Admin/PaymentFailureObject.php +++ b/app/Mail/Admin/PaymentFailureObject.php @@ -11,25 +11,29 @@ namespace App\Mail\Admin; +use App\Models\Client; +use App\Models\Company; use App\Models\Invoice; +use App\Models\PaymentHash; use App\Utils\Ninja; use App\Utils\Number; use App\Utils\Traits\MakesHash; -use stdClass; use Illuminate\Support\Facades\App; +use stdClass; class PaymentFailureObject { use MakesHash; - public $client; + public Client $client; - public $error; + public string $error; - public $company; + public Company $company; public $amount; + public PaymentHash $payment_hash; // private $invoices; /** @@ -40,7 +44,7 @@ class PaymentFailureObject * @param $company * @param $amount */ - public function __construct($client, $error, $company, $amount) + public function __construct(Client $client, string $error, Company $company, $amount, ?PaymentHash $payment_hash) { $this->client = $client; @@ -52,6 +56,8 @@ class PaymentFailureObject $this->company = $company; + $this->payment_hash = $payment_hash; + } public function build() @@ -65,8 +71,6 @@ class PaymentFailureObject /* Set customized translations _NOW_ */ $t->replace(Ninja::transformTranslations($this->company->settings)); - // $this->invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get(); - $mail_obj = new stdClass; $mail_obj->amount = $this->getAmount(); $mail_obj->subject = $this->getSubject(); @@ -106,32 +110,33 @@ class PaymentFailureObject 'client' => $this->client->present()->name() ] ), - 'message' => $this->error, + 'content' => ctrans( + 'texts.notification_invoice_payment_failed', + [ + 'client' => $this->client->present()->name(), + 'invoice' => $this->getDescription(), + 'amount' => Number::formatMoney($this->amount, $this->client) + ]), 'signature' => $signature, 'logo' => $this->company->present()->logo(), 'settings' => $this->client->getMergedSettings(), 'whitelabel' => $this->company->account->isPaid() ? true : false, 'url' => config('ninja.app_url'), 'button' => ctrans('texts.login'), - 'additional_info' => $this->buildFailedInvoices() + 'additional_info' => $this->error ]; return $data; } - private function buildFailedInvoices() + + public function getDescription(bool $abbreviated = false) { + if(!$this->payment_hash) + return ""; - $text = ''; - - // foreach($this->invoices as $invoice) - // { - - // $text .= ctrans('texts.notification_invoice_payment_failed_subject', ['invoice' => $invoice->number]) . "\n"; - - // } - - return $text; + return \implode(', ', collect($this->payment_hash->invoices())->pluck('invoice_number')->toArray()); } + } diff --git a/app/Models/Client.php b/app/Models/Client.php index aaf56737c465..1a739f288604 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -519,31 +519,7 @@ class Client extends BaseModel implements HasLocalePreference } return null; - // $company_gateways = $this->getSetting('company_gateway_ids'); - // if (strlen($company_gateways) >= 1) { - // $transformed_ids = $this->transformKeys(explode(',', $company_gateways)); - // $gateways = $this->company - // ->company_gateways - // ->whereIn('id', $transformed_ids) - // ->sortby(function ($model) use ($transformed_ids) { - // return array_search($model->id, $transformed_ids); - // }); - // } else { - // $gateways = $this->company->company_gateways; - // } - - // foreach ($gateways as $gateway) { - // if ($this->currency()->code == 'USD' && in_array(GatewayType::BANK_TRANSFER, $gateway->driver($this)->gatewayTypeEnabled(GatewayType::BANK_TRANSFER))) { - // return $gateway; - // } - - // if ($this->currency()->code == 'EUR' && in_array(GatewayType::SEPA, $gateway->driver($this)->gatewayTypeEnabled(GatewayType::SEPA))) { - // return $gateway; - // } - // } - - // return null; } public function getBankTransferMethodType() diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 5be23f8a6a6d..6c51acedf4bd 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -12,7 +12,6 @@ namespace App\Models; use App\Models\GatewayType; -use App\PaymentDrivers\BasePaymentDriver; use App\Utils\Number; use Illuminate\Database\Eloquent\SoftDeletes; use stdClass; @@ -134,11 +133,10 @@ class CompanyGateway extends BaseModel $class = 'App\\PaymentDrivers\\'.$this->gateway->provider.'PaymentDriver'; $class = str_replace('_', '', $class); - if (class_exists($class)) { + if (class_exists($class)) return $class; - } else { - return BasePaymentDriver::class; - } + + throw new \Exception("Payment Driver does not exist"); } /** diff --git a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php index e357d8682163..3c52fd146a0a 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php +++ b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Authorize; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -134,7 +133,15 @@ class AuthorizeCreditCard 'data' => $this->formatGatewayResponse($data, $vars), ]; - PaymentFailureMailer::dispatch($this->authorize->client, $response->getTransId(), $this->authorize->client->company, $amount); + $code = "Error"; + $description = "There was an error processing the payment"; + + if ($response->getErrors() != null) { + $code = $response->getErrors()[0]->getErrorCode(); + $description = $response->getErrors()[0]->getErrorText(); + } + + $this->authorize->sendFailureMail($description); SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_AUTHORIZE, $this->authorize->client, $this->authorize->client->company); @@ -212,7 +219,7 @@ class AuthorizeCreditCard $description = $response->getErrors()[0]->getErrorText(); } - PaymentFailureMailer::dispatch($this->authorize->client, $response->getTransId(), $this->authorize->client->company, $amount); + $this->authorize->sendFailureMail($description); $payment_hash = PaymentHash::where('hash', $request->input('payment_hash'))->firstOrFail(); diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index a572030dcc03..535546e3fb5c 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -19,7 +19,7 @@ use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Jobs\Mail\NinjaMailer; use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerObject; -use App\Jobs\Mail\PaymentFailureMailer; +use App\Jobs\Mail\PaymentFailedMailer; use App\Jobs\Util\SystemLogger; use App\Mail\Admin\ClientPaymentFailureObject; use App\Models\Client; @@ -375,13 +375,7 @@ class BaseDriver extends AbstractPaymentDriver $amount = array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total; - - PaymentFailureMailer::dispatch( - $gateway->client, - $error, - $gateway->client->company, - $amount - ); + $this->sendFailureMail($error); SystemLogger::dispatch( $gateway->payment_hash, @@ -395,6 +389,18 @@ class BaseDriver extends AbstractPaymentDriver throw new PaymentFailed($error, $e->getCode()); } + public function sendFailureMail(string $error) + { + + PaymentFailedMailer::dispatch( + $this->payment_hash, + $this->client->company, + $this->client, + $error + ); + + } + public function clientPaymentFailureMailer($error) { @@ -451,7 +457,7 @@ class BaseDriver extends AbstractPaymentDriver $this->unWindGatewayFees($this->payment_hash); - PaymentFailureMailer::dispatch($this->client, $error, $this->client->company, $this->payment_hash->data->amount_with_fee); + $this->sendFailureMail($error); $nmo = new NinjaMailerObject; $nmo->mailable = new NinjaMailer( (new ClientPaymentFailureObject($this->client, $error, $this->client->company, $this->payment_hash))->build() ); diff --git a/app/PaymentDrivers/BasePaymentDriver.php b/app/PaymentDrivers/BasePaymentDriver.php deleted file mode 100644 index 2c21a228eff9..000000000000 --- a/app/PaymentDrivers/BasePaymentDriver.php +++ /dev/null @@ -1,315 +0,0 @@ - $invoice->getRequestedAmount(), - 'currency' => $invoice->getCurrencyCode(), - 'returnUrl' => $completeUrl, - 'cancelUrl' => $this->invitation->getLink(), - 'description' => trans('texts.' . $invoice->getEntityType()) . " {$invoice->number}", - 'transactionId' => $invoice->number, - 'transactionType' => 'Purchase', - 'clientIp' => Request::getClientIp(), - ]; - */ -class BasePaymentDriver -{ - use SystemLogTrait; - use MakesHash; - - /* The company gateway instance*/ - public $company_gateway; - - /* The Omnipay payment driver instance*/ - protected $gateway; - - /* The Invitation */ - protected $invitation; - - /* Gateway capabilities */ - protected $refundable = false; - - /* Token billing */ - public $token_billing = false; - - /* Authorise payment methods */ - protected $can_authorise_credit_card = false; - - public function __construct(CompanyGateway $company_gateway, Client $client, $invitation = false) - { - $this->company_gateway = $company_gateway; - - $this->invitation = $invitation; - - $this->client = $client; - } - - /** - * Returns the Omnipay driver. - * @return stdClass Omnipay initialized object - */ - protected function gateway() - { - $this->gateway = Omnipay::create($this->company_gateway->gateway->provider); - $this->gateway->initialize((array) $this->company_gateway->getConfig()); - - return $this; - } - - /** - * Return the configuration fields for the - * Gatway. - * @return array The configuration fields - */ - public function getFields() - { - return $this->gateway->getDefaultParameters(); - } - - /** - * Returns the default gateway type. - */ - public function gatewayTypes() - { - return [ - GatewayType::CREDIT_CARD, - ]; - } - - public function getCompanyGatewayId(): int - { - return $this->company_gateway->id; - } - - /** - * Returns whether refunds are possible with the gateway. - * @return bool TRUE|FALSE - */ - public function getRefundable(): bool - { - return $this->refundable; - } - - /** - * Returns whether token billing is possible with the gateway. - * @return bool TRUE|FALSE - */ - public function getTokenBilling(): bool - { - return $this->token_billing; - } - - /** - * Returns whether gateway can - * authorise and credit card. - * @return bool [type] [description] - */ - public function canAuthoriseCreditCard(): bool - { - return $this->can_authorise_credit_card; - } - - /** - * Refunds a given payment. - * @param $payment - * @param int $amount - * @return false - */ - public function refundPayment($payment, $amount = 0) - { - if ($amount) { - $amount = min($amount, $payment->getCompletedAmount()); - } else { - $amount = $payment->getCompletedAmount(); - } - - if ($payment->is_deleted || ! $amount) { - return false; - } - - if ($payment->type_id == Payment::TYPE_CREDIT_CARD) { - return $payment->recordRefund($amount); - } - - $details = $this->refundDetails($payment, $amount); - $response = $this->gateway()->refund($details)->send(); - - if ($response->isSuccessful()) { - return $payment->recordRefund($amount); - } elseif ($this->attemptVoidPayment($response, $payment, $amount)) { - $details = ['transactionReference' => $payment->transaction_reference]; - $response = $this->gateway->void($details)->send(); - if ($response->isSuccessful()) { - return $payment->markVoided(); - } - } - - return false; - } - - protected function attemptVoidPayment($response, $payment, $amount) - { - // Partial refund not allowed for unsettled transactions - return $amount == $payment->amount; - } - - public function authorizeCreditCardView(array $data) - { - } - - public function authorizeCreditCardResponse($request) - { - } - - public function processPaymentView(array $data) - { - } - - public function processPaymentResponse($request) - { - } - - /** - * Return the contact if possible. - * - * @return ClientContact The ClientContact object - */ - public function getContact() - { - if ($this->invitation) { - return ClientContact::find($this->invitation->client_contact_id); - } elseif (auth()->guard('contact')->user()) { - return auth()->user(); - } else { - return false; - } - } - - /************************************* Omnipay ****************************************** - * authorize($options) - authorize an amount on the customer's card - * completeAuthorize($options) - handle return from off-site gateways after authorization - * capture($options) - capture an amount you have previously authorized - * purchase($options) - authorize and immediately capture an amount on the customer's card - * completePurchase($options) - handle return from off-site gateways after purchase - * refund($options) - refund an already processed transaction - * void($options) - generally can only be called up to 24 hours after submitting a transaction - * acceptNotification() - convert an incoming request from an off-site gateway to a generic notification object for further processing - * @param $input - * @return array - */ - - protected function paymentDetails($input): array - { - $data = [ - 'currency' => $this->client->getCurrencyCode(), - 'transactionType' => 'Purchase', - 'clientIp' => request()->getClientIp(), - ]; - - return $data; - } - - public function purchase($data, $items) - { - $this->gateway(); - - $response = $this->gateway - ->purchase($data) - ->setItems($items) - ->send(); - - return $response; - } - - public function completePurchase($data) - { - $this->gateway(); - - return $this->gateway - ->completePurchase($data) - ->send(); - } - - public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment - { - $payment = PaymentFactory::create($this->client->company->id, $this->client->user->id); - $payment->client_id = $this->client->id; - $payment->company_gateway_id = $this->company_gateway->id; - $payment->status_id = $status; - $payment->currency_id = $this->client->getSetting('currency_id'); - $payment->date = Carbon::now(); - - return $payment->service()->applyNumber()->save(); - } - - public function attachInvoices(Payment $payment, PaymentHash $payment_hash): Payment - { - $paid_invoices = $payment_hash->invoices(); - $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get(); - $payment->invoices()->sync($invoices); - $payment->saveQuietly(); - - event('eloquent.created: App\Models\Payment', $payment); - - return $payment; - } - - /** - * When a successful payment is made, we need to append the gateway fee - * to an invoice. - * - * @param PaymentResponseRequest $request The incoming payment request - * @return void Success/Failure - */ - public function confirmGatewayFee(PaymentResponseRequest $request) :void - { - /*Payment meta data*/ - $payment_hash = $request->getPaymentHash(); - - /*Payment invoices*/ - $payment_invoices = $payment_hash->invoices(); - - // /*Fee charged at gateway*/ - $fee_total = $payment_hash->fee_total; - - // Sum of invoice amounts - // $invoice_totals = array_sum(array_column($payment_invoices,'amount')); - - /*Hydrate invoices*/ - $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->withTrashed()->get(); - - $invoices->each(function ($invoice) use ($fee_total) { - if (collect($invoice->line_items)->contains('type_id', '3')) { - $invoice->service()->toggleFeesPaid()->save(); - $invoice->client->service()->updateBalance($fee_total)->save(); - $invoice->ledger()->updateInvoiceBalance($fee_total, "Gateway fee adjustment for Invoice {$invoice->number}"); - } - }); - } -} diff --git a/app/PaymentDrivers/Braintree/ACH.php b/app/PaymentDrivers/Braintree/ACH.php index 1a91473d5b07..25cd07e2cc4d 100644 --- a/app/PaymentDrivers/Braintree/ACH.php +++ b/app/PaymentDrivers/Braintree/ACH.php @@ -14,7 +14,6 @@ namespace App\PaymentDrivers\Braintree; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -158,15 +157,8 @@ class ACH implements MethodInterface private function processUnsuccessfulPayment($response) { - PaymentFailureMailer::dispatch($this->braintree->client, $response->transaction->additionalProcessorResponse, $this->braintree->client->company, $this->braintree->payment_hash->data->amount_with_fee); - - PaymentFailureMailer::dispatch( - $this->braintree->client, - $response, - $this->braintree->client->company, - $this->braintree->payment_hash->data->amount_with_fee, - ); - + $this->braintree->sendFailureMail($response->transaction->additionalProcessorResponse); + $message = [ 'server_response' => $response, 'data' => $this->braintree->payment_hash->data, diff --git a/app/PaymentDrivers/Braintree/CreditCard.php b/app/PaymentDrivers/Braintree/CreditCard.php index 2455eb176e59..a00dd398a7f9 100644 --- a/app/PaymentDrivers/Braintree/CreditCard.php +++ b/app/PaymentDrivers/Braintree/CreditCard.php @@ -16,7 +16,6 @@ namespace App\PaymentDrivers\Braintree; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -113,13 +112,13 @@ class CreditCard $result = $this->braintree->gateway->transaction()->sale($data); } catch(\Exception $e) { if ($e instanceof \Braintree\Exception\Authorization) { + + $this->braintree->sendFailureMail(ctrans('texts.generic_gateway_error')); + throw new PaymentFailed(ctrans('texts.generic_gateway_error'), $e->getCode()); } - PaymentFailureMailer::dispatch($this->braintree->client, - $e->getMessage(), - $this->braintree->client->company, - $this->braintree->payment_hash->data->amount_with_fee); + $this->braintree->sendFailureMail($e->getMessage()); throw new PaymentFailed($e->getMessage(), $e->getCode()); } @@ -167,6 +166,8 @@ class CreditCard return $response->paymentMethod->token; } + $this->braintree->sendFailureMail($response->message); + throw new PaymentFailed($response->message); } @@ -200,7 +201,8 @@ class CreditCard */ private function processUnsuccessfulPayment($response) { - PaymentFailureMailer::dispatch($this->braintree->client, $response->transaction->additionalProcessorResponse, $this->braintree->client->company, $this->braintree->payment_hash->data->amount_with_fee); + + $this->braintree->sendFailureMail($response->transaction->additionalProcessorResponse); $message = [ 'server_response' => $response, diff --git a/app/PaymentDrivers/Braintree/PayPal.php b/app/PaymentDrivers/Braintree/PayPal.php index ebba89ea5be8..94aa0fffd063 100644 --- a/app/PaymentDrivers/Braintree/PayPal.php +++ b/app/PaymentDrivers/Braintree/PayPal.php @@ -6,7 +6,6 @@ namespace App\PaymentDrivers\Braintree; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -149,14 +148,7 @@ class PayPal private function processUnsuccessfulPayment($response) { - PaymentFailureMailer::dispatch($this->braintree->client, $response->message, $this->braintree->client->company, $this->braintree->payment_hash->data->amount_with_fee); - - PaymentFailureMailer::dispatch( - $this->braintree->client, - $response, - $this->braintree->client->company, - $this->braintree->payment_hash->data->amount_with_fee, - ); + $this->braintree->sendFailureMail($response->message); $message = [ 'server_response' => $response, diff --git a/app/PaymentDrivers/BraintreePaymentDriver.php b/app/PaymentDrivers/BraintreePaymentDriver.php index 2c5cc40a507b..d360bdf1f819 100644 --- a/app/PaymentDrivers/BraintreePaymentDriver.php +++ b/app/PaymentDrivers/BraintreePaymentDriver.php @@ -14,7 +14,6 @@ namespace App\PaymentDrivers; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -240,7 +239,7 @@ class BraintreePaymentDriver extends BaseDriver if (!$result->success) { $this->unWindGatewayFees($payment_hash); - PaymentFailureMailer::dispatch($this->client, $result->transaction->additionalProcessorResponse, $this->client->company, $this->payment_hash->data->amount_with_fee); + $this->sendFailureMail($result->transaction->additionalProcessorResponse); $message = [ 'server_response' => $result, diff --git a/app/PaymentDrivers/CheckoutCom/CreditCard.php b/app/PaymentDrivers/CheckoutCom/CreditCard.php index 4859e2f1cb9f..317153749afa 100644 --- a/app/PaymentDrivers/CheckoutCom/CreditCard.php +++ b/app/PaymentDrivers/CheckoutCom/CreditCard.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers\CheckoutCom; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\PaymentDrivers\CheckoutComPaymentDriver; @@ -210,8 +209,9 @@ class CreditCard implements MethodInterface if ($response->status == 'Declined') { $this->checkout->unWindGatewayFees($this->checkout->payment_hash); - PaymentFailureMailer::dispatch($this->checkout->client, $response->response_summary, $this->checkout->client->company, $this->checkout->payment_hash->data->value); + $this->checkout->sendFailureMail($response->response_summary); + //@todo - this will double up the checkout . com failed mails $this->checkout->clientPaymentFailureMailer($response->status); return $this->processUnsuccessfulPayment($response); diff --git a/app/PaymentDrivers/CheckoutCom/Utilities.php b/app/PaymentDrivers/CheckoutCom/Utilities.php index a3e3fb4bba55..9790a5ca8d2d 100644 --- a/app/PaymentDrivers/CheckoutCom/Utilities.php +++ b/app/PaymentDrivers/CheckoutCom/Utilities.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\CheckoutCom; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\PaymentType; @@ -85,12 +84,8 @@ trait Utilities public function processUnsuccessfulPayment(Payment $_payment, $throw_exception = true) { - PaymentFailureMailer::dispatch( - $this->getParent()->client, - $_payment, - $this->getParent()->client->company, - $this->getParent()->payment_hash->data->value - ); + + $this->getParent()->sendFailureMail($_payment->status . " " . $_payment->response_summary); $message = [ 'server_response' => $_payment, @@ -107,7 +102,7 @@ trait Utilities ); if ($throw_exception) { - throw new PaymentFailed($_payment->status, $_payment->http_code); + throw new PaymentFailed($_payment->status . " " . $_payment->response_summary, $_payment->http_code); } } diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 7b4f2aea4a6f..1cb3443c014f 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Gateways\Checkout3ds\Checkout3dsRequest; use App\Http\Requests\Payments\PaymentWebhookRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\Company; @@ -284,11 +283,7 @@ class CheckoutComPaymentDriver extends BaseDriver if ($response->status == 'Declined') { $this->unWindGatewayFees($payment_hash); - PaymentFailureMailer::dispatch( - $this->client, $response->response_summary, - $this->client->company, - $amount - ); + $this->sendFailureMail($response->status . " " . $response->response_summary); $message = [ 'server_response' => $response, @@ -319,7 +314,16 @@ class CheckoutComPaymentDriver extends BaseDriver 'message' => $message, ]; - SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client, $this->client->company); + $this->sendFailureMail($message); + + SystemLogger::dispatch( + $data, + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_CHECKOUT, + $this->client, + $this->client->company + ); } } diff --git a/app/PaymentDrivers/Eway/CreditCard.php b/app/PaymentDrivers/Eway/CreditCard.php index eec96e743f42..3ef197c948c0 100644 --- a/app/PaymentDrivers/Eway/CreditCard.php +++ b/app/PaymentDrivers/Eway/CreditCard.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Eway; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; diff --git a/app/PaymentDrivers/Eway/Token.php b/app/PaymentDrivers/Eway/Token.php index 9f9ac8ac2ae6..7b20629ca44f 100644 --- a/app/PaymentDrivers/Eway/Token.php +++ b/app/PaymentDrivers/Eway/Token.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Eway; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -73,8 +72,8 @@ class Token $amount = array_sum(array_column($this->eway_driver->payment_hash->invoices(), 'amount')) + $this->eway_driver->payment_hash->fee_total; $data = [ - 'gateway_type_id' => $cgt->gateway_type_id, - 'payment_type' => GatewayType::CREDIT_CARD_OTHER, + 'gateway_type_id' => GatewayType::CREDIT_CARD, + 'payment_type' => PaymentType::CREDIT_CARD_OTHER, 'transaction_reference' => $response->Customer->Reference, 'amount' => $amount, ]; @@ -83,8 +82,8 @@ class Token $payment->meta = $cgt->meta; $payment->save(); - $payment_hash->payment_id = $payment->id; - $payment_hash->save(); + $this->eway_driver->payment_hash->payment_id = $payment->id; + $this->eway_driver->payment_hash->save(); return $payment; diff --git a/app/PaymentDrivers/GoCardless/ACH.php b/app/PaymentDrivers/GoCardless/ACH.php index 2dd94a453b25..2efd6f39a88b 100644 --- a/app/PaymentDrivers/GoCardless/ACH.php +++ b/app/PaymentDrivers/GoCardless/ACH.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers\GoCardless; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -228,15 +227,8 @@ class ACH implements MethodInterface */ public function processUnsuccessfulPayment(\GoCardlessPro\Resources\Payment $payment) { - PaymentFailureMailer::dispatch($this->go_cardless->client, $payment->status, $this->go_cardless->client->company, $this->go_cardless->payment_hash->data->amount_with_fee); - - PaymentFailureMailer::dispatch( - $this->go_cardless->client, - $payment, - $this->go_cardless->client->company, - $payment->amount - ); - + $this->go_cardless->sendFailureMail($payment->status); + $message = [ 'server_response' => $payment, 'data' => $this->go_cardless->payment_hash->data, diff --git a/app/PaymentDrivers/GoCardlessPaymentDriver.php b/app/PaymentDrivers/GoCardlessPaymentDriver.php index 770a272c7d37..190321b8832e 100644 --- a/app/PaymentDrivers/GoCardlessPaymentDriver.php +++ b/app/PaymentDrivers/GoCardlessPaymentDriver.php @@ -12,7 +12,6 @@ namespace App\PaymentDrivers; use App\Http\Requests\Payments\PaymentWebhookRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -148,12 +147,7 @@ class GoCardlessPaymentDriver extends BaseDriver return $payment; } - PaymentFailureMailer::dispatch( - $this->client, - $payment->status, - $this->client->company, - $amount - ); + $this->sendFailureMail($payment->status); $message = [ 'server_response' => $payment, diff --git a/app/PaymentDrivers/Mollie/Bancontact.php b/app/PaymentDrivers/Mollie/Bancontact.php index ee2b3e2bd2da..d8a0eab78807 100644 --- a/app/PaymentDrivers/Mollie/Bancontact.php +++ b/app/PaymentDrivers/Mollie/Bancontact.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers\Mollie; use App\Exceptions\PaymentFailed; use App\Http\Requests\Request; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -109,12 +108,8 @@ class Bancontact implements MethodInterface */ public function processUnsuccessfulPayment(\Exception $exception): void { - PaymentFailureMailer::dispatch( - $this->mollie->client, - $exception->getMessage(), - $this->mollie->client->company, - $this->mollie->payment_hash->data->amount_with_fee - ); + + $this->mollie->sendFailureMail($exception->getMessage()); SystemLogger::dispatch( $exception->getMessage(), diff --git a/app/PaymentDrivers/Mollie/BankTransfer.php b/app/PaymentDrivers/Mollie/BankTransfer.php index 1cfb0651d13f..3002d201ca3e 100644 --- a/app/PaymentDrivers/Mollie/BankTransfer.php +++ b/app/PaymentDrivers/Mollie/BankTransfer.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers\Mollie; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -112,12 +111,8 @@ class BankTransfer implements MethodInterface */ public function processUnsuccessfulPayment(\Exception $e): void { - PaymentFailureMailer::dispatch( - $this->mollie->client, - $e->getMessage(), - $this->mollie->client->company, - $this->mollie->payment_hash->data->amount_with_fee - ); + + $this->mollie->sendFailureMail($e->getMessage()); SystemLogger::dispatch( $e->getMessage(), diff --git a/app/PaymentDrivers/Mollie/CreditCard.php b/app/PaymentDrivers/Mollie/CreditCard.php index f8f5f57f9cdb..7efbd17c915c 100644 --- a/app/PaymentDrivers/Mollie/CreditCard.php +++ b/app/PaymentDrivers/Mollie/CreditCard.php @@ -4,7 +4,6 @@ namespace App\PaymentDrivers\Mollie; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -88,6 +87,8 @@ class CreditCard return redirect($payment->getCheckoutUrl()); } } catch (\Exception $e) { + + return $this->processUnsuccessfulPayment($e); } } @@ -142,6 +143,7 @@ class CreditCard return redirect($payment->getCheckoutUrl()); } } catch (\Exception $e) { + $this->processUnsuccessfulPayment($e); throw new PaymentFailed($e->getMessage(), $e->getCode()); @@ -196,12 +198,8 @@ class CreditCard public function processUnsuccessfulPayment(\Exception $e) { - PaymentFailureMailer::dispatch( - $this->mollie->client, - $e->getMessage(), - $this->mollie->client->company, - $this->mollie->payment_hash->data->amount_with_fee - ); + + $this->mollie->sendFailureMail($e->getMessage()); SystemLogger::dispatch( $e->getMessage(), diff --git a/app/PaymentDrivers/Mollie/IDEAL.php b/app/PaymentDrivers/Mollie/IDEAL.php index a7c95f310211..20ac61b3cebe 100644 --- a/app/PaymentDrivers/Mollie/IDEAL.php +++ b/app/PaymentDrivers/Mollie/IDEAL.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers\Mollie; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -109,12 +108,8 @@ class IDEAL implements MethodInterface */ public function processUnsuccessfulPayment(\Exception $exception): void { - PaymentFailureMailer::dispatch( - $this->mollie->client, - $exception->getMessage(), - $this->mollie->client->company, - $this->mollie->payment_hash->data->amount_with_fee - ); + + $this->mollie->sendFailureMail($exception->getMessage()); SystemLogger::dispatch( $exception->getMessage(), diff --git a/app/PaymentDrivers/Mollie/KBC.php b/app/PaymentDrivers/Mollie/KBC.php index bb2386c8b6e6..d608a62b89ca 100644 --- a/app/PaymentDrivers/Mollie/KBC.php +++ b/app/PaymentDrivers/Mollie/KBC.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers\Mollie; use App\Exceptions\PaymentFailed; use App\Http\Requests\Request; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -109,12 +108,8 @@ class KBC implements MethodInterface */ public function processUnsuccessfulPayment(\Exception $exception): void { - PaymentFailureMailer::dispatch( - $this->mollie->client, - $exception->getMessage(), - $this->mollie->client->company, - $this->mollie->payment_hash->data->amount_with_fee - ); + + $this->mollie->sendFailureMail($exception->getMessage()); SystemLogger::dispatch( $exception->getMessage(), diff --git a/app/PaymentDrivers/MolliePaymentDriver.php b/app/PaymentDrivers/MolliePaymentDriver.php index cb09fbb2db57..8ec71a6d2a18 100644 --- a/app/PaymentDrivers/MolliePaymentDriver.php +++ b/app/PaymentDrivers/MolliePaymentDriver.php @@ -15,7 +15,6 @@ namespace App\PaymentDrivers; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Gateways\Mollie\Mollie3dsRequest; use App\Http\Requests\Payments\PaymentWebhookRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -247,12 +246,7 @@ class MolliePaymentDriver extends BaseDriver $this->unWindGatewayFees($payment_hash); - PaymentFailureMailer::dispatch( - $this->client, - $payment->details, - $this->client->company, - $amount - ); + $this->sendFailureMail($payment->details); $message = [ 'server_response' => $payment, diff --git a/app/PaymentDrivers/PayFast/CreditCard.php b/app/PaymentDrivers/PayFast/CreditCard.php index 796c59ce4d19..be021760fa17 100644 --- a/app/PaymentDrivers/PayFast/CreditCard.php +++ b/app/PaymentDrivers/PayFast/CreditCard.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\PayFast; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -253,15 +252,8 @@ class CreditCard private function processUnsuccessfulPayment($server_response) { - PaymentFailureMailer::dispatch($this->payfast->client, $server_response->cancellation_reason, $this->payfast->client->company, $server_response->amount); - - PaymentFailureMailer::dispatch( - $this->payfast->client, - $server_response, - $this->payfast->client->company, - $server_response['amount_gross'] - ); - + $this->payfast->sendFailureMail($server_response->cancellation_reason); + $message = [ 'server_response' => $server_response, 'data' => $this->payfast->payment_hash->data, diff --git a/app/PaymentDrivers/PayFast/Token.php b/app/PaymentDrivers/PayFast/Token.php index 43208c58dd36..35433cae9fa8 100644 --- a/app/PaymentDrivers/PayFast/Token.php +++ b/app/PaymentDrivers/PayFast/Token.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\PayFast; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -32,45 +31,11 @@ class Token public $payfast; - //https://api.payfast.co.za/subscriptions/dc0521d3-55fe-269b-fa00-b647310d760f/adhoc - public function __construct(PayFastPaymentDriver $payfast) { $this->payfast = $payfast; } - // Attributes - // merchant-id - // integer, 8 char | REQUIRED - // Header, the Merchant ID as given by the PayFast system. - // version - // string | REQUIRED - // Header, the PayFast API version (i.e. v1). - // timestamp - // ISO-8601 date and time | REQUIRED - // Header, the current timestamp (YYYY-MM-DDTHH:MM:SS[+HH:MM]). - // signature - // string | REQUIRED - // Header, MD5 hash of the alphabetised submitted header and body variables, as well as the passphrase. Characters must be in lower case. - // amount - // integer | REQUIRED - // Body, the amount which the buyer must pay, in cents (ZAR), no decimals. - // item_name - // string, 100 char | REQUIRED - // Body, the name of the item being charged for. - // item_description - // string, 255 char | OPTIONAL - // Body, the description of the item being charged for. - // itn - // boolean | OPTIONAL - // Body, specify whether an ITN must be sent for the tokenization payment (true by default). - // m_payment_id - // string, 100 char | OPTIONAL - // Body, unique payment ID on the merchant’s system. - // cc_cvv - // numeric | OPTIONAL - - public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) { @@ -89,10 +54,6 @@ class Token 'item_description' => ctrans('texts.invoices') . ': ' . collect($payment_hash->invoices())->pluck('invoice_number'), 'm_payment_id' => $payment_hash->hash, ]; - - nlog(array_merge($body, $header)); - - // $header['signature'] = md5( $this->generate_parameter_string(array_merge($header, $body), false) ); $header['signature'] = $this->payfast->generateTokenSignature(array_merge($body, $header)); @@ -100,63 +61,6 @@ class Token $result = $this->send($header, $body, $cgt->token); - nlog($result); - - // $api = new \PayFast\PayFastApi( - // [ - // 'merchantId' => $this->payfast->company_gateway->getConfigField('merchantId'), - // 'passPhrase' => $this->payfast->company_gateway->getConfigField('passPhrase'), - // 'testMode' => $this->payfast->company_gateway->getConfigField('testMode') - // ] - // ); - - // $adhocArray = $api - // ->subscriptions - // ->adhoc($cgt->token, ['amount' => $amount, 'item_name' => 'purchase']); - - - // nlog($adhocArray); - - - - // /*Refactor and push to BaseDriver*/ - // if ($data['response'] != null && $data['response']->getMessages()->getResultCode() == 'Ok') { - - // $response = $data['response']; - - // $this->storePayment($payment_hash, $data); - - // $vars = [ - // 'invoices' => $payment_hash->invoices(), - // 'amount' => $amount, - // ]; - - // $logger_message = [ - // 'server_response' => $response->getTransactionResponse()->getTransId(), - // 'data' => $this->formatGatewayResponse($data, $vars), - // ]; - - // SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_AUTHORIZE, $this->authorize->client, $this->authorize->client->company); - - // return true; - // } else { - - // $vars = [ - // 'invoices' => $payment_hash->invoices(), - // 'amount' => $amount, - // ]; - - // $logger_message = [ - // 'server_response' => $response->getTransactionResponse()->getTransId(), - // 'data' => $this->formatGatewayResponse($data, $vars), - // ]; - - // PaymentFailureMailer::dispatch($this->authorize->client, $response->getTransactionResponse()->getTransId(), $this->authorize->client->company, $amount); - - // SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_AUTHORIZE, $this->authorize->client, $this->authorize->client->company); - - // return false; - // } } protected function generate_parameter_string( $api_data, $sort_data_before_merge = true, $skip_empty_values = true ) { diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 88de81b4823a..2ee33668b8d7 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Invoice; @@ -94,7 +93,7 @@ class PayPalExpressPaymentDriver extends BaseDriver return $response->redirect(); } - PaymentFailureMailer::dispatch($this->client, $response->getData(), $this->client->company, $data['total']['amount_with_fee']); + $this->sendFailureMail($response->getData()); $message = [ 'server_response' => $response->getMessage(), @@ -151,8 +150,8 @@ class PayPalExpressPaymentDriver extends BaseDriver if (!$response->isSuccessful()) { $data = $response->getData(); - - PaymentFailureMailer::dispatch($this->client, $response->getMessage(), $this->client->company, $this->payment_hash->data->amount); + + $this->sendFailureMail($response->getMessage()); $message = [ 'server_response' => $data['L_LONGMESSAGE0'], diff --git a/app/PaymentDrivers/PayTrace/CreditCard.php b/app/PaymentDrivers/PayTrace/CreditCard.php index f2a9a744aca7..3e00f657e881 100644 --- a/app/PaymentDrivers/PayTrace/CreditCard.php +++ b/app/PaymentDrivers/PayTrace/CreditCard.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\PayTrace; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; diff --git a/app/PaymentDrivers/Razorpay/Hosted.php b/app/PaymentDrivers/Razorpay/Hosted.php index 736a31c4e762..efb15074ca9d 100644 --- a/app/PaymentDrivers/Razorpay/Hosted.php +++ b/app/PaymentDrivers/Razorpay/Hosted.php @@ -16,7 +16,6 @@ namespace App\PaymentDrivers\Razorpay; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -163,12 +162,8 @@ class Hosted implements MethodInterface */ public function processUnsuccessfulPayment(\Exception $exception): void { - PaymentFailureMailer::dispatch( - $this->razorpay->client, - $exception->getMessage(), - $this->razorpay->client->company, - $this->razorpay->payment_hash->data->amount_with_fee - ); + + $this->razorpay->sendFailureMail($exception->getMessage()); SystemLogger::dispatch( $exception->getMessage(), @@ -180,5 +175,6 @@ class Hosted implements MethodInterface ); throw new PaymentFailed($exception->getMessage(), $exception->getCode()); + } } diff --git a/app/PaymentDrivers/Sample/CreditCard.php b/app/PaymentDrivers/Sample/CreditCard.php index 976ae402f2e8..201ca514b14f 100644 --- a/app/PaymentDrivers/Sample/CreditCard.php +++ b/app/PaymentDrivers/Sample/CreditCard.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Sample; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; diff --git a/app/PaymentDrivers/SquarePaymentDriver.php b/app/PaymentDrivers/SquarePaymentDriver.php index 43d16b30089e..5970cfd491bb 100644 --- a/app/PaymentDrivers/SquarePaymentDriver.php +++ b/app/PaymentDrivers/SquarePaymentDriver.php @@ -12,7 +12,6 @@ namespace App\PaymentDrivers; use App\Http\Requests\Payments\PaymentWebhookRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -167,12 +166,7 @@ class SquarePaymentDriver extends BaseDriver $this->unWindGatewayFees($payment_hash); - PaymentFailureMailer::dispatch( - $this->client, - $body->errors[0]->detail, - $this->client->company, - $amount - ); + $this->sendFailureMail($body->errors[0]->detail); $message = [ 'server_response' => $response, diff --git a/app/PaymentDrivers/Stripe/ACH.php b/app/PaymentDrivers/Stripe/ACH.php index e2e3c8941108..33fc448a1128 100644 --- a/app/PaymentDrivers/Stripe/ACH.php +++ b/app/PaymentDrivers/Stripe/ACH.php @@ -17,7 +17,6 @@ use App\Http\Requests\ClientPortal\PaymentMethod\VerifyPaymentMethodRequest; use App\Http\Requests\Request; use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerObject; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Mail\Gateways\ACHVerificationNotification; use App\Models\ClientGatewayToken; @@ -290,13 +289,7 @@ class ACH public function processUnsuccessfulPayment($state) { - - PaymentFailureMailer::dispatch( - $this->stripe->client, - $state['charge'], - $this->stripe->client->company, - $state['amount'] - ); + $this->stripe->sendFailureMail($state['charge']); $message = [ 'server_response' => $state['charge'], diff --git a/app/PaymentDrivers/Stripe/Alipay.php b/app/PaymentDrivers/Stripe/Alipay.php index 20f2c0e1ebf2..a3f5dd248bfc 100644 --- a/app/PaymentDrivers/Stripe/Alipay.php +++ b/app/PaymentDrivers/Stripe/Alipay.php @@ -14,7 +14,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -98,14 +97,7 @@ class Alipay { $server_response = $this->stripe->payment_hash->data; - PaymentFailureMailer::dispatch($this->stripe->client, $server_response->redirect_status, $this->stripe->client->company, $server_response->amount); - - 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()) - ); + $this->stripe->sendFailureMail($server_response->redirect_status); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/ApplePay.php b/app/PaymentDrivers/Stripe/ApplePay.php index fcb702570dcb..658900cd5d47 100644 --- a/app/PaymentDrivers/Stripe/ApplePay.php +++ b/app/PaymentDrivers/Stripe/ApplePay.php @@ -14,7 +14,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; diff --git a/app/PaymentDrivers/Stripe/Bancontact.php b/app/PaymentDrivers/Stripe/Bancontact.php index cfcab8361ab1..c269b286f7e0 100644 --- a/app/PaymentDrivers/Stripe/Bancontact.php +++ b/app/PaymentDrivers/Stripe/Bancontact.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -119,12 +118,7 @@ class Bancontact { $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()) - ); + $this->stripe->sendFailureMail($server_response); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/Connect/Verify.php b/app/PaymentDrivers/Stripe/Connect/Verify.php index daf443caec20..e957e6f5b248 100644 --- a/app/PaymentDrivers/Stripe/Connect/Verify.php +++ b/app/PaymentDrivers/Stripe/Connect/Verify.php @@ -18,7 +18,6 @@ use App\Http\Requests\ClientPortal\PaymentMethod\VerifyPaymentMethodRequest; use App\Http\Requests\Request; use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerObject; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Mail\Gateways\ACHVerificationNotification; use App\Models\ClientGatewayToken; diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index 41ee4ec4ae5c..f89e52688961 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -14,7 +14,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -160,14 +159,7 @@ class CreditCard public function processUnsuccessfulPayment($server_response) { - PaymentFailureMailer::dispatch($this->stripe->client, $server_response->cancellation_reason, $this->stripe->client->company, $server_response->amount); - - PaymentFailureMailer::dispatch( - $this->stripe->client, - $server_response, - $this->stripe->client->company, - $server_response->amount - ); + $this->stripe->sendFailureMail($server_response->cancellation_reason); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/EPS.php b/app/PaymentDrivers/Stripe/EPS.php index f9e294862b08..3d8f8323e7d6 100644 --- a/app/PaymentDrivers/Stripe/EPS.php +++ b/app/PaymentDrivers/Stripe/EPS.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -119,12 +118,7 @@ class EPS { $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()) - ); + $this->stripe->sendFailureMail($server_response); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/GIROPAY.php b/app/PaymentDrivers/Stripe/GIROPAY.php index 840c4a3c2c87..6894870af0a8 100644 --- a/app/PaymentDrivers/Stripe/GIROPAY.php +++ b/app/PaymentDrivers/Stripe/GIROPAY.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -119,12 +118,7 @@ class GIROPAY { $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()) - ); + $this->stripe->sendFailureMail($server_response); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/PRZELEWY24.php b/app/PaymentDrivers/Stripe/PRZELEWY24.php index 67dbf4516854..8ed4b76bc3a1 100644 --- a/app/PaymentDrivers/Stripe/PRZELEWY24.php +++ b/app/PaymentDrivers/Stripe/PRZELEWY24.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -119,12 +118,7 @@ class PRZELEWY24 { $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()) - ); + $this->stripe->sendFailureMail($server_response); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/SEPA.php b/app/PaymentDrivers/Stripe/SEPA.php index a20a61f290c5..251dbbb00bfa 100644 --- a/app/PaymentDrivers/Stripe/SEPA.php +++ b/app/PaymentDrivers/Stripe/SEPA.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -120,12 +119,7 @@ class SEPA { $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()) - ); + $this->stripe->sendFailureMail($server_response); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/SOFORT.php b/app/PaymentDrivers/Stripe/SOFORT.php index 43d25ab2634b..bf5c1e717d9a 100644 --- a/app/PaymentDrivers/Stripe/SOFORT.php +++ b/app/PaymentDrivers/Stripe/SOFORT.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -115,12 +114,7 @@ class SOFORT { $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()) - ); + $this->stripe->sendFailureMail($server_response); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/Stripe/iDeal.php b/app/PaymentDrivers/Stripe/iDeal.php index d19971c065c9..1a26e03c18bd 100644 --- a/app/PaymentDrivers/Stripe/iDeal.php +++ b/app/PaymentDrivers/Stripe/iDeal.php @@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Stripe; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -119,12 +118,7 @@ class iDeal { $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()) - ); + $this->stripe->sendFailureMail($server_response); $message = [ 'server_response' => $server_response, diff --git a/app/PaymentDrivers/WePay/ACH.php b/app/PaymentDrivers/WePay/ACH.php index 3a8a14b5ad8e..ae49f350418d 100644 --- a/app/PaymentDrivers/WePay/ACH.php +++ b/app/PaymentDrivers/WePay/ACH.php @@ -13,9 +13,11 @@ namespace App\PaymentDrivers\WePay; use App\Exceptions\PaymentFailed; +use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\Payment; +use App\Models\SystemLog; use App\PaymentDrivers\WePayPaymentDriver; use App\PaymentDrivers\WePay\WePayCommon; use App\Utils\Traits\MakesHash; @@ -68,6 +70,23 @@ class ACH ]); } catch(\Exception $e){ + + $this->wepay_payment_driver->sendFailureMail($e->getMessage()); + + $message = [ + 'server_response' => $e->getMessage(), + 'data' => $this->wepay_payment_driver->payment_hash->data, + ]; + + SystemLogger::dispatch( + $e->getMessage(), + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_WEPAY, + $this->wepay_payment_driver->client, + $this->wepay_payment_driver->client->company, + ); + throw new PaymentFailed($e->getMessage(), 400); } // display the response diff --git a/app/PaymentDrivers/WePay/CreditCard.php b/app/PaymentDrivers/WePay/CreditCard.php index 8209c71c3546..c2c74fc15140 100644 --- a/app/PaymentDrivers/WePay/CreditCard.php +++ b/app/PaymentDrivers/WePay/CreditCard.php @@ -14,7 +14,6 @@ namespace App\PaymentDrivers\WePay; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\Payment; @@ -153,24 +152,48 @@ use WePayCommon; $app_fee = (config('ninja.wepay.fee_cc_multiplier') * $this->wepay_payment_driver->payment_hash->data->amount_with_fee) + config('ninja.wepay.fee_fixed'); // charge the credit card - $response = $this->wepay_payment_driver->wepay->request('checkout/create', array( - 'unique_id' => Str::random(40), - 'account_id' => $this->wepay_payment_driver->company_gateway->getConfigField('accountId'), - 'amount' => $this->wepay_payment_driver->payment_hash->data->amount_with_fee, - 'currency' => $this->wepay_payment_driver->client->getCurrencyCode(), - 'short_description' => 'Goods and services', - 'type' => 'goods', - 'fee' => [ - 'fee_payer' => config('ninja.wepay.fee_payer'), - 'app_fee' => $app_fee, - ], - 'payment_method' => array( - 'type' => 'credit_card', - 'credit_card' => array( - 'id' => $credit_card_id + + try { + $response = $this->wepay_payment_driver->wepay->request('checkout/create', array( + 'unique_id' => Str::random(40), + 'account_id' => $this->wepay_payment_driver->company_gateway->getConfigField('accountId'), + 'amount' => $this->wepay_payment_driver->payment_hash->data->amount_with_fee, + 'currency' => $this->wepay_payment_driver->client->getCurrencyCode(), + 'short_description' => 'Goods and services', + 'type' => 'goods', + 'fee' => [ + 'fee_payer' => config('ninja.wepay.fee_payer'), + 'app_fee' => $app_fee, + ], + 'payment_method' => array( + 'type' => 'credit_card', + 'credit_card' => array( + 'id' => $credit_card_id + ) ) - ) - )); + )); + } + catch(\Exception $e){ + + $this->wepay_payment_driver->sendFailureMail($e->getMessage()); + + $message = [ + 'server_response' => $e->getMessage(), + 'data' => $this->wepay_payment_driver->payment_hash->data, + ]; + + SystemLogger::dispatch( + $e->getMessage(), + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_WEPAY, + $this->wepay_payment_driver->client, + $this->wepay_payment_driver->client->company, + ); + + throw new PaymentFailed($e->getMessage(), 500); + + } /* Merge all data and store in the payment hash*/ $state = [ diff --git a/app/PaymentDrivers/WePay/WePayCommon.php b/app/PaymentDrivers/WePay/WePayCommon.php index 2cf843b43828..8555a0c95772 100644 --- a/app/PaymentDrivers/WePay/WePayCommon.php +++ b/app/PaymentDrivers/WePay/WePayCommon.php @@ -12,7 +12,6 @@ namespace App\PaymentDrivers\WePay; use App\Exceptions\PaymentFailed; -use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\GatewayType; use App\Models\PaymentType; @@ -21,7 +20,6 @@ use App\Models\SystemLog; trait WePayCommon { - private function processSuccessfulPayment($response, $payment_status, $gateway_type, $return_payment = false) { @@ -56,14 +54,7 @@ trait WePayCommon private function processUnSuccessfulPayment($response, $payment_status) { - PaymentFailureMailer::dispatch($this->wepay_payment_driver->client, $response->state, $this->wepay_payment_driver->client->company, $response->amount); - - PaymentFailureMailer::dispatch( - $this->wepay_payment_driver->client, - $response, - $this->wepay_payment_driver->client->company, - $response->gross - ); + $this->wepay_payment_driver->sendFailureMail($response->state); $message = [ 'server_response' => $response, diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 98d003507ea4..ae84ea16462a 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -107,7 +107,7 @@ class AutoBillInvoice extends AbstractService /* Build payment hash */ $payment_hash = PaymentHash::create([ 'hash' => Str::random(64), - 'data' => ['invoices' => [['invoice_id' => $this->invoice->hashed_id, 'amount' => $amount]]], + 'data' => ['invoices' => [['invoice_id' => $this->invoice->hashed_id, 'amount' => $amount, 'invoice_number' => $this->invoice->number]]], 'fee_total' => $fee, 'fee_invoice_id' => $this->invoice->id, ]);