diff --git a/app/Models/Account.php b/app/Models/Account.php index dc4ee52151..3ea45aaeab 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -420,9 +420,11 @@ class Account extends Eloquent public function paymentDriver($invitation = false, $gatewayType = false) { - $accountGateway = $this->getGatewayByType($gatewayType); + if ($accountGateway = $this->getGatewayByType($gatewayType)) { + return $accountGateway->paymentDriver($invitation, $gatewayType); + } - return $accountGateway->paymentDriver($invitation, $gatewayType); + return false; } public function gatewayIds() diff --git a/app/Models/AccountGateway.php b/app/Models/AccountGateway.php index fec6cc722c..e2690b9b6c 100644 --- a/app/Models/AccountGateway.php +++ b/app/Models/AccountGateway.php @@ -136,6 +136,7 @@ class AccountGateway extends EntityModel public function getWebhookUrl() { $account = $this->account ? $this->account : Account::find($this->account_id); + return \URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$account->account_key.'/'.$this->gateway_id.env('WEBHOOK_SUFFIX','')); } } diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 1fb11bd827..eded203571 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -26,7 +26,7 @@ class BasePaymentDriver protected $tokenResponse; protected $purchaseResponse; - protected $sourceReferenceParam; + protected $sourceReferenceParam = 'token'; protected $customerReferenceParam; protected $transactionReferenceParam; @@ -119,6 +119,7 @@ class BasePaymentDriver } $data = [ + 'details' => ! empty($input['details']) ? json_decode($input['details']) : false, 'accountGateway' => $this->accountGateway, 'acceptedCreditCardTypes' => $this->accountGateway->getCreditcardTypes(), 'gateway' => $gateway, diff --git a/app/Ninja/PaymentDrivers/WePayPaymentDriver.php b/app/Ninja/PaymentDrivers/WePayPaymentDriver.php index 71e97cc9f3..1f47227ec6 100644 --- a/app/Ninja/PaymentDrivers/WePayPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/WePayPaymentDriver.php @@ -1,5 +1,7 @@ id . 'payment_ref')) { + if ($transactionId = Session::get($this->invitation->id . 'payment_ref')) { $data['transaction_id'] = $transactionId; } @@ -69,25 +71,109 @@ class WePayPaymentDriver extends BasePaymentDriver return $data; } + public function createToken() + { + $wepay = Utils::setupWePay($this->accountGateway); + $token = intval($this->input['sourceToken']); + + if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) { + // Persist bank details + $this->tokenResponse = $wepay->request('/payment_bank/persist', array( + 'client_id' => WEPAY_CLIENT_ID, + 'client_secret' => WEPAY_CLIENT_SECRET, + 'payment_bank_id' => $token, + )); + } else { + // Authorize credit card + $tokenResponse = $wepay->request('credit_card/authorize', array( + 'client_id' => WEPAY_CLIENT_ID, + 'client_secret' => WEPAY_CLIENT_SECRET, + 'credit_card_id' => $token, + )); + + // Update the callback uri and get the card details + $tokenResponse = $wepay->request('credit_card/modify', array( + 'client_id' => WEPAY_CLIENT_ID, + 'client_secret' => WEPAY_CLIENT_SECRET, + 'credit_card_id' => $token, + 'auto_update' => WEPAY_AUTO_UPDATE, + 'callback_uri' => $this->accountGateway->getWebhookUrl(), + )); + + $this->tokenResponse = $wepay->request('credit_card', array( + 'client_id' => WEPAY_CLIENT_ID, + 'client_secret' => WEPAY_CLIENT_SECRET, + 'credit_card_id' => $token, + )); + } + + return parent::createToken(); + } + + /* + public function creatingCustomer($customer) + { + if ($gatewayResponse instanceof \Omnipay\WePay\Message\CustomCheckoutResponse) { + $wepay = \Utils::setupWePay($accountGateway); + $paymentMethodType = $gatewayResponse->getData()['payment_method']['type']; + + $gatewayResponse = $wepay->request($paymentMethodType, array( + 'client_id' => WEPAY_CLIENT_ID, + 'client_secret' => WEPAY_CLIENT_SECRET, + $paymentMethodType.'_id' => $gatewayResponse->getData()['payment_method'][$paymentMethodType]['id'], + )); + } + } + */ + + protected function creatingPaymentMethod($paymentMethod) + { + $source = $this->tokenResponse; + + if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) { + $paymentMethod->payment_type_id = PAYMENT_TYPE_ACH; + $paymentMethod->last4 = $source->account_last_four; + $paymentMethod->bank_name = $source->bank_name; + $paymentMethod->source_reference = $source->payment_bank_id; + + switch($source->state) { + case 'new': + case 'pending': + $paymentMethod->status = 'new'; + break; + case 'authorized': + $paymentMethod->status = 'verified'; + break; + } + } else { + $paymentMethod->last4 = $source->last_four; + $paymentMethod->payment_type_id = $this->parseCardType($source->credit_card_name); + $paymentMethod->expiration = $source->expiration_year . '-' . $source->expiration_month . '-01'; + $paymentMethod->source_reference = $source->credit_card_id; + } + + return $paymentMethod; + } + public function removePaymentMethod($paymentMethod) { $wepay = Utils::setupWePay($this->accountGateway); - $wepay->request('/credit_card/delete', [ + $response = $wepay->request('/credit_card/delete', [ 'client_id' => WEPAY_CLIENT_ID, 'client_secret' => WEPAY_CLIENT_SECRET, 'credit_card_id' => intval($paymentMethod->source_reference), ]); - if ($response->isSuccessful()) { + if ($response->state == 'deleted') { return parent::removePaymentMethod($paymentMethod); } else { - throw new Exception($response->getMessage()); + throw new Exception(); } } protected function refundDetails($payment, $amount) { - $data = parent::refundDetails($parent); + $data = parent::refundDetails($payment, $amount); $data['refund_reason'] = 'Refund issued by merchant.'; diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 1f0b594fae..675b944f95 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -43,143 +43,6 @@ class PaymentService extends BaseService return $this->paymentRepo; } - public function createToken($paymentType, $gateway, $details, $accountGateway, $client, $contactId, &$customerReference = null, &$paymentMethod = null) - { - if ($accountGateway->gateway_id == GATEWAY_BRAINTREE) { - } elseif ($accountGateway->gateway_id == GATEWAY_WEPAY) { - $wepay = Utils::setupWePay($accountGateway); - try { - if ($paymentType == PAYMENT_TYPE_WEPAY_ACH) { - // Persist bank details - $tokenResponse = $wepay->request('/payment_bank/persist', array( - 'client_id' => WEPAY_CLIENT_ID, - 'client_secret' => WEPAY_CLIENT_SECRET, - 'payment_bank_id' => intval($details['token']), - )); - } else { - // Authorize credit card - $wepay->request('credit_card/authorize', array( - 'client_id' => WEPAY_CLIENT_ID, - 'client_secret' => WEPAY_CLIENT_SECRET, - 'credit_card_id' => intval($details['token']), - )); - - // Update the callback uri and get the card details - $wepay->request('credit_card/modify', array( - 'client_id' => WEPAY_CLIENT_ID, - 'client_secret' => WEPAY_CLIENT_SECRET, - 'credit_card_id' => intval($details['token']), - 'auto_update' => WEPAY_AUTO_UPDATE, - 'callback_uri' => $accountGateway->getWebhookUrl(), - )); - $tokenResponse = $wepay->request('credit_card', array( - 'client_id' => WEPAY_CLIENT_ID, - 'client_secret' => WEPAY_CLIENT_SECRET, - 'credit_card_id' => intval($details['token']), - )); - } - - $customerReference = CUSTOMER_REFERENCE_LOCAL; - $sourceReference = $details['token']; - } catch (\WePayException $ex) { - $this->lastError = $ex->getMessage(); - return; - } - } else { - return null; - } - - if ($customerReference) { - $accountGatewayToken = AccountGatewayToken::where('client_id', '=', $client->id) - ->where('account_gateway_id', '=', $accountGateway->id)->first(); - - if (!$accountGatewayToken) { - $accountGatewayToken = new AccountGatewayToken(); - $accountGatewayToken->account_id = $client->account->id; - $accountGatewayToken->contact_id = $contactId; - $accountGatewayToken->account_gateway_id = $accountGateway->id; - $accountGatewayToken->client_id = $client->id; - } - - $accountGatewayToken->token = $customerReference; - $accountGatewayToken->save(); - - $paymentMethod = $this->convertPaymentMethodFromGatewayResponse($tokenResponse, $accountGateway, $accountGatewayToken, $contactId); - $paymentMethod->ip = \Request::ip(); - $paymentMethod->save(); - - } else { - $this->lastError = $tokenResponse->getMessage(); - } - - return $sourceReference; - } - - public function convertPaymentMethodFromWePay($source, $accountGatewayToken = null, $paymentMethod = null) { - // Creating a new one or updating an existing one - if (!$paymentMethod) { - $paymentMethod = $accountGatewayToken ? PaymentMethod::createNew($accountGatewayToken) : new PaymentMethod(); - } - - if ($source->payment_bank_id) { - $paymentMethod->payment_type_id = PAYMENT_TYPE_ACH; - $paymentMethod->last4 = $source->account_last_four; - $paymentMethod->bank_name = $source->bank_name; - $paymentMethod->source_reference = $source->payment_bank_id; - - switch($source->state) { - case 'new': - case 'pending': - $paymentMethod->status = 'new'; - break; - case 'authorized': - $paymentMethod->status = 'verified'; - break; - } - } else { - $paymentMethod->last4 = $source->last_four; - $paymentMethod->payment_type_id = $this->parseCardType($source->credit_card_name); - $paymentMethod->expiration = $source->expiration_year . '-' . $source->expiration_month . '-01'; - $paymentMethod->setRelation('payment_type', Cache::get('paymentTypes')->find($paymentMethod->payment_type_id)); - - $paymentMethod->source_reference = $source->credit_card_id; - } - - return $paymentMethod; - } - - public function convertPaymentMethodFromGatewayResponse($gatewayResponse, $accountGateway, $accountGatewayToken = null, $contactId = null, $existingPaymentMethod = null) { - if ($accountGateway->gateway_id == GATEWAY_WEPAY) { - if ($gatewayResponse instanceof \Omnipay\WePay\Message\CustomCheckoutResponse) { - $wepay = \Utils::setupWePay($accountGateway); - $paymentMethodType = $gatewayResponse->getData()['payment_method']['type']; - - $gatewayResponse = $wepay->request($paymentMethodType, array( - 'client_id' => WEPAY_CLIENT_ID, - 'client_secret' => WEPAY_CLIENT_SECRET, - $paymentMethodType.'_id' => $gatewayResponse->getData()['payment_method'][$paymentMethodType]['id'], - )); - - } - $paymentMethod = $this->convertPaymentMethodFromWePay($gatewayResponse, $accountGatewayToken, $existingPaymentMethod); - } - - if (!empty($paymentMethod) && $accountGatewayToken && $contactId) { - $paymentMethod->account_gateway_token_id = $accountGatewayToken->id; - $paymentMethod->account_id = $accountGatewayToken->account_id; - $paymentMethod->contact_id = $contactId; - $paymentMethod->save(); - - if (!$paymentMethod->account_gateway_token->default_payment_method_id) { - $paymentMethod->account_gateway_token->default_payment_method_id = $paymentMethod->id; - $paymentMethod->account_gateway_token->save(); - } - } - - return $paymentMethod; - } - - public function autoBillInvoice($invoice) { $client = $invoice->client; diff --git a/database/seeds/PaymentLibrariesSeeder.php b/database/seeds/PaymentLibrariesSeeder.php index ff8b6d5392..e7884e422d 100644 --- a/database/seeds/PaymentLibrariesSeeder.php +++ b/database/seeds/PaymentLibrariesSeeder.php @@ -74,7 +74,7 @@ class PaymentLibrariesSeeder extends Seeder ['name' => 'Secure Trading', 'provider' => 'SecureTrading'], ['name' => 'SecPay', 'provider' => 'SecPay'], ['name' => 'WeChat Express', 'provider' => 'WeChat_Express'], - ['name' => 'WePay', 'provider' => 'WePay'], + ['name' => 'WePay', 'provider' => 'WePay', 'is_offsite' => false], ['name' => 'Braintree', 'provider' => 'Braintree'], ]; @@ -82,7 +82,7 @@ class PaymentLibrariesSeeder extends Seeder $record = Gateway::where('name', '=', $gateway['name'])->first(); if ($record) { $record->provider = $gateway['provider']; - $record->is_offsite = isset($gateway['is_offsite']); + $record->is_offsite = isset($gateway['is_offsite']) ? boolval($gateway['is_offsite']) : false; $record->save(); } else { Gateway::create($gateway); diff --git a/resources/views/invoices/view.blade.php b/resources/views/invoices/view.blade.php index 72aff941cf..b42aeebb87 100644 --- a/resources/views/invoices/view.blade.php +++ b/resources/views/invoices/view.blade.php @@ -30,7 +30,6 @@ var paypalLink = $('.dropdown-menu a[href$="paypal"]'), paypalUrl = paypalLink.attr('href'), checkout; - console.log(paypalUrl); paypalLink.parent().attr('id', 'paypal-container'); braintree.setup("{{ $transactionToken }}", "custom", { onReady: function (integration) { @@ -65,7 +64,7 @@ - \ No newline at end of file diff --git a/resources/views/payments/wepay/bank_transfer.blade.php b/resources/views/payments/wepay/bank_transfer.blade.php index e273d00267..cfca52b94a 100644 --- a/resources/views/payments/wepay/bank_transfer.blade.php +++ b/resources/views/payments/wepay/bank_transfer.blade.php @@ -2,6 +2,8 @@ @section('payment_details') + {!! Former::vertical_open($url) !!} +