From 12d8da942ecb162975ce95e1f4111b969a65bc63 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 17 May 2021 18:56:14 +1000 Subject: [PATCH] Import stripe customers and update their payment methods --- app/Http/Controllers/StripeController.php | 17 +++++ app/Jobs/Util/ImportStripeCustomers.php | 68 +++++++++++++++++++ app/PaymentDrivers/Stripe/ImportCustomers.php | 3 + app/PaymentDrivers/StripePaymentDriver.php | 4 +- routes/api.php | 1 + 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 app/Jobs/Util/ImportStripeCustomers.php diff --git a/app/Http/Controllers/StripeController.php b/app/Http/Controllers/StripeController.php index 2a17496b4af9..d1d3d3f54715 100644 --- a/app/Http/Controllers/StripeController.php +++ b/app/Http/Controllers/StripeController.php @@ -12,6 +12,7 @@ namespace App\Http\Controllers; +use App\Jobs\Util\ImportStripeCustomers; use App\Jobs\Util\StripeUpdatePaymentMethods; class StripeController extends BaseController @@ -29,6 +30,22 @@ class StripeController extends BaseController } + return response()->json(['message' => 'Unauthorized'], 403); + } + + public function import() + { + + if(auth()->user()->isAdmin()) + { + + ImportStripeCustomers::dispatch(auth()->user()->getCompany()); + + return response()->json(['message' => 'Processing'], 403); + + } + + return response()->json(['message' => 'Unauthorized'], 403); } diff --git a/app/Jobs/Util/ImportStripeCustomers.php b/app/Jobs/Util/ImportStripeCustomers.php new file mode 100644 index 000000000000..ed1592955990 --- /dev/null +++ b/app/Jobs/Util/ImportStripeCustomers.php @@ -0,0 +1,68 @@ +company = $company; + } + + /** + * Execute the job. + * + * @return bool + */ + public function handle() + { + + MultiDB::setDb($this->company->db); + + $cgs = CompanyGateway::where('company_id', $this->company->id) + ->whereIn('gateway_key', $this->stripe_keys) + ->get(); + + $cgs->each(function ($company_gateway){ + + $company_gateway->driver(new Client)->importCustomers(); + + }); + + } + + public function failed($exception) + { + nlog("Stripe import customer methods exception"); + nlog($exception->getMessage()); + } +} diff --git a/app/PaymentDrivers/Stripe/ImportCustomers.php b/app/PaymentDrivers/Stripe/ImportCustomers.php index 48e6fac72ea4..720b4a5cc65d 100644 --- a/app/PaymentDrivers/Stripe/ImportCustomers.php +++ b/app/PaymentDrivers/Stripe/ImportCustomers.php @@ -48,6 +48,9 @@ class ImportCustomers $this->addCustomer($customer); } + /* Now call the update payment methods handler*/ + $this->stripe->updateAllPaymentMethods(); + } private function addCustomer(Customer $customer) diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 3f420dd76c8d..bfbd5df65618 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -25,6 +25,7 @@ use App\PaymentDrivers\Stripe\ACH; use App\PaymentDrivers\Stripe\Alipay; use App\PaymentDrivers\Stripe\Charge; use App\PaymentDrivers\Stripe\CreditCard; +use App\PaymentDrivers\Stripe\ImportCustomers; use App\PaymentDrivers\Stripe\SOFORT; use App\PaymentDrivers\Stripe\UpdatePaymentMethods; use App\PaymentDrivers\Stripe\Utilities; @@ -513,9 +514,10 @@ class StripePaymentDriver extends BaseDriver * Phone * Email */ - public function importAndUpdateCustomers() + public function importCustomers() { + return (new ImportCustomers($this))->run(); //match clients based on the gateway_customer_reference column } diff --git a/routes/api.php b/routes/api.php index 2979829ead17..ea1477edd5a8 100644 --- a/routes/api.php +++ b/routes/api.php @@ -184,6 +184,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a // Route::delete('hooks/{subscription_id}', 'SubscriptionController@unsubscribe')->name('hooks.unsubscribe'); 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::resource('subscriptions', 'SubscriptionController'); Route::post('subscriptions/bulk', 'SubscriptionController@bulk')->name('subscriptions.bulk');