Skip to content

Commit

Permalink
Check Credential when store DB
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Sep 13, 2023
1 parent 5fadf0b commit fc4bbc0
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions Http/Controllers/StripeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Modules\Stripe\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Modules\Stripe\Entities\Mailbox;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Modules\Stripe\Entities\Mailbox;
use Stripe\StripeClient;

class StripeController extends Controller
{
Expand Down Expand Up @@ -49,9 +50,14 @@ public function index(Mailbox $mailbox)
public function update(Request $request, Mailbox $mailbox)
{
$this->validate($request, [
'stripe_secret_key'=>'required|max:250',
'stripe_secret_key' => 'required|max:250',
]);

if ($this->checkCredential($request->stripe_secret_key) !== true) {
\Session::flash('flash_error_floating', __('Credentials Mismatch.'));
return redirect()->back();
}

$requestData = [
'mailbox_id' => $mailbox->id,
'stripe_secret_key' => Crypt::encryptString($request->stripe_secret_key),
Expand All @@ -70,11 +76,11 @@ public function update(Request $request, Mailbox $mailbox)
return redirect()->route('stripe.settings', $mailbox->id);
}

/**
* Delete stripe key
* @param Request $request
* @return void
*/
/**
* Delete stripe key
* @param Request $request
* @return void
*/
public function destroy(Request $request, Mailbox $mailbox)
{
try {
Expand All @@ -86,4 +92,20 @@ public function destroy(Request $request, Mailbox $mailbox)

return redirect()->back();
}

private function checkCredential($credential)
{
try {
$stripeClient = new StripeClient($credential);
$response = $stripeClient->customers->all();

if($response) {
return true;
}

return false;
} catch (\Throwable $th) {
return $th->getMessage();
}
}
}

0 comments on commit fc4bbc0

Please sign in to comment.