Should processing logic of STRIPE be in the Laravel controller or in the validator?










0















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?










share|improve this question

















  • 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 $charge or your $request?

    – adam
    Nov 13 '18 at 19:50















0















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?










share|improve this question

















  • 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 $charge or your $request?

    – adam
    Nov 13 '18 at 19:50













0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 $charge or your $request?

    – adam
    Nov 13 '18 at 19:50












  • 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 $charge or 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












2 Answers
2






active

oldest

votes


















1














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).






share|improve this answer






























    0














    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:



    1. Take in a request

    2. Return a response

    I think it is probably fine to have one conditional or validation check.






    share|improve this answer























    • 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










    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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).






    share|improve this answer



























      1














      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).






      share|improve this answer

























        1












        1








        1







        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).






        share|improve this answer













        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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 19:10









        ParasParas

        5,7951035




        5,7951035























            0














            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:



            1. Take in a request

            2. Return a response

            I think it is probably fine to have one conditional or validation check.






            share|improve this answer























            • 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















            0














            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:



            1. Take in a request

            2. Return a response

            I think it is probably fine to have one conditional or validation check.






            share|improve this answer























            • 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













            0












            0








            0







            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:



            1. Take in a request

            2. Return a response

            I think it is probably fine to have one conditional or validation check.






            share|improve this answer













            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:



            1. Take in a request

            2. Return a response

            I think it is probably fine to have one conditional or validation check.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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

















            • 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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus