Files

205 lines
6.4 KiB
Perl
Raw Permalink Normal View History

2020-01-15 23:42:58 -06:00
package Dojo::Controller::Proc;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Template;
2020-01-22 18:22:31 -06:00
use Dojo::Support qw{ send_mail log get_names};
2020-01-20 14:49:59 -06:00
2020-01-15 23:42:58 -06:00
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'));
2020-01-20 18:19:38 -06:00
$c->redirect_to('cal') unless (defined $data->{'cname'});
2020-01-15 23:42:58 -06:00
$c->stash($data);
$c->stash(layout=> "defaultContact");
}
2020-01-20 18:19:38 -06:00
sub spay{
2020-01-15 23:42:58 -06:00
my $c = shift;
2020-01-20 18:19:38 -06:00
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");
}
2020-01-29 03:13:02 -06:00
$c->stash(stripe_sk_public => Dojo::Conf::STRIPE_SK_PUBLIC );
2020-01-20 18:19:38 -06:00
$c->stash($data);
2020-01-15 23:42:58 -06:00
}
2020-01-20 14:49:59 -06:00
#stripe appi function =============================================================================
2020-01-15 23:42:58 -06:00
sub intentCreate{
my $c = shift;
unless (defined ($c->req->json)){
$c->stash(layout=>'clean');
$c->render(template=>'home/not_found', status=>404);
2020-01-29 03:13:02 -06:00
return 0;
}
my $json = $c->req->json;
my $data=$c->mproc->intentCreate($json->{'tid'});
if ($data==0){intentError($c,"store id error",1); return;}
if ( $c->mproc->userDataSanity($json) != 0) {
intentError($c,"data sanity error",1); return;
2020-01-15 23:42:58 -06:00
}
2020-01-20 14:49:59 -06:00
2020-01-29 03:13:02 -06:00
my $max_time = $data->{'meses'};
my @count = (0,3,6,9,12,18); #arregla esta atrocidad
my $idisc= $data->{'desc_estado'}; #immediate payment discount
my $discount = $data->{'descuento'}; #disoutn %
my $pl = $json->{'pl'}; # 0 imediate. 1-5: 3,6,9,12,18 months installment
my $plan = $count[$pl];
my $pmi=$json->{'payment_method_id'};
my $amount = $data->{'precio'} * $json->{'mq'} ;
$amount = $amount * (1 - ($discount/100 )) if($pl==0 && $idisc == 1 && $discount > 0);
2020-01-20 14:49:59 -06:00
2020-01-15 23:42:58 -06:00
my $api_create_path='https://api.stripe.com/v1/payment_intents';
my $payment_info=[
'payment_method'=>$pmi,
2020-01-29 03:13:02 -06:00
'amount'=>int($amount*100),
2020-01-15 23:42:58 -06:00
'currency'=>'mxn',
2020-01-29 03:13:02 -06:00
'description'=>$data->{'nombre'},
2020-01-15 23:42:58 -06:00
'payment_method_options[card][installments][enabled]'=>'true'
];
my $r=stripeAppi($api_create_path,$payment_info);
2020-01-20 14:49:59 -06:00
if ($r==0){intentError($c,"api create error",1); return;}
2020-01-15 23:42:58 -06:00
2020-01-29 03:13:02 -06:00
if ($pl > 0) {
unless (defined( $r->{'payment_method_options'}->{'card'}->{'installments'}->{'available_plans'} ) ){
intentError($c,"api can't accept installment",1); return;
}
2020-01-15 23:42:58 -06:00
}
2020-01-20 14:49:59 -06:00
2020-01-29 03:13:02 -06:00
#=====================================================
#intent confirm ======================================
my $pii = $r->{'id'};
2020-01-20 14:49:59 -06:00
my $api_confirm_path = "https://api.stripe.com/v1/payment_intents/$pii/confirm";
2020-01-29 03:13:02 -06:00
2020-01-20 14:49:59 -06:00
my $cplan = 0;
if ($plan != 0){
$cplan = [
'payment_method_options[card][installments][plan][type]'=>'fixed_count',
'payment_method_options[card][installments][plan][interval]'=>'month',
2020-01-29 03:13:02 -06:00
'payment_method_options[card][installments][plan][count]'=>$plan,
2020-01-20 14:49:59 -06:00
]
}
2020-01-15 23:42:58 -06:00
2020-01-29 03:13:02 -06:00
my $rr = stripeAppi($api_confirm_path,$cplan);
if ($rr==0){intentError($c,"api confirm error post",2); return;}
if ($rr->{'status'} ne "succeeded" ){intentError($c,"payment not completed,2"); return;}
#======================================================
# end and send messages ================================
2020-01-22 18:22:31 -06:00
2020-01-29 03:13:02 -06:00
$c->mproc->intentConfirm($json,$rr);
map { send_mail(@$_) }$c->mproc->notify($json,$rr);
2020-01-22 18:22:31 -06:00
2020-01-15 23:42:58 -06:00
my $rtt = {
2020-01-29 03:13:02 -06:00
'status'=>$rr->{'status'},
'pl' => $rr->{'payment_method_options'}{'card'}{'installments'}{'plan'},
2020-01-15 23:42:58 -06:00
};
$c->render(json=>$rtt);
return 0;
}
2020-01-20 14:49:59 -06:00
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;
}
2020-01-15 23:42:58 -06:00
sub stripeAppi{
my ($path,$data) = @_;
2020-01-20 14:49:59 -06:00
my $req;
if ($data == 0 ) { $req = POST $path;} # -.-
else { $req = POST $path, $data;}
$req->authorization_basic(Dojo::Conf::STRIPE_SK,'');
2020-01-15 23:42:58 -06:00
$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);
}
2020-01-20 14:49:59 -06:00
# 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');
#}
#}
2020-01-15 23:42:58 -06:00
1;