Adding price and discount configuration.

This commit is contained in:
Sergiotarxz 2024-11-12 13:13:45 +01:00
parent dec5cc34f8
commit 449b074f53
3 changed files with 75 additions and 27 deletions

View File

@ -21,6 +21,7 @@ sub startup ($self) {
# Normal route to controller
$r->get('/get_pay_url/:uuid')->to('Main#get_url');
$r->get('/price/:uuid')->to('Main#price');
$r->get('/set_paid/:uuid')->to('Main#set_paid');
$r->get('/get_paid/:uuid')->to('Main#get_paid');
}

View File

@ -15,42 +15,90 @@ use Data::Dumper;
use MakeThemPay::DB;
sub price($self) {
my $uuid = $self->param('uuid');
my ( $price, $discount );
eval { $self->_insert_license( $uuid, 0 ); };
if ($@) {
warn $@;
}
eval { ( $price, $discount ) = $self->_get_real_price; };
if ($@) {
warn $@;
$self->res->code(500);
$self->render( json => undef );
return;
}
return $self->render(
json => {
price => $price,
discount => $discount,
}
);
}
sub _get_real_price($self) {
my $price = $self->config->{price};
my $discount = $self->config->{discount};
if ( !defined $price
|| !defined $discount
|| $discount > 99
|| $discount < 0 )
{
die
"Bad price and/or discount format price: $price discount: $discount";
}
my $discounted_price = $price - ( ( $price * $discount ) / 100 );
if (!wantarray) {
return $discounted_price;
}
return ( $discounted_price, $discount );
}
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);
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);
if ( !$license->{paid} ) {
return $self->render( json => $JSON::false );
}
return $self->render(json => $JSON::true);
return $self->render( json => $JSON::true );
}
sub _insert_license( $self, $uuid, $is_paid ) {
my $dbh = MakeThemPay::DB->connect;
$dbh->do( 'REPLACE INTO licenses (uuid, paid) VALUES (?, ?);',
{}, $uuid, $is_paid );
}
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);
};
my $uuid = $self->param('uuid');
eval { $self->_insert_license( $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 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');
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;
my $product_name = "Hiperthermia product license";
my $product_id = "$uuid-product-license-exd-".int(rand(1000000000));
my $product_id = "$uuid-product-license-exd-" . int( rand(1000000000) );
my $authorization =
'Basic ' . encode_base64url( $self->config->{stripe_secret} );
chomp $authorization;
say $authorization;
my $result = $ua->post(
'https://api.stripe.com/v1/products' => {
Authorization => $authorization
@ -60,19 +108,17 @@ sub get_url ($self) {
id => $product_id,
}
)->result;
print Data::Dumper::Dumper $result->json;
$result = $ua->post(
'https://api.stripe.com/v1/prices' => {
Authorization => $authorization
},
form => {
currency => 'eur',
unit_amount => '500',
unit_amount => int($self->_get_real_price),
product => $product_id,
}
)->result;
my $json = $result->json;
print Data::Dumper::Dumper $json;
my $json = $result->json;
my $price_id = $json->{id};
if ( !defined $price_id ) {
@ -84,14 +130,13 @@ sub get_url ($self) {
Authorization => $authorization
},
form => {
'line_items[0][price]' => $price_id,
'line_items[0][quantity]' => 1,
'after_completion[type]' => 'redirect',
'after_completion[redirect][url]' => $base_url."/set_paid/$uuid",
'automatic_tax[enabled]' => 'true',
'line_items[0][price]' => $price_id,
'line_items[0][quantity]' => 1,
'after_completion[type]' => 'redirect',
'after_completion[redirect][url]' => $base_url . "/set_paid/$uuid",
'automatic_tax[enabled]' => 'true',
}
)->result;
print Data::Dumper::Dumper $result->json;
my $url = $result->json->{url};
if ( !defined $url ) {
die 'Unable to get url';

View File

@ -3,3 +3,5 @@ secrets:
- c930c669dd826bd844b8addabc2cc32ceb920e16
stripe_secret: <super_secret>
base-url: 'https://exd.sergiotarxz.me'
price: 500
discount: 50