1

I am developing a web app and I observe REST API standards. I am searching for REST API best practice for subscription and payments.

When a new user subscribe for "pro plan", user should pay money for plan and it is a transaction.

Should I set POST: users/{id}/subscriptions and SubscriptionsController@store when new user subscribes?

And because subscription is a transaction and 2 separated request (before/after bank), all of subscribe codes should be in SubscriptionController@store?

For upgrade, cancel or updating a plan should I set PUT: users/{id}/subscriptions/{id} and SubscriptionController@update or other endpoint?

  • Possible duplicate of [REST API - PUT vs PATCH with real life examples](http://stackoverflow.com/questions/28459418/rest-api-put-vs-patch-with-real-life-examples). All of your questions are answered in this comment and covered thoroughly. Please have a read through. – Ohgodwhy Jan 19 '17 at 10:41
  • @Ohgodwhy I read many contents about REST API. I couldn't find my answer. And in these answers also there isn't certain and best practice about my question. If you have answer please answer. – نرم افزار حضور و غیاب Jan 19 '17 at 10:56
  • 1
    What are you mean by "because subscription is a transaction and 2 separated request (before/after bank)". Its not clear enough. – Gayan Jan 23 '17 at 15:24
  • 1
    It's really unclear what you're asking here. You can set up your own routes however you like. Is there a specific programming issue/problem you have...? – samiles Jan 24 '17 at 08:49
  • @gayan Subscribing for a specify plan need 2 request: 1- before payment for credit card info and ... . 2- after payment for validating payment. If this two request were successful, user will be subscribe for plan. – نرم افزار حضور و غیاب Jan 24 '17 at 20:57

3 Answers3

3

Generally you would not pass the user id in the route, unless there was some sort of authentication in the controller. eg. Admin is updating the user. Instead use the Auth::user() object in the controller.

With regard to your question, there are many options and it is entirely up to you, but a possible way of doing it would be to use a resource route\controller for this.

Route::resource('user/subscription', 'User\SubscriptionController');

Then the controller would look something like this:

<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SubscriptionController extends Controller
{

    public function index()
    {
        // get user
        $user = Auth::user();

        // list all user subscriptions
    }

    public function store(Request $request)
    {

        // get user
        $user = Auth::user();

        if(empty($user)) {
            // create user
        }

        // create and process subscription for the user 
        // possibly using a plan id in the $request
    }

    public function show($id)
    {
        // get user
        $user = Auth::user();

        // return user subscription details for $id
    }

    public function update(Request $request, $id)
    {
        // get user
        $user = Auth::user();

        // update or change user subscription
        // possibly using a plan id in the $request

    }


    public function destroy($id)
    {
        // get user
        $user = Auth::user();

        // cancel user subscription with $id
    }
}

And your routes would be like this:

GET user/subscription list all user subscriptions index()

POST user/subscription create a user subscription store(Request $request)

GET user/subscription/{subscription_id} show a user subscription show($id)

PUT/PATCH user/subscription/{subscription_id} update a user subscription update($id)

DELETE user/subscription/{subscription_id} cancel a user subscription destroy($id)

CUGreen
  • 3,066
  • 2
  • 13
  • 22
1

I am trying to understand what you asking for but it's a little bit blur for me so if I got right , you are trying to figure what is the best practice for API End point naming , it's really depend on the functions you will provide and how you will expose the documentation.

But from my point I don't see a reason to chain user ID and subscriber ID in the URL , I recommend something like this and you can pass all the information you want in the body

$router->post('settings/user/plan', 'Settings\SubscriptionController@subscribe');
$router->put('settings/user/plan', 'Settings\SubscriptionController@changeSubscriptionPlan');
$router->delete('settings/user/plan', 'Settings\SubscriptionController@cancelSubscription');
$router->post('settings/user/plan/resume', 'Settings\SubscriptionController@resumeSubscription');
$router->put('settings/user/card', 'Settings\SubscriptionController@updateCard');
$router->put('settings/user/vat', 'Settings\SubscriptionController@updateExtraBillingInfo');
$router->get('settings/user/plan/invoice/{id}', 'Settings\SubscriptionController@downloadInvoice');

It's really up to you how you define your endpoints

Ahmed Bebars
  • 367
  • 1
  • 13
1

If your try for Braintree or Stripe both payment gateway through easily maintan plans and subscription.

Main Benifits :-

  1. Less coding for subscription and plan
  2. UI ready in Braintree tree drop-ui its reponsive
  3. Easily attach add-ons charges
swaroop suthar
  • 632
  • 3
  • 19