From d71017e2031f909005877dece5e0e648b73597d2 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 23 Jun 2016 20:17:02 +0300 Subject: [PATCH] Payment webhook bug fixes --- app/Models/Payment.php | 4 ++-- app/Models/PaymentMethod.php | 14 +++++++++++++- app/Ninja/PaymentDrivers/BasePaymentDriver.php | 13 +++++++++---- .../PaymentDrivers/BraintreePaymentDriver.php | 2 +- .../PaymentDrivers/PayPalExpressPaymentDriver.php | 2 +- app/Ninja/PaymentDrivers/StripePaymentDriver.php | 4 ++-- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/Models/Payment.php b/app/Models/Payment.php index a3f0943157..d3efe368cd 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -88,12 +88,12 @@ class Payment extends EntityModel public function isPending() { - return $this->payment_status_id = PAYMENT_STATUS_PENDING; + return $this->payment_status_id == PAYMENT_STATUS_PENDING; } public function isFailed() { - return $this->payment_status_id = PAYMENT_STATUS_FAILED; + return $this->payment_status_id == PAYMENT_STATUS_FAILED; } public function isCompleted() diff --git a/app/Models/PaymentMethod.php b/app/Models/PaymentMethod.php index 51b919946f..f35631aafd 100644 --- a/app/Models/PaymentMethod.php +++ b/app/Models/PaymentMethod.php @@ -149,9 +149,21 @@ class PaymentMethod extends EntityModel } } - public function requiresDelayedAutoBill(){ + public function requiresDelayedAutoBill() + { return $this->payment_type_id == PAYMENT_TYPE_ACH; } + + public function gatewayType() + { + if ($this->payment_type_id == PAYMENT_TYPE_ACH) { + return GATEWAY_TYPE_BANK_TRANSFER; + } elseif ($this->payment_type_id == PAYMENT_TYPE_PAYPAL) { + return GATEWAY_TYPE_PAYPAL; + } else { + return GATEWAY_TYPE_TOKEN; + } + } } PaymentMethod::deleting(function($paymentMethod) { diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 61e2a0d61c..34c70cdbd0 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -42,9 +42,14 @@ class BasePaymentDriver return $this->accountGateway->gateway_id == $gatewayId; } - protected function isGatewayType($gatewayType) + // optionally pass a paymentMethod to determine the type from the token + protected function isGatewayType($gatewayType, $paymentMethod = false) { - return $this->gatewayType === $gatewayType; + if ($paymentMethod) { + return $paymentMethod->gatewayType() == $gatewayType; + } else { + return $this->gatewayType === $gatewayType; + } } public function gatewayTypes() @@ -547,7 +552,7 @@ class BasePaymentDriver $payment->payment_date = date_create()->format('Y-m-d'); $payment->ip = Request::ip(); - $payment = $this->creatingPayment($payment); + $payment = $this->creatingPayment($payment, $paymentMethod); if ($paymentMethod) { $payment->last4 = $paymentMethod->last4; @@ -618,7 +623,7 @@ class BasePaymentDriver return $payment; } - protected function creatingPayment($payment) + protected function creatingPayment($payment, $paymentMethod) { return $payment; } diff --git a/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php b/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php index d714b8161c..dae0e1bfad 100644 --- a/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php @@ -70,7 +70,7 @@ class BraintreePaymentDriver extends BasePaymentDriver $data['device_data'] = $deviceData; } - if ($this->isGatewayType(GATEWAY_TYPE_PAYPAL)) { + if ($this->isGatewayType(GATEWAY_TYPE_PAYPAL, $paymentMethod)) { $data['ButtonSource'] = 'InvoiceNinja_SP'; } diff --git a/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php index 15ad415fde..240a7dc9dc 100644 --- a/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -20,7 +20,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver return $data; } - protected function creatingPayment($payment) + protected function creatingPayment($payment, $paymentMethod) { $payment->payer_id = $this->input['PayerID']; diff --git a/app/Ninja/PaymentDrivers/StripePaymentDriver.php b/app/Ninja/PaymentDrivers/StripePaymentDriver.php index 69efea4b4b..3e734cbdd0 100644 --- a/app/Ninja/PaymentDrivers/StripePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/StripePaymentDriver.php @@ -168,9 +168,9 @@ class StripePaymentDriver extends BasePaymentDriver return $paymentMethod; } - protected function creatingPayment($payment) + protected function creatingPayment($payment, $paymentMethod) { - if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) { + if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER, $paymentMethod)) { $payment->payment_status_id = $this->purchaseResponse['status'] == 'succeeded' ? PAYMENT_STATUS_COMPLETED : PAYMENT_STATUS_PENDING; }