205 lines
6.4 KiB
Perl
205 lines
6.4 KiB
Perl
package Dojo::Controller::Proc;
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
use Mojo::Template;
|
|
use Dojo::Support qw{ send_mail 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(stripe_sk_public => Dojo::Conf::STRIPE_SK_PUBLIC );
|
|
$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 $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;
|
|
}
|
|
|
|
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);
|
|
|
|
my $api_create_path='https://api.stripe.com/v1/payment_intents';
|
|
my $payment_info=[
|
|
'payment_method'=>$pmi,
|
|
'amount'=>int($amount*100),
|
|
'currency'=>'mxn',
|
|
'description'=>$data->{'nombre'},
|
|
'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;}
|
|
|
|
if ($pl > 0) {
|
|
|
|
unless (defined( $r->{'payment_method_options'}->{'card'}->{'installments'}->{'available_plans'} ) ){
|
|
intentError($c,"api can't accept installment",1); return;
|
|
}
|
|
}
|
|
|
|
|
|
#=====================================================
|
|
#intent confirm ======================================
|
|
|
|
my $pii = $r->{'id'};
|
|
my $api_confirm_path = "https://api.stripe.com/v1/payment_intents/$pii/confirm";
|
|
|
|
my $cplan = 0;
|
|
if ($plan != 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,
|
|
]
|
|
}
|
|
|
|
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 ================================
|
|
|
|
$c->mproc->intentConfirm($json,$rr);
|
|
|
|
map { send_mail(@$_) }$c->mproc->notify($json,$rr);
|
|
|
|
my $rtt = {
|
|
'status'=>$rr->{'status'},
|
|
'pl' => $rr->{'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 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;
|