Should processing logic of STRIPE be in the Laravel controller or in the validator?
I am a newcomer to laravel. I have a controller ProductController like this
public function buy(Request $request, User $user) {
StripeStripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$token = $_POST['stripeToken'];
$charge = StripeCharge::create([
'amount' => 100,
'currency' => 'aud',
'description' => 'Example charge',
'source' => $token,
]);
if ($charge->status === "succeeded")
//-- Processing... --//
I would like to ask the more appropriate design style, should I put the part of STTRIE in other places, such as the validator.
If yes, it is to make a rule and a request than verify it in the validator ?
Can someone tell me how to use the rule in the request?
laravel validation request rules
add a comment |
I am a newcomer to laravel. I have a controller ProductController like this
public function buy(Request $request, User $user) {
StripeStripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$token = $_POST['stripeToken'];
$charge = StripeCharge::create([
'amount' => 100,
'currency' => 'aud',
'description' => 'Example charge',
'source' => $token,
]);
if ($charge->status === "succeeded")
//-- Processing... --//
I would like to ask the more appropriate design style, should I put the part of STTRIE in other places, such as the validator.
If yes, it is to make a rule and a request than verify it in the validator ?
Can someone tell me how to use the rule in the request?
laravel validation request rules
1
Neither place is likely the best place for it. Business logic should live in models or service classes.
– Devon
Nov 13 '18 at 15:09
are you validating the stripe$chargeor your$request?
– adam
Nov 13 '18 at 19:50
add a comment |
I am a newcomer to laravel. I have a controller ProductController like this
public function buy(Request $request, User $user) {
StripeStripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$token = $_POST['stripeToken'];
$charge = StripeCharge::create([
'amount' => 100,
'currency' => 'aud',
'description' => 'Example charge',
'source' => $token,
]);
if ($charge->status === "succeeded")
//-- Processing... --//
I would like to ask the more appropriate design style, should I put the part of STTRIE in other places, such as the validator.
If yes, it is to make a rule and a request than verify it in the validator ?
Can someone tell me how to use the rule in the request?
laravel validation request rules
I am a newcomer to laravel. I have a controller ProductController like this
public function buy(Request $request, User $user) {
StripeStripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$token = $_POST['stripeToken'];
$charge = StripeCharge::create([
'amount' => 100,
'currency' => 'aud',
'description' => 'Example charge',
'source' => $token,
]);
if ($charge->status === "succeeded")
//-- Processing... --//
I would like to ask the more appropriate design style, should I put the part of STTRIE in other places, such as the validator.
If yes, it is to make a rule and a request than verify it in the validator ?
Can someone tell me how to use the rule in the request?
laravel validation request rules
laravel validation request rules
asked Nov 13 '18 at 14:58
Jhih Wei JhanJhih Wei Jhan
53
53
1
Neither place is likely the best place for it. Business logic should live in models or service classes.
– Devon
Nov 13 '18 at 15:09
are you validating the stripe$chargeor your$request?
– adam
Nov 13 '18 at 19:50
add a comment |
1
Neither place is likely the best place for it. Business logic should live in models or service classes.
– Devon
Nov 13 '18 at 15:09
are you validating the stripe$chargeor your$request?
– adam
Nov 13 '18 at 19:50
1
1
Neither place is likely the best place for it. Business logic should live in models or service classes.
– Devon
Nov 13 '18 at 15:09
Neither place is likely the best place for it. Business logic should live in models or service classes.
– Devon
Nov 13 '18 at 15:09
are you validating the stripe
$charge or your $request?– adam
Nov 13 '18 at 19:50
are you validating the stripe
$charge or your $request?– adam
Nov 13 '18 at 19:50
add a comment |
2 Answers
2
active
oldest
votes
Creating a Stripe charge is not request validation. It's an API call to Stripe. So, it should definitely not stay in the validator.
You can have this logic in a controller for small apps, but for medium/large scale apps with abstraction (e.g. if you want to have the option later to change the payment provider from Stripe to say Braintree), it should be in a service class.
Also, never use $_POST directly. Use $request->input instead. As a thumb rule, if you have 2 ways to do something in code, always use the way that implements higher level libraries (libraries > then framework > then core PHP).
add a comment |
To write the validation using Laravel 5.5+
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
For Laravel 5.0 - 5.4:
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
The typical responsibilities of a controller in my opinion:
- Take in a request
- Return a response
I think it is probably fine to have one conditional or validation check.
Thank you @adam. I prefer to separate the logic into different places so that the controller doesn't look too messy. I originally thought that I would customize a STRIPE rule and then use it in laravel's request.
– Jhih Wei Jhan
Nov 14 '18 at 2:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283782%2fshould-processing-logic-of-stripe-be-in-the-laravel-controller-or-in-the-validat%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Creating a Stripe charge is not request validation. It's an API call to Stripe. So, it should definitely not stay in the validator.
You can have this logic in a controller for small apps, but for medium/large scale apps with abstraction (e.g. if you want to have the option later to change the payment provider from Stripe to say Braintree), it should be in a service class.
Also, never use $_POST directly. Use $request->input instead. As a thumb rule, if you have 2 ways to do something in code, always use the way that implements higher level libraries (libraries > then framework > then core PHP).
add a comment |
Creating a Stripe charge is not request validation. It's an API call to Stripe. So, it should definitely not stay in the validator.
You can have this logic in a controller for small apps, but for medium/large scale apps with abstraction (e.g. if you want to have the option later to change the payment provider from Stripe to say Braintree), it should be in a service class.
Also, never use $_POST directly. Use $request->input instead. As a thumb rule, if you have 2 ways to do something in code, always use the way that implements higher level libraries (libraries > then framework > then core PHP).
add a comment |
Creating a Stripe charge is not request validation. It's an API call to Stripe. So, it should definitely not stay in the validator.
You can have this logic in a controller for small apps, but for medium/large scale apps with abstraction (e.g. if you want to have the option later to change the payment provider from Stripe to say Braintree), it should be in a service class.
Also, never use $_POST directly. Use $request->input instead. As a thumb rule, if you have 2 ways to do something in code, always use the way that implements higher level libraries (libraries > then framework > then core PHP).
Creating a Stripe charge is not request validation. It's an API call to Stripe. So, it should definitely not stay in the validator.
You can have this logic in a controller for small apps, but for medium/large scale apps with abstraction (e.g. if you want to have the option later to change the payment provider from Stripe to say Braintree), it should be in a service class.
Also, never use $_POST directly. Use $request->input instead. As a thumb rule, if you have 2 ways to do something in code, always use the way that implements higher level libraries (libraries > then framework > then core PHP).
answered Nov 13 '18 at 19:10
ParasParas
5,7951035
5,7951035
add a comment |
add a comment |
To write the validation using Laravel 5.5+
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
For Laravel 5.0 - 5.4:
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
The typical responsibilities of a controller in my opinion:
- Take in a request
- Return a response
I think it is probably fine to have one conditional or validation check.
Thank you @adam. I prefer to separate the logic into different places so that the controller doesn't look too messy. I originally thought that I would customize a STRIPE rule and then use it in laravel's request.
– Jhih Wei Jhan
Nov 14 '18 at 2:04
add a comment |
To write the validation using Laravel 5.5+
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
For Laravel 5.0 - 5.4:
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
The typical responsibilities of a controller in my opinion:
- Take in a request
- Return a response
I think it is probably fine to have one conditional or validation check.
Thank you @adam. I prefer to separate the logic into different places so that the controller doesn't look too messy. I originally thought that I would customize a STRIPE rule and then use it in laravel's request.
– Jhih Wei Jhan
Nov 14 '18 at 2:04
add a comment |
To write the validation using Laravel 5.5+
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
For Laravel 5.0 - 5.4:
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
The typical responsibilities of a controller in my opinion:
- Take in a request
- Return a response
I think it is probably fine to have one conditional or validation check.
To write the validation using Laravel 5.5+
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
For Laravel 5.0 - 5.4:
public function buy(Request $request, User $user)
// first define your rules
$rules = [
'amount' => 'required
The typical responsibilities of a controller in my opinion:
- Take in a request
- Return a response
I think it is probably fine to have one conditional or validation check.
answered Nov 13 '18 at 19:53
adamadam
917811
917811
Thank you @adam. I prefer to separate the logic into different places so that the controller doesn't look too messy. I originally thought that I would customize a STRIPE rule and then use it in laravel's request.
– Jhih Wei Jhan
Nov 14 '18 at 2:04
add a comment |
Thank you @adam. I prefer to separate the logic into different places so that the controller doesn't look too messy. I originally thought that I would customize a STRIPE rule and then use it in laravel's request.
– Jhih Wei Jhan
Nov 14 '18 at 2:04
Thank you @adam. I prefer to separate the logic into different places so that the controller doesn't look too messy. I originally thought that I would customize a STRIPE rule and then use it in laravel's request.
– Jhih Wei Jhan
Nov 14 '18 at 2:04
Thank you @adam. I prefer to separate the logic into different places so that the controller doesn't look too messy. I originally thought that I would customize a STRIPE rule and then use it in laravel's request.
– Jhih Wei Jhan
Nov 14 '18 at 2:04
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283782%2fshould-processing-logic-of-stripe-be-in-the-laravel-controller-or-in-the-validat%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Neither place is likely the best place for it. Business logic should live in models or service classes.
– Devon
Nov 13 '18 at 15:09
are you validating the stripe
$chargeor your$request?– adam
Nov 13 '18 at 19:50