Files
dojo/lib/Dojo/Controller/Misc.pm
2020-01-15 23:42:58 -06:00

108 lines
3.3 KiB
Perl

package Dojo::Controller::Misc;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Template;
use Dojo::Support qw{ log get_names};
# estos son paypal=======:w
use LWP::UserAgent;
use HTTP::Request::Common;
use JSON;
use Data::Dumper;
# 4000004840000008 3, 6, 9, 12, and 18 month installment plans available
# "*p4000004840000008
# 4242424242424242 No installment plans available.
my $sky='sk_test_GXWsfsoxy5336LrSw7SgTXNC00TV9O4Dbq';
my $api_create_path='https://api.stripe.com/v1/payment_intents';
sub stripe {
my $self = shift;
my $request = defined ($self->req->json)? $self->req->json->{'req'}:"start";
# item info ======================
my $header = "Beijing Wisdom </br> Healing Center";
my $title = "Retiro Curso de Tercer método de ZhiNengQiGong";
my $subtitle = "Una experiencia de instrucción del tercer método y profundización con los instructores ZhangQing (Helen) y QiuFuChun (Karl).";
my ($amount,$description,$max_time) = (1417500,"Tercer método",9);
# ==============================
if( $request eq "precheck" ){
my $pmi=$self->req->json->{'payment_method_id'} // 0;
my $t = intentCreate( $pmi, $amount, $description, $max_time);
$self->render(json=>$t);
}
elsif( $request eq "check" ){
my $pii = $self->req->json->{'payment_intent_id'};
my $plan = $self->req->json->{'selected_plan'}//0;
unless ($plan==0) {if ($plan->{count} > $max_time) {
log("\n\n max time instalment attempt\n\n");
$self->render(json=>{'error'=>'max time error'});
return 0;
}}
my $r = intentConfirm($pii,$plan);
$self->render(json=>$r);
}
else {
$self->stash(header=>$header);
$self->stash(title=>$title);
$self->stash(subtitle=>$subtitle);
$self->stash(layout=>'xpay');
}
}
#stripe appi functions
sub intentCreate{
my ($pmi,$amount,$descriptor,$max_time) = @_;
my $payment_info=[
'payment_method'=>$pmi,
'amount'=>$amount,
'currency'=>'mxn',
'description'=>$descriptor,
'payment_method_options[card][installments][enabled]'=>'true'
];
my $r=stripeAppi($api_create_path,$payment_info);
my $t->{intent_id}=$r->{id};
foreach (@{$r->{payment_method_options}{card}{installments}{available_plans}}){
push (@{$t->{available_plans}}, $_) unless ($_->{count}>$max_time);
}
return $t;
}
sub intentConfirm{
my ($pii,$plan) = @_;
my $api_confirm_path = "https://api.stripe.com/v1/payment_intents/$pii/confirm";
my $cplan=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},
]
unless ($plan==0);
my $r = stripeAppi($api_confirm_path,$cplan);
my $rtt = {
'status'=>$r->{'status'},
'plan' => $r->{'payment_method_options'}{'card'}{'installments'}{'plan'},
};
return $rtt;
}
sub stripeAppi{
my ($path,$data) = @_;
my $req = POST $path, $data;
$req->authorization_basic($sky,'');
$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);
}
1;