Add payment logic.

This commit is contained in:
Sergiotarxz 2024-10-30 11:17:08 +01:00
parent b553e01194
commit 0ce171205c
3 changed files with 33 additions and 1 deletions

View File

@ -15,6 +15,8 @@ sub startup ($self) {
# Normal route to controller # Normal route to controller
$r->get('/get_pay_url/:uuid')->to('Main#get_url'); $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; 1;

View File

@ -13,6 +13,34 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;
use Mojo::UserAgent; use Mojo::UserAgent;
use Data::Dumper; 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) { sub get_url ($self) {
my $uuid = $self->param('uuid'); my $uuid = $self->param('uuid');
my $ua = Mojo::UserAgent->new; my $ua = Mojo::UserAgent->new;
@ -50,6 +78,7 @@ sub get_url ($self) {
if ( !defined $price_id ) { if ( !defined $price_id ) {
die 'Unable to get price'; die 'Unable to get price';
} }
my $base_url = $self->config->{'base-url'} or die 'Base URL not configured';
$result = $ua->post( $result = $ua->post(
'https://api.stripe.com/v1/payment_links' => { 'https://api.stripe.com/v1/payment_links' => {
Authorization => $authorization Authorization => $authorization
@ -58,7 +87,7 @@ sub get_url ($self) {
'line_items[0][price]' => $price_id, 'line_items[0][price]' => $price_id,
'line_items[0][quantity]' => 1, 'line_items[0][quantity]' => 1,
'after_completion[type]' => 'redirect', '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', 'automatic_tax[enabled]' => 'true',
} }
)->result; )->result;

View File

@ -2,3 +2,4 @@
secrets: secrets:
- c930c669dd826bd844b8addabc2cc32ceb920e16 - c930c669dd826bd844b8addabc2cc32ceb920e16
stripe_secret: <super_secret> stripe_secret: <super_secret>
base-url: 'https://exd.sergiotarxz.me'