How the flow of this context should be? ( Where to put the code to process the payment depending on the selected payment method?) How the flow of this context should be? ( Where to put the code to process the payment depending on the selected payment method?) laravel laravel

How the flow of this context should be? ( Where to put the code to process the payment depending on the selected payment method?)


As in your doubts, to better structure your code and separate logic (DRY concept), you should do few things:

  • Create PaymentsController with 3 functions (for the start, you will definitely add more here)

    1. for processing your references
    2. for processing credit cards
    3. for storing payment methods (store payment method in session, until user completes step3)

    You are totally free, on how you name these functions, but I would suggest something like processViaReferences, processViaCreditCard and storePaymentMethod.

    It will be much easier to read and validate, if you have them separated.

    Create corresponding post routes for them and reference appropriately, in cour code.

  • Create part views containing your forms only.

    Then include, these part views via @include to have it separated on view level. For your references in javascript/jQuery, remember to keep correct ids of them. You can extract javascript, to other view part views or separate js files also, if you have not done it already.

    Your view/form code should go to these part views...


There are no clear instructions, you need to decide for yourself how to organize your code.

If you can not choose exactly where to place the PHP code responsible for payment, then I would place processing of both payments in PaymentsController.php

So it is obvious that after choosing the method of payment you need to form something like an order in the database.

  1. In case of references they will have to be tied to this order.
  2. In case of using credit cards, payment will be tied to this order.