finspay
This commit is contained in:
@@ -41,6 +41,7 @@ sub spay{
|
||||
else {
|
||||
$c->stash(layout=> "defaultContact");
|
||||
}
|
||||
$c->stash(stripe_sk_public => Dojo::Conf::STRIPE_SK_PUBLIC );
|
||||
$c->stash($data);
|
||||
}
|
||||
|
||||
@@ -50,87 +51,76 @@ sub intentCreate{
|
||||
unless (defined ($c->req->json)){
|
||||
$c->stash(layout=>'clean');
|
||||
$c->render(template=>'home/not_found', status=>404);
|
||||
return 0;
|
||||
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 $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'=>$amount*100,
|
||||
'amount'=>int($amount*100),
|
||||
'currency'=>'mxn',
|
||||
'description'=>$description,
|
||||
'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;}
|
||||
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;
|
||||
if ($pl > 0) {
|
||||
|
||||
unless (defined( $r->{'payment_method_options'}->{'card'}->{'installments'}->{'available_plans'} ) ){
|
||||
intentError($c,"api can't accept installment",1); 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;
|
||||
#=====================================================
|
||||
#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){
|
||||
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},
|
||||
'payment_method_options[card][installments][plan][count]'=>$plan,
|
||||
]
|
||||
}
|
||||
|
||||
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);
|
||||
map { send_mail(@$_) }$c->mproc->notify($data,$r);
|
||||
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'=>$r->{'status'},
|
||||
'plan' => $r->{'payment_method_options'}{'card'}{'installments'}{'plan'},
|
||||
'status'=>$rr->{'status'},
|
||||
'pl' => $rr->{'payment_method_options'}{'card'}{'installments'}{'plan'},
|
||||
};
|
||||
$c->render(json=>$rtt);
|
||||
return 0;
|
||||
@@ -149,14 +139,6 @@ sub userCheck{
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user