This commit is contained in:
mynah
2020-01-29 03:13:02 -06:00
parent 174f4b0756
commit 57f19e749d
7 changed files with 208 additions and 205 deletions

View File

@@ -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;

View File

@@ -17,6 +17,9 @@ select
t.promo_estado as promo_estado,
t.playera_estado as playera_estado,
t.cupo as cupo,
t.meses as meses,
t.descuento_contado as descuento,
t.descuento_contado_estado as desc_estado,
l.nombre as lnombre,
l.federativa as federativa,
l.pais as pais,

View File

@@ -1,6 +1,10 @@
select
t.precio as precio,
c.nombre as nombre
c.nombre as nombre,
t.meses as meses,
t.descuento_contado as descuento,
t.descuento_contado_estado as desc_estado
from
curso_tienda as t
inner join

View File

@@ -61,10 +61,7 @@ sub intentConfirm{
# cliente_nombre,cliente_correo,cliente_comentario,tienda_evento_id,talla_id,cantidad(1),monto,etapa_id(4),entrega(3),pago_clave,plazo,fecha_pago
my $mq = $data->{'mq'};
my $students = $data->{'students'};
my $plan = 0;
if (defined $data->{'selected_plan'}){
$plan = $data->{'selected_plan'}->{'count'};
}
my $plan = $r->{'payment_method_options'}->{'card'}->{'installments'}->{'plan'}->{'count'} // 0;
for (1..$mq){
_write (
$data_path->child("/spay/qIntentConfirm.q")->slurp,
@@ -88,19 +85,20 @@ sub notify{
my ($c,$d,$r) = @_;
my $t =shift @{ _read ($data_path->child("spay/qNotify.q")->slurp,$d->{'tid'})};
my @ra;
my %h=qw/0 na 1 ch 2 m 3 g/;
my $plazo= defined( $d->{'selected_plan'})?
$d->{'selected_plan'}->{'count'} : "0";
my %h=qw/0 na 1 CH 2 M 3 G/;
my $plazo = $r->{'payment_method_options'}->{'card'}->{'installments'}->{'plan'}->{'count'};
my $students = $d->{'students'};
my $basetxta = "$t->{'cnombre'}
clave de pago stripe: $d->{'payment_intent_id'}
my $basetxta = "$t->{'cnombre'}
clave de pago stripe: $r->{'id'}
plazo: $plazo meses
cantidad: $t->{'cantidad'}
";
my $basetxtu = $t->{'cnombre'}//""."
".$t->{'subnombre'}//""."
";
my $basetxtu = "
$t->{'cnombre'}
============================================
".$t->{'subnombre'}//""."
/////////////////////////////////////////////
";
foreach(1 .. $d->{'mq'}) {
push (@ra, ["
@@ -110,9 +108,9 @@ $$students[$_ -1][0]
tu clave de pago es la sigiente. Tenla a la mano para cualquier aclaración
==========================
$d->{'payment_intent_id'}
===========================
==================================
$r->{'id'}
==================================
", " Inscripción ", $$students[$_ -1][1]]);
$basetxta .= "
Nombre: $$students[$_ -1][0]
@@ -125,6 +123,9 @@ $$students[$_ -1][0]
}
push (@ra, [$basetxta,"Inscripciones",'david@chaos.foundation']);
push (@ra, [$basetxta,"Inscripciones",'benjamuga@gmail.com']);
push (@ra, [$basetxta,"Inscripciones",'mmunoz@harvest.com']);
return @ra;
}
@@ -139,19 +140,6 @@ $$students[$_ -1][0]
#'students' => [ [ 'User', 'me@me.com', 'obs', 'M' ], [ 'usr2', 'mail2', 'obs', 'G' ], undef, undef, undef, undef, undef, undef, undef, undef ] #arr(10)
# ===============
sub intentSanity{
my ($c,$d)=@_;
if (defined $d->{'selected_plan'} ){
return -1 unless defined ($d->{'selected_plan'}{'type'});
return -1 unless defined ($d->{'selected_plan'}{'count'});
return -1 unless defined ($d->{'selected_plan'}{'interval'});
}
return -1 unless defined ($d->{'tid'}) && $d->{'tid'} =~ /^\d+$/ ;
return -1 unless defined ($d->{'req'}) && $d->{'req'} eq 'check';
return -1 unless defined ($d->{'payment_intent_id'});
return userDataSanity($c,$d) ;
}
sub userDataSanity{
my ($c,$d) = @_;
return -1 unless (
@@ -160,6 +148,7 @@ sub userDataSanity{
$d->{'mq'} =~/^\d+$/ &&
$d->{'mq'} < 10
);
my %h=qw(n 0 CH 1 M 2 G 3);
my $mq = $d->{'mq'};
my $students = $d->{'students'};