From 0ce171205c61949a5e225d814229eb24a588c515 Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Wed, 30 Oct 2024 11:17:08 +0100 Subject: [PATCH] Add payment logic. --- lib/MakeThemPay.pm | 2 ++ lib/MakeThemPay/Controller/Main.pm | 31 +++++++++++++++++++++++++++++- make_them_pay.example.yml | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/MakeThemPay.pm b/lib/MakeThemPay.pm index 231ccc7..35c9aae 100644 --- a/lib/MakeThemPay.pm +++ b/lib/MakeThemPay.pm @@ -15,6 +15,8 @@ sub startup ($self) { # Normal route to controller $r->get('/get_pay_url/:uuid')->to('Main#get_url'); + $r->get('/set_paid/:uuid')->to('Main#set_paid'); + $r->get('/get_paid/:uuid')->to('Main#get_paid'); } 1; diff --git a/lib/MakeThemPay/Controller/Main.pm b/lib/MakeThemPay/Controller/Main.pm index 09aa8d4..466df73 100644 --- a/lib/MakeThemPay/Controller/Main.pm +++ b/lib/MakeThemPay/Controller/Main.pm @@ -13,6 +13,34 @@ use Mojo::Base 'Mojolicious::Controller', -signatures; use Mojo::UserAgent; use Data::Dumper; +use MakeThemPay::DB; + +sub get_paid($self) { + my $uuid = $self->param('uuid'); + my $dbh = MakeThemPay::DB->connect; + my $license = $dbh->selectrow_hashref('SELECT paid FROM licenses WHERE uuid = ?;', {}, $uuid); + if (!defined $license) { + return $self->render(json => $JSON::false); + } + if (!$license->{paid}) { + return $self->render(json => $JSON::false); + } + return $self->render(json => $JSON::true); +} + +sub set_paid($self) { + my $uuid = $self->param('uuid'); + my $dbh = MakeThemPay::DB->connect; + eval { + $dbh->do('INSERT INTO licenses (uuid, paid) VALUES (?, ?);', {}, $uuid, 1); + }; + if ($@) { + warn $@; + return $self->render(text => 'You already paid, if you are unable to use the program contact sergiotarxz@posteo.net'); + } + return $self->render(text => 'You just paid, if you are unable to use the program after a few seconds contact sergiotarxz@posteo.net'); +} + sub get_url ($self) { my $uuid = $self->param('uuid'); my $ua = Mojo::UserAgent->new; @@ -50,6 +78,7 @@ sub get_url ($self) { if ( !defined $price_id ) { die 'Unable to get price'; } + my $base_url = $self->config->{'base-url'} or die 'Base URL not configured'; $result = $ua->post( 'https://api.stripe.com/v1/payment_links' => { Authorization => $authorization @@ -58,7 +87,7 @@ sub get_url ($self) { 'line_items[0][price]' => $price_id, 'line_items[0][quantity]' => 1, 'after_completion[type]' => 'redirect', - 'after_completion[redirect][url]' => "https://exd.sergiotarxz.me/set_paid/$uuid", + 'after_completion[redirect][url]' => $base_url."/set_paid/$uuid", 'automatic_tax[enabled]' => 'true', } )->result; diff --git a/make_them_pay.example.yml b/make_them_pay.example.yml index 0d14b58..6934872 100644 --- a/make_them_pay.example.yml +++ b/make_them_pay.example.yml @@ -2,3 +2,4 @@ secrets: - c930c669dd826bd844b8addabc2cc32ceb920e16 stripe_secret: +base-url: 'https://exd.sergiotarxz.me'