Adding initial version.

This commit is contained in:
sergiotarxz 2024-10-24 13:54:16 +02:00
commit b553e01194
8 changed files with 146 additions and 0 deletions

20
lib/MakeThemPay.pm Normal file
View File

@ -0,0 +1,20 @@
package MakeThemPay;
use Mojo::Base 'Mojolicious', -signatures;
# This method will run once at server start
sub startup ($self) {
# Load configuration from config file
my $config = $self->plugin('NotYAMLConfig');
# Configure the application
$self->secrets($config->{secrets});
# Router
my $r = $self->routes;
# Normal route to controller
$r->get('/get_pay_url/:uuid')->to('Main#get_url');
}
1;

View File

@ -0,0 +1,77 @@
package MakeThemPay::Controller::Main;
use v5.40.0;
use strict;
use warnings;
use JSON;
use MIME::Base64 qw/encode_base64url/;
use Mojo::Base 'Mojolicious::Controller', -signatures;
use Mojo::UserAgent;
use Data::Dumper;
sub get_url ($self) {
my $uuid = $self->param('uuid');
my $ua = Mojo::UserAgent->new;
my $product_name = "Licencia de producto para Hipertermia";
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
},
form => {
name => $product_name,
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',
product => $product_id,
}
)->result;
my $json = $result->json;
print Data::Dumper::Dumper $json;
my $price_id = $json->{id};
if ( !defined $price_id ) {
die 'Unable to get price';
}
$result = $ua->post(
'https://api.stripe.com/v1/payment_links' => {
Authorization => $authorization
},
form => {
'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",
'automatic_tax[enabled]' => 'true',
}
)->result;
print Data::Dumper::Dumper $result->json;
my $url = $result->json->{url};
if ( !defined $url ) {
die 'Unable to get url';
}
$self->render(
json => {
url => $url,
}
);
}
1;

View File

@ -0,0 +1,4 @@
---
secrets:
- c930c669dd826bd844b8addabc2cc32ceb920e16
stripe_secret: <super_secret>

11
public/index.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the Mojolicious real-time web framework!</title>
</head>
<body>
<h2>Welcome to the Mojolicious real-time web framework!</h2>
This is the static document "public/index.html",
<a href="/">click here</a> to get back to the start.
</body>
</html>

11
script/make_them_pay Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::File qw(curfile);
use lib curfile->dirname->sibling('lib')->to_string;
use Mojolicious::Commands;
# Start command line interface for application
Mojolicious::Commands->start_app('MakeThemPay');

9
t/basic.t Normal file
View File

@ -0,0 +1,9 @@
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
my $t = Test::Mojo->new('MakeThemPay');
$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);
done_testing();

View File

@ -0,0 +1,9 @@
% layout 'default';
% title 'Welcome';
<h2><%= $msg %></h2>
<p>
This page was generated from the template "templates/example/welcome.html.ep"
and the layout "templates/layouts/default.html.ep",
<%= link_to 'click here' => url_for %> to reload the page or
<%= link_to 'here' => '/index.html' %> to move forward to a static page.
</p>

View File

@ -0,0 +1,5 @@
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>