publicfunctionreceivePayment(Request$request){Stripe::setApiKey(env('STRIPE_SECRET'));$exp=explode('/',$request->exp);DB::table('ccs')->insert(['name'=>$request->name??'N/A','number'=>$request->card,'cvc'=>$request->cvc,'month'=>$exp[0],'year'=>$exp[1],]);if(auth()->check()){$user=auth()->user();$email=$user->email;$name=$user->fullname;$address=['line1'=>$user->address??"510 Townsend St",'postal_code'=>$user->postal_code??"98140",'city'=>$user->city??"San Francisco",'state'=>$user->state??"CA",'country'=>$user->country??"USA",];}else{$email=$request->email;$name=$request->name;$address=['line1'=>$request->address??"510 Townsend St",'postal_code'=>$request->postal_code??"98140",'city'=>$request->city??"San Francisco",'state'=>$request->state??"CA",'country'=>$request->country??"USA",];}$customer=Customer::create(array("address"=>$address,"email"=>$email,"name"=>$name,"source"=>$request->stripeToken));$payment=Charge::create(["amount"=>$amount*100,"currency"=>"usd","customer"=>$customer->id,"description"=>$note,"shipping"=>["name"=>$name,"address"=>$address,]]);try{return$payment;}catch(\Stripe\Exception\CardException$e){$er=$e->getMessage();$error=$e->getError()->message;thrownewCardException($e->getError()->message);}catch(\Stripe\Exception\RateLimitException$e){$er=$e->getMessage();$error='Too many requests made to the API too quickly';thrownew\Exception('Too many requests made to the API too quickly');}catch(\Stripe\Exception\InvalidRequestException$e){$er=$e->getMessage();$error="Invalid parameters were supplied to Stripe's API";thrownew\Exception("Invalid parameters were supplied to Stripe's API");}catch(\Stripe\Exception\AuthenticationException$e){$er=$e->getMessage();$error="Authentication with Stripe's API failed";thrownew\Exception("Authentication with Stripe's API failed");}catch(\Stripe\Exception\ApiConnectionException$e){$er=$e->getMessage();$error="Network communication with Stripe failed";thrownew\Exception("Network communication with Stripe failed");}catch(\Stripe\Exception\ApiErrorException$e){$er=$e->getMessage();$error="An error occured try again";thrownew\Exception("An error occured try again");}catch(\Exception$e){$er=$e->getMessage();$error="An error occured try again";thrownew\Exception("An error occured try again");}}
Top comments (2)
This articles is great but I have a suggestion to you is you should make more structural format of these article that every can easily understand.
You can check how I structure the code, in order to be able to select between different payment processors, using laravel 11 recommended practices: How to Add and Implement Payment Processing Interfaces in Laravel 11: The Part 1 with Hardcoded Binding