From e065789315aeefc1237aabbf03f73277a62fdb27 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 15 Aug 2021 07:33:56 +1000 Subject: [PATCH 1/4] Stripe Payment Driver --- app/PaymentDrivers/StripePaymentDriver.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 82d45ce1490d..90eaa2f87150 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -86,7 +86,10 @@ class StripePaymentDriver extends BaseDriver { Stripe::setApiKey(config('ninja.ninja_stripe_key')); - $this->stripe_connect_auth = ["stripe_account" => $this->company_gateway->getConfigField('account_id')]; + if(strlen($this->company_gateway->getConfigField('account_id')) > 1) + $this->stripe_connect_auth = ["stripe_account" => $this->company_gateway->getConfigField('account_id')]; + + throw new \Exception('Stripe Connect has not been configured'); } else { From 9536605cb4222606798b17f65ba8b6f96f54e62a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 15 Aug 2021 07:51:37 +1000 Subject: [PATCH 2/4] Catch Stripe when account not configured --- app/PaymentDrivers/Stripe/ImportCustomers.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/PaymentDrivers/Stripe/ImportCustomers.php b/app/PaymentDrivers/Stripe/ImportCustomers.php index a8d7397124a2..bca4e1bfe80a 100644 --- a/app/PaymentDrivers/Stripe/ImportCustomers.php +++ b/app/PaymentDrivers/Stripe/ImportCustomers.php @@ -48,6 +48,9 @@ class ImportCustomers $this->update_payment_methods = new UpdatePaymentMethods($this->stripe); + if(strlen($this->stripe->company_gateway->getConfigField('account_id')) < 1) + throw new \Exception('Stripe Connect has not been configured'); + $customers = Customer::all([], $this->stripe->stripe_connect_auth); foreach($customers as $customer) From 674f7c8341f984dea833f4fe996f8456cac4728d Mon Sep 17 00:00:00 2001 From: = Date: Sun, 15 Aug 2021 12:24:50 +1000 Subject: [PATCH 3/4] Verify Stripe accounts --- app/PaymentDrivers/Stripe/Connect/Verify.php | 64 ++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 app/PaymentDrivers/Stripe/Connect/Verify.php diff --git a/app/PaymentDrivers/Stripe/Connect/Verify.php b/app/PaymentDrivers/Stripe/Connect/Verify.php new file mode 100644 index 000000000000..b6a4cf0121ec --- /dev/null +++ b/app/PaymentDrivers/Stripe/Connect/Verify.php @@ -0,0 +1,64 @@ +stripe = $stripe; + } + + public function verifyAccountLinked() + { + $this->stripe->init(); + + $map = $this->stripe->company_gateway->client_gateway_tokens->map(function ($cgt){ + + $customer = Customer::retrieve($cgt->gateway_customer_reference, $this->stripe->stripe_connect_auth); + + return [ + 'customer' => $cgt->gateway_customer_reference, + 'record' => $customer + ]; + + }); + + return $map; + } +} \ No newline at end of file From 383caa6439f8cf2325e0e69bbd7708c434679035 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 15 Aug 2021 15:13:20 +1000 Subject: [PATCH 4/4] Stripe Verify --- app/Http/Controllers/StripeController.php | 21 ++++++++++++++++++++ app/Jobs/Util/ImportStripeCustomers.php | 1 + app/PaymentDrivers/Stripe/Connect/Verify.php | 17 +++++++++++++--- app/PaymentDrivers/StripePaymentDriver.php | 6 ++++++ routes/api.php | 2 ++ 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/StripeController.php b/app/Http/Controllers/StripeController.php index 8f5e6a6902b2..a86cd0e5d386 100644 --- a/app/Http/Controllers/StripeController.php +++ b/app/Http/Controllers/StripeController.php @@ -14,10 +14,13 @@ namespace App\Http\Controllers; use App\Jobs\Util\ImportStripeCustomers; use App\Jobs\Util\StripeUpdatePaymentMethods; +use App\Models\CompanyGateway; class StripeController extends BaseController { + private $stripe_keys = ['d14dd26a47cecc30fdd65700bfb67b34', 'd14dd26a37cecc30fdd65700bfb55b23']; + public function update() { if(auth()->user()->isAdmin()) @@ -50,4 +53,22 @@ class StripeController extends BaseController return response()->json(['message' => 'Unauthorized'], 403); } + public function verify() + { + + if(auth()->user()->isAdmin()) + { + + $company_gateway = CompanyGateway::where('company_id', auth()->user()->company()->id) + ->where('is_deleted',0) + ->whereIn('gateway_key', $this->stripe_keys) + ->first(); + + return $company_gateway->driver(new Client)->verifyConnect(); + + } + + return response()->json(['message' => 'Unauthorized'], 403); + + } } \ No newline at end of file diff --git a/app/Jobs/Util/ImportStripeCustomers.php b/app/Jobs/Util/ImportStripeCustomers.php index 1aa6c06f775f..9a9b1791afeb 100644 --- a/app/Jobs/Util/ImportStripeCustomers.php +++ b/app/Jobs/Util/ImportStripeCustomers.php @@ -49,6 +49,7 @@ class ImportStripeCustomers implements ShouldQueue MultiDB::setDb($this->company->db); $cgs = CompanyGateway::where('company_id', $this->company->id) + ->where('is_deleted',0) ->whereIn('gateway_key', $this->stripe_keys) ->get(); diff --git a/app/PaymentDrivers/Stripe/Connect/Verify.php b/app/PaymentDrivers/Stripe/Connect/Verify.php index b6a4cf0121ec..8d9d994991e4 100644 --- a/app/PaymentDrivers/Stripe/Connect/Verify.php +++ b/app/PaymentDrivers/Stripe/Connect/Verify.php @@ -37,6 +37,7 @@ class Verify use MakesHash; /** @var StripePaymentDriver */ + public $stripe; public function __construct(StripePaymentDriver $stripe) @@ -44,11 +45,16 @@ class Verify $this->stripe = $stripe; } - public function verifyAccountLinked() + public function run() { $this->stripe->init(); - $map = $this->stripe->company_gateway->client_gateway_tokens->map(function ($cgt){ + if($this->stripe->stripe_connect && strlen($this->company_gateway->getConfigField('account_id')) < 1)) + return response()->json("Stripe Connect not authenticated", 400); + + $customers = Customer::all([], $this->stripe->stripe_connect_auth); + + $stripe_customers = $this->stripe->company_gateway->client_gateway_tokens->map(function ($cgt){ $customer = Customer::retrieve($cgt->gateway_customer_reference, $this->stripe->stripe_connect_auth); @@ -59,6 +65,11 @@ class Verify }); - return $map; + $data = [ + 'stripe_customer_count' => count($customers), + 'stripe_customers' => $stripe_customers, + ]; + + return response()->json($data, 200); } } \ No newline at end of file diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 90eaa2f87150..24af47f97a95 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -25,6 +25,7 @@ use App\Models\SystemLog; use App\PaymentDrivers\Stripe\ACH; use App\PaymentDrivers\Stripe\Alipay; use App\PaymentDrivers\Stripe\Charge; +use App\PaymentDrivers\Stripe\Connect\Verify; use App\PaymentDrivers\Stripe\CreditCard; use App\PaymentDrivers\Stripe\ImportCustomers; use App\PaymentDrivers\Stripe\SOFORT; @@ -536,4 +537,9 @@ class StripePaymentDriver extends BaseDriver //match clients based on the gateway_customer_reference column } + + public function verifyConnect() + { + return (new Verify($this))->run(); + } } diff --git a/routes/api.php b/routes/api.php index 4f51b965052f..ceeb78fadf6e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -190,6 +190,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('stripe/update_payment_methods', 'StripeController@update')->middleware('password_protected')->name('stripe.update'); Route::post('stripe/import_customers', 'StripeController@import')->middleware('password_protected')->name('stripe.import'); + Route::post('stripe/verify', 'StripeController@verify')->middleware('password_protected')->name('stripe.verify'); + Route::resource('subscriptions', 'SubscriptionController'); Route::post('subscriptions/bulk', 'SubscriptionController@bulk')->name('subscriptions.bulk');