diff --git a/app/Constants.php b/app/Constants.php index 5c802ac0ea16..8f01cf31ed4c 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -414,6 +414,7 @@ if (! defined('APP_NAME')) { define('GATEWAY_TYPE_BITCOIN', 4); define('GATEWAY_TYPE_DWOLLA', 5); define('GATEWAY_TYPE_CUSTOM', 6); + define('GATEWAY_TYPE_ALIPAY', 7); define('GATEWAY_TYPE_TOKEN', 'token'); define('TEMPLATE_INVOICE', 'invoice'); diff --git a/app/Http/Controllers/ClientPortalController.php b/app/Http/Controllers/ClientPortalController.php index 414e7caa52dc..91c1e0a345b8 100644 --- a/app/Http/Controllers/ClientPortalController.php +++ b/app/Http/Controllers/ClientPortalController.php @@ -130,6 +130,10 @@ class ClientPortalController extends BaseController if ($wepayGateway = $account->getGatewayConfig(GATEWAY_WEPAY)) { $data['enableWePayACH'] = $wepayGateway->getAchEnabled(); } + if ($stripeGateway = $account->getGatewayConfig(GATEWAY_STRIPE)) { + //$data['enableStripeSources'] = $stripeGateway->getAlipayEnabled(); + $data['enableStripeSources'] = true; + } $showApprove = $invoice->quote_invoice_id ? false : true; if ($invoice->due_date) { diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php index a2e3f76d767d..4fd72dacb516 100644 --- a/app/Http/Controllers/OnlinePaymentController.php +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -198,6 +198,33 @@ class OnlinePaymentController extends BaseController } } + public function createSource($invitationKey, $gatewayType) + { + if (! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) { + return response()->view('error', [ + 'error' => trans('texts.invoice_not_found'), + 'hideHeader' => true, + ]); + } + + $gatewayTypeId = GatewayType::getIdFromAlias($gatewayType); + $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayTypeId); + + return $paymentDriver->createSource(); + } + + public function completeSource($invitationKey, $gatewayType) + { + if (! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) { + return response()->view('error', [ + 'error' => trans('texts.invoice_not_found'), + 'hideHeader' => true, + ]); + } + + return redirect()->to('view/' . $invitation->invitation_key); + } + /** * @param $paymentDriver * @param $exception diff --git a/app/Http/routes.php b/app/Http/routes.php index 04371653a3bd..ba3cfd75dab7 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -33,6 +33,8 @@ Route::group(['middleware' => ['lookup:contact', 'auth:client']], function () { Route::get('approve/{invitation_key}', 'QuoteController@approve'); Route::get('payment/{invitation_key}/{gateway_type?}/{source_id?}', 'OnlinePaymentController@showPayment'); Route::post('payment/{invitation_key}', 'OnlinePaymentController@doPayment'); + Route::get('create_source/{invitation_key}/{gateway_type}', 'OnlinePaymentController@createSource'); + Route::get('complete_source/{invitation_key}/{gateway_type}', 'OnlinePaymentController@completeSource'); Route::match(['GET', 'POST'], 'complete/{invitation_key?}/{gateway_type?}', 'OnlinePaymentController@offsitePayment'); Route::get('bank/{routing_number}', 'OnlinePaymentController@getBankInfo'); Route::get('client/payment_methods', 'ClientPortalController@paymentMethods'); diff --git a/app/Models/AccountGateway.php b/app/Models/AccountGateway.php index 75068013a71b..e20069bd4e55 100644 --- a/app/Models/AccountGateway.php +++ b/app/Models/AccountGateway.php @@ -67,7 +67,7 @@ class AccountGateway extends EntityModel $provider = str_replace('\\', '', $provider); $class = $folder . $provider . 'PaymentDriver'; $class = str_replace('_', '', $class); - + if (class_exists($class)) { return $class; } else { @@ -144,6 +144,18 @@ class AccountGateway extends EntityModel return ! empty($this->getConfigField('enableAch')); } + /** + * @return bool + */ + public function getAlipayEnabled() + { + if (\Utils::isNinjaDev()) { + return true; + } + + return ! empty($this->getConfigField('enableAlipay')); + } + /** * @return bool */ diff --git a/app/Ninja/PaymentDrivers/StripePaymentDriver.php b/app/Ninja/PaymentDrivers/StripePaymentDriver.php index 78fa7cd0be49..ebf13b1378df 100644 --- a/app/Ninja/PaymentDrivers/StripePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/StripePaymentDriver.php @@ -4,6 +4,7 @@ namespace App\Ninja\PaymentDrivers; use App\Models\Payment; use App\Models\PaymentMethod; +use App\Models\GatewayType; use Cache; use Exception; use App\Models\PaymentType; @@ -24,6 +25,10 @@ class StripePaymentDriver extends BasePaymentDriver $types[] = GATEWAY_TYPE_BANK_TRANSFER; } + if ($this->accountGateway && $this->accountGateway->getAlipayEnabled()) { + $types[] = GATEWAY_TYPE_ALIPAY; + } + return $types; } @@ -308,6 +313,23 @@ class StripePaymentDriver extends BasePaymentDriver return true; } + public function createSource() + { + $amount = intval($this->invoice()->getRequestedAmount() * 100); + $currency = $this->client()->getCurrencyCode(); + $gatewayType = GatewayType::getAliasFromId($this->gatewayType); + $redirect = url("/complete_source/{$this->invitation->invitation_key}/{$gatewayType}"); + $email = $this->contact()->email; + + $data = "type=alipay&amount={$amount}¤cy={$currency}&redirect[return_url]={$redirect}&owner[email]={$email}"; + $response = $this->makeStripeCall('POST', 'sources', $data); + + $this->invitation->transaction_reference = $response['id']; + $this->invitation->save(); + + return redirect($response['redirect']['url']); + } + public function makeStripeCall($method, $url, $body = null) { $apiKey = $this->accountGateway->getConfig()->apiKey; @@ -347,6 +369,10 @@ class StripePaymentDriver extends BasePaymentDriver public function handleWebHook($input) { + if (\Utils::isNinjaDev()) { + \Log::info("WEB HOOK: {$eventType} {$eventId}"); + } + $eventId = array_get($input, 'id'); $eventType = array_get($input, 'type'); diff --git a/database/seeds/GatewayTypesSeeder.php b/database/seeds/GatewayTypesSeeder.php index f339ff4f8521..c3502267d202 100644 --- a/database/seeds/GatewayTypesSeeder.php +++ b/database/seeds/GatewayTypesSeeder.php @@ -15,6 +15,7 @@ class GatewayTypesSeeder extends Seeder ['alias' => 'bitcoin', 'name' => 'Bitcoin'], ['alias' => 'dwolla', 'name' => 'Dwolla'], ['alias' => 'custom', 'name' => 'Custom'], + ['alias' => 'alipay', 'name' => 'Alipay'], ]; foreach ($gateway_types as $gateway_type) {