220 lines
6.6 KiB
Perl
220 lines
6.6 KiB
Perl
package Dojo::Controller::Proc;
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
use Mojo::Template;
|
|
use Dojo::Support qw{ log get_names};
|
|
|
|
use LWP::UserAgent;
|
|
use HTTP::Request::Common;
|
|
use JSON;
|
|
use Data::Dumper;
|
|
#=========================
|
|
|
|
|
|
# User ========================================================================
|
|
|
|
sub cal {
|
|
my $c = shift;
|
|
my $data=$c->mproc->calendar_events;
|
|
my $block=$c->mproc->calendar_monthblock;
|
|
my %h;
|
|
map{ push @{ $h{$_->{bid}} },$_; }(@$data);
|
|
$c->stash( r=>\%h, b=>$block);
|
|
$c->stash(layout=>'default');
|
|
}
|
|
|
|
sub event{
|
|
my $c = shift;
|
|
my $data=$c->mproc->event($c->param('id'));
|
|
$c->redirect_to('cal') unless (defined $data->{'cname'});
|
|
$c->stash($data);
|
|
$c->stash(layout=> "defaultContact");
|
|
}
|
|
|
|
sub spay{
|
|
my $c = shift;
|
|
my $data=$c->mproc->spay(1,$c->param('id'));
|
|
|
|
$c->redirect_to('cal') unless (defined $data->{'tid'});
|
|
if ($c->param('id') >= 80 && $c->param('id') <= 86 ){
|
|
$c->stash(layout=> "xpay");
|
|
}
|
|
else {
|
|
$c->stash(layout=> "defaultContact");
|
|
}
|
|
$c->stash($data);
|
|
}
|
|
|
|
#stripe appi function =============================================================================
|
|
sub intentCreate{
|
|
my $c = shift;
|
|
unless (defined ($c->req->json)){
|
|
$c->stash(layout=>'clean');
|
|
$c->render(template=>'home/not_found', status=>404);
|
|
return 0;
|
|
}
|
|
|
|
my $data=$c->mproc->intentCreate($c->req->json->{'tid'});
|
|
# if(!@$data){ intentError($c,"event not found",1); return;};
|
|
|
|
my $pmi=$c->req->json->{'payment_method_id'} // 0;
|
|
my $amount = $data->{'precio'} * $c->req->json->{'mq'} ;
|
|
my $description = $data->{'nombre'};
|
|
my $max_time = 9;
|
|
|
|
my $api_create_path='https://api.stripe.com/v1/payment_intents';
|
|
my $payment_info=[
|
|
'payment_method'=>$pmi,
|
|
'amount'=>$amount*100,
|
|
'currency'=>'mxn',
|
|
'description'=>$description,
|
|
'payment_method_options[card][installments][enabled]'=>'true'
|
|
];
|
|
my $r=stripeAppi($api_create_path,$payment_info);
|
|
if ($r==0){intentError($c,"api create error",1); return;}
|
|
my $t->{intent_id}=$r->{id};
|
|
foreach (@{$r->{payment_method_options}{card}{installments}{available_plans}}){
|
|
push (@{$t->{available_plans}}, $_) unless ($_->{count} > $max_time);
|
|
}
|
|
$c->render(json=>$t);
|
|
return 0;
|
|
}
|
|
|
|
sub intentConfirm{
|
|
my $c = shift;
|
|
my $max_time = 9;
|
|
|
|
unless (defined ($c->req->json)){
|
|
$c->stash(layout=>'clean');
|
|
$c->render(template=>'home/not_found', status=>404);
|
|
return;
|
|
}
|
|
|
|
unless ($c->mproc->intentSanity($c->req->json) == 0 ){
|
|
intentError($c,"sanity failed",0); return; }
|
|
|
|
my $data = $c->req->json;
|
|
my $pii = $data->{'payment_intent_id'};
|
|
my $plan = $data->{'selected_plan'} // 0;
|
|
my $api_confirm_path = "https://api.stripe.com/v1/payment_intents/$pii/confirm";
|
|
|
|
my $cplan = 0;
|
|
if ($plan != 0){
|
|
if ($plan->{count} > $max_time) {
|
|
intentError($c,'max time installment attempt error',3);
|
|
return 0;
|
|
}
|
|
$cplan = [
|
|
'payment_method_options[card][installments][plan][type]'=>'fixed_count',
|
|
'payment_method_options[card][installments][plan][interval]'=>'month',
|
|
'payment_method_options[card][installments][plan][count]'=>$plan->{count},
|
|
]
|
|
}
|
|
|
|
if (
|
|
intentCheck(
|
|
$c,
|
|
$data->{'payment_intent_id'},
|
|
$data->{'tid'},
|
|
$data->{'mq'}
|
|
) == -1) {
|
|
intentError($c,"api confirm error pre",2); return;
|
|
}
|
|
|
|
my $r = stripeAppi($api_confirm_path,$cplan);
|
|
if ($r==0){intentError($c,"api confirm error post",2); return;}
|
|
if ($r->{'status'} ne "succeeded" ){intentError($c,"payment not completed,2"); return;}
|
|
|
|
$c->mproc->intentConfirm($data,$r);
|
|
my $rtt = {
|
|
'status'=>$r->{'status'},
|
|
'plan' => $r->{'payment_method_options'}{'card'}{'installments'}{'plan'},
|
|
};
|
|
$c->render(json=>$rtt);
|
|
return 0;
|
|
}
|
|
|
|
sub intentError{ # e: error msg, stage: 0-userinfo, 1 icreate. 2 iconfirm
|
|
my $c = shift;
|
|
$c->render(json => { 'e'=>shift, 'stage'=>shift });
|
|
return 0;
|
|
}
|
|
|
|
sub userCheck{
|
|
my $c=shift;
|
|
my $r = { 'userst' => $c->mproc->userDataSanity($c->req->json)};
|
|
$c->render(json=>$r);
|
|
return 0;
|
|
}
|
|
|
|
sub intentCheck{
|
|
my ($c, $cid, $tid, $mq) = @_;
|
|
my $stripe = stripeAppi("https://api.stripe.com/v1/payment_intents/$cid",0);
|
|
my $data=$c->mproc->intentCreate($tid);
|
|
return -1 unless ( $stripe->{'amount'} == ($data->{'precio'} * $mq)*100);
|
|
return 0;
|
|
}
|
|
|
|
sub stripeAppi{
|
|
my ($path,$data) = @_;
|
|
my $req;
|
|
if ($data == 0 ) { $req = POST $path;} # -.-
|
|
else { $req = POST $path, $data;}
|
|
$req->authorization_basic(Dojo::Conf::STRIPE_SK,'');
|
|
$req->content_type('application/x-www-form-urlencoded');
|
|
my $ua = LWP::UserAgent->new;
|
|
my $res=$ua->request($req);
|
|
unless ($res->is_success){
|
|
log("stripe error! path=$path\n data=".Dumper($data)."\n $res->status_line ");
|
|
return 0;
|
|
}
|
|
my $json = JSON->new->utf8;
|
|
return $json->decode($res->decoded_content);
|
|
}
|
|
|
|
# Not used but maybe later ======================================================
|
|
|
|
#sub storeppl{
|
|
#my $c=shift;
|
|
#if( defined($c->req->json) ){
|
|
#my $oid = $c->req->json->{'orderID'} //0;
|
|
#log( $oid );
|
|
##credentials====
|
|
#my $client='AQdZ1JJL-GNBgGTI3W6cXChmj6MnZsInRGlWeHw1kbGiE_49eTtZ2fPvEq9ersU2y6O5WpxccdsyAQw7';
|
|
#my $secter='EBn8CNOCrJbVZ_fNllXB7oUIm9_vhtqrJMm2zSBGbxSyiiych3cHOIWOLAD5NVw4Z3dmNhmsHQwPsrZ0';
|
|
#my $pauth='https://api.sandbox.paypal.com/v1/oauth2/token/';
|
|
#my $porder='https://api.sandbox.paypal.com/v2/checkout/orders/';
|
|
##=================
|
|
##pre auth ====
|
|
#my $ua = LWP::UserAgent->new;
|
|
#my $req=POST $pauth, ["grant_type"=>"client_credentials"];
|
|
#$req->authorization_basic($client,$secter);
|
|
#$req->content_type('application/x-www-form-urlencoded');
|
|
#my $res=$ua->request($req);
|
|
#log("paypal pre auth error! $res->status_line") unless ($res->is_success );
|
|
#my $json = JSON->new->utf8;
|
|
#my $r=$json->decode($res->decoded_content);
|
|
##==============
|
|
|
|
##order details ====
|
|
#my $ub = LWP::UserAgent->new;
|
|
#$ub->default_header('Content-Type'=> 'application/json' );
|
|
#$ub->default_header('Authorization'=> 'Bearer ' . $r->{access_token} );
|
|
#my $resb=$ub->get($porder.$oid);
|
|
#log("paypal checkout error!") unless ($resb->is_success );
|
|
#my $jsonb = JSON->new->utf8;
|
|
#my $s=$jsonb->decode($resb->decoded_content);
|
|
##===================
|
|
#$c->rendered(200); #es default,nomas para enfatizar que puede ser 400 o 500
|
|
## interesan create time, id, payer: email address, shippng address
|
|
#}
|
|
#else{
|
|
#my $data=$c->dbg->store_id($c->param('id'));
|
|
#$c->redirect_to('cal') if ($c->param('id')==4);
|
|
#$c->redirect_to('store') unless ($data->{'id'});
|
|
#$c->stash($data);
|
|
#$c->stash(layout=>'default');
|
|
#}
|
|
#}
|
|
|
|
1;
|