stripe recurring payments per cycle for quantity plans - laravel

Dears,
I'm working on a system with tow plans interval monthly and yearly subscriptions
the cost of the subscription per quantity so the customer can increment and decrement the subscriptions as he wants
I have a subscriptions table with the following column
CREATE TABLE `subscriptions` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`company_company_id` int NOT NULL,
`plan_name` varchar(191) NOT NULL,
`product_id` varchar(100) DEFAULT NULL,
`subscription_stripe_id` varchar(191) NOT NULL,
`stripe_status` varchar(191) NOT NULL,
`stripe_price_id` varchar(191) DEFAULT NULL,
`quantity` int DEFAULT NULL,
`amount` float DEFAULT NULL,
`trial_ends_at` timestamp NULL DEFAULT NULL,
`ended_at` timestamp NULL DEFAULT NULL,
`ends_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`interval` varchar(20) DEFAULT NULL,
`current_period_start` timestamp NULL DEFAULT NULL,
`current_period_end` timestamp NULL DEFAULT NULL,
`cancelled_at` timestamp NULL DEFAULT NULL,
`cancellation_reason_id` int DEFAULT NULL,
`cancellation_notes` text,
PRIMARY KEY (`id`),
KEY `subscriptions_company_id_stripe_status_index` (`stripe_status`)
) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
I need to make a monthly report for invoices for each subscription cycle
my questions what happened exactly when renewing subscriptions current_period_start and
current_period_end column is updated to the new subscriptions cycle how I can get invoices per cycle because the subscriptions start and end date not always start from the first day of the month
thanks

The simplest thing would be to list the Invoices that belong to a particular Subscription [1]. Those monthly/yearly Invoices will be listed in reverse chronological order. The topmost Invoice will always be the on created for the current billing period (assuming licensing and not usage reported billing).
You could also listen for webhook events for invoice.created events as the Subscription cycles and process the Invoices in your own system as they arrive [2].
[1] https://stripe.com/docs/api/invoices/list#list_invoices-subscription
[2] https://stripe.com/docs/webhooks

Related

Sort By with function on collection laravel

I would like some help on this, i have a table like this
CREATE TABLE IF NOT EXISTS `items` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`code` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`unitPrice` decimal(8,2) NOT NULL,
`quantity` int(11) NOT NULL,
`totalSold` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and after using DB::table('items')->get();
i would like to get the same result as if i run this sql command
"select `code`,(`unitPrice`*(`quantity`+`totalSold`)) as totalEarnIfsold from `items` order by `totalEarnIfsold` desc"
what i have been able to achieve without success ofc is this:
$items_all->sortBy([
$totalEarnIfsold= fn ($a) => $a->unitPrice *($a->quantity+$a->totalSold),
['totalEarnIfsold', 'desc'],
]);
So i need your help if you may ofc, and thanks
You can do in following way
First you need to calculate totalEarnIfsold with map function and after that you can easily sort with value.
$items_all = $items_all->map(function($item) {
$item->totalEarnIfsold = $item->unitPrice *($item->quantity + $item->totalSold);
return $item;
})->sortByDesc('totalEarnIfsold');

Error on SQLite migrations after upgrade Laravel 5.8 to 6.2

I need some help!
I have an API built in laravel 5.8, i am upgrading the platform to 6.2.
After all changes in configuration files and some scripts php, all my tests witch run the migrations on SQLite is broken.
The following error is displayed:
SQLSTATE[HY000]: General error: 1 no such collation sequence: utf8_general_ci (SQL: CREATE TABLE events (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, event VARCHAR(255) NOT NULL COLLATE BINARY, description CLOB DEFAULT NULL COLLATE BINARY, invitation CLOB DEFAULT NULL COLLATE BINARY, sale CLOB DEFAULT NULL COLLATE BINARY, information CLOB DEFAULT NULL COLLATE BINARY, city VARCHAR(255) NOT NULL COLLATE BINARY,
location VARCHAR(255) NOT NULL COLLATE BINARY, date_start DATE NOT NULL, time_start TIME NOT NULL, date_end DATE DEFAULT NULL, flyer CLOB DEFAULT NULL COLLATE BINARY, atv BOOLEAN DEFAULT '1', created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAULT NULL, location_map VARCHAR(255) DEFAULT NULL COLLATE utf8_general_ci --IFRAME com a localização do evento.
My intention is update to 7.x after resolve all issues on 6.2.
In the migrations, was enough to remove the collation option from the fields. Example:
Initially this way:
$table->string('field', 255)->charset('utf8')->collation('utf8_general_ci')->change();
The solution was as follows:
$table->string('field', 255)->charset('utf8')->change();
Removing this option will not force a collation not accepted by SQLite.

Laravel - Eloquent query to display Quiz Result

I am trying to write a query in my controller to display Quiz result for students. I have these tables
CREATE TABLE IF NOT EXISTS `quizz_question` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`topic` varchar(1000) COLLATE utf8_unicode_ci NOT NULL,
`question_code` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`answer1` varchar(100) COLLATE utf8_unicode_ci NULL,
`answer2` varchar(100) COLLATE utf8_unicode_ci NULL,
`answer3` varchar(100) COLLATE utf8_unicode_ci NULL,
`answer4` varchar(100) COLLATE utf8_unicode_ci NULL,
`topic` varchar(1000) COLLATE utf8_unicode_ci NOT NULL,
`correct_answer` varchar(100) COLLATE utf8_unicode_ci NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;
CREATE TABLE IF NOT EXISTS `quizz_attempt` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`student_code` varchar(1000) COLLATE utf8_unicode_ci NOT NULL,
`answer` varchar(100) COLLATE utf8_unicode_ci NULL,
`question_code` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;
The two tables became these two model classes: QuizzQuestion and QuizAttempt.
In quizz_attempt, if a student select an answer (answer), it will take answer and question_code in quizz_attempt and compare with quizz_question.
quizz_attempt.answer = 1 then it will choose the content in answer1 as correct answer
quizz_attempt.answer = 2 then it will choose the content in answer2 as correct answer
quizz_attempt.answer = 3 then it will choose the content in answer3 as correct answer
quizz_attempt.answer = 4 then it will choose the content in answer4 as correct answer
public function gameQualifiers(Request $request)
{
$revenuedetails = DB::table('quizz_attempt as g')
->select(
'g.student_code',
'g.answer'
)
->orderByRaw('g.created_at DESC');
}
I know I need to join the two tables for the result. I started the code in my controller, but don't know how to complete it. i want to write a query to display list of students who choose correct answers
The quiz attempt should have the quizz_question_id as a Foreign Key. This will make it easier down the line to have the two connected.
You can set up two models to match your database tables: QuizzQuestion and QuizzAttempt. You can set up Foreign Keys like so:
QuizzAttempt.php
public function quizzQuestion()
{
return $this->belongsTo('App\QuizzQuestion');
}
And QuizzQuestion.php
public function quizzAttempts()
{
return $this->hasMany('App\QuizzQuestion');
}
So now, you want to get all of the attempts where the answer is correct - you are looking at an instance of QuizzQuestion, e.g.
$question = QuizzQuestion::find(1); // first question
$correctResults = QuizzAttempts::where('quizz_question_id', $question->id)
->where('answer', $question->correct_answer)
->pluck('student_code');`
Now you have all the student codes of the students that got the correct answers.
*** Update
If you cannot change the structure of the tables, you can run the following query:
// 1) find the question you want the answers for
$question = QuizzQuestion::find(1);
// 2) retrieve the correct results
$correctResults = QuizzAttempts::where('question_code', $question->question_code)
->where('answer', $question->correct_answer)
->pluck('student_code');`

ERROR: null value in column "id" violates not-null constraint

I just migrated my app from mysql to postgres but when I try to insert a record in a specific table I get violates not-null constraint error:
ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null).
********** Error **********
ERROR: null value in column "id" violates not-null constraint
SQL state: 23502
Detail: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null).
When I try to create the record using factory_girl:
#assignment = FactoryGirl.create(:assignment)
It builds this sql query:
INSERT INTO assignments(
id, account_id, l_id, viewed_at, accepted_at, declined_at,
expires_at, created_at, updated_at, decline_reason, decline_reason_text,
promotion, c_checked_at, forwardable, type, f_promo,
c_check_successful, c_check_api_result, c_check_human_result)
VALUES (null, 1, 1, null, null, null, '2016-03-09 09:24:12.841891', '2012-12-31 23:00:00', '2012-12-31 23:00:00', null, null, 'f', null, 'f', 'XYZAssignment', null, null, null, null);
This is the assignment factory:
FactoryGirl.define do
factory :assignment do
expires_at 24.hours.from_now
account
lead
end
end
this is the table description:
CREATE TABLE assignments(
id serial NOT NULL, account_id integer NOT NULL, l_id integer NOT NULL, viewed_at timestamp without time zone, accepted_at timestamp without time zone, declined_at timestamp without time zone, expires_at timestamp without time zone, created_at timestamp without time zone, updated_at timestamp without time zone, decline_reason character varying(16), decline_reason_text character varying(256), promotion boolean NOT NULL DEFAULT false, c_checked_at timestamp without time zone, forwardable boolean DEFAULT true, type character varying(64), f_promo boolean, c_check_successful boolean, c_check_api_result character varying(32), c_check_human_result character varying(32), CONSTRAINT assignments_pkey PRIMARY KEY (id)
) WITH (
OIDS=FALSE
);
Looks its not able to auto increment the id, any idea?
You have to skip id in the INSERT operation:
INSERT INTO assignments(account_id, l_id, ...)
VALUES
(1, 1, ...)
The id will automatically get the next sequence number, since it is an auto-increment field.

Can I use spring security without username or password?

I need to make sso for applications and combile user table.
I want to use spring security with email authentication, without username or password.
How can I do this?
My limittations:
Single user can authenticate with multiple emails (Like github)
User can manage all authentication state and expire specific authentication. (In profile page)
No password. No string username or id. (Because no service supports basic login)
--- EDIT ---
OAuth 2.0 / 1.0a Authentication
Generated scheme:
(Is this proper for this case?)
create table hib_authentication (
id BIGINT UNSIGNED not null auto_increment,
firstAuthenticatedTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP not null,
ip VARBINARY(16) not null,
browser VARBINARY(24) not null,
lastAuthenticatedTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP not null,
user_id INT UNSIGNED not null,
primary key (id)
) ENGINE=InnoDB;
create table hib_user (
id INT UNSIGNED not null auto_increment,
country SMALLINT UNSIGNED not null,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP not null,
locale varchar(255) not null,
timeZone varchar(255) not null,
primary key (id)
) ENGINE=InnoDB;
create table hib_user_email (
id BIGINT UNSIGNED not null auto_increment,
email varchar(255) not null,
user_id INT UNSIGNED not null,
primary key (id)
) ENGINE=InnoDB;
create index index_to_get_authentication_by_user on hib_authentication (user_id);
alter table hib_user_email
add constraint UK_isuygi7fmcwnlht8f4plckt6n unique (email);
create index index_to_search_email_by_user on hib_user_email (user_id);
alter table hib_authentication
add constraint authentication_belongs_to_user
foreign key (user_id)
references hib_user (id);
alter table hib_user_email
add constraint email_belongs_to_user
foreign key (user_id)
references hib_user (id);

Resources