From 30be4eeb21e51778046714042cb882a75e19bdeb Mon Sep 17 00:00:00 2001 From: muthulakshmi Date: Thu, 29 Dec 2022 17:39:33 +0530 Subject: [PATCH] checkout api --- app/Config/Routes.php | 6 +- app/Controllers/Api/Checkout/Index.php | 39 +++++++++++ .../Myaccount/Currentreservation/Index.php | 68 ++++++++++++++++--- 3 files changed, 102 insertions(+), 11 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 61a1afd..16ed1bc 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -90,7 +90,8 @@ //Current Reservation $routes->post('list', 'Api\Myaccount\Currentreservation\Index::index'); $routes->post('view', 'Api\Myaccount\Currentreservation\Index::view'); - $routes->post('paidunpaid', 'Api\Myaccount\Currentreservation\Index::paidunpaid'); + $routes->post('paidunpaid', 'Api\Myaccount\Currentreservation\Index::paidunpaid'); + $routes->post('striperefunds', 'Api\Myaccount\Currentreservation\Index::striperefunds'); $routes->post('cancelsubscription', 'Api\Myaccount\Currentreservation\Index::cancelsubscription'); //Past Reservation @@ -122,7 +123,8 @@ //Checkout $routes->post('checkout', 'Api\Checkout\Index::index'); $routes->post('checkout/action', 'Api\Checkout\Index::action'); - $routes->post('stripepayment', 'Api\Stripe\Index::stripepayment'); + $routes->post('stripepayment', 'Api\Stripe\Index::stripepayment'); + $routes->post('stripesecretkey', 'Api\Checkout\Index::stripesecretkey'); //Homepage $routes->get('faq', 'Api\Faq\Index::index'); diff --git a/app/Controllers/Api/Checkout/Index.php b/app/Controllers/Api/Checkout/Index.php index 66c8037..b69d554 100644 --- a/app/Controllers/Api/Checkout/Index.php +++ b/app/Controllers/Api/Checkout/Index.php @@ -7,6 +7,8 @@ use App\Models\Event; use App\Models\Cart; use App\Models\Booking; +use App\Models\Users; +use App\Models\Stripe; class Index extends BaseController { @@ -16,6 +18,8 @@ public function __construct() $this->event = new Event(); $this->cart = new Cart(); $this->booking = new Booking(); + $this->users = new Users(); + $this->stripe = new Stripe(); } public function index() @@ -174,6 +178,18 @@ public function index() $total =''; $totaldue = (number_format($price,2)+ number_format($transactionfee,2)+ number_format($cleaning_fee,2)+ number_format(($tax*100),2)); + + //if(isset($post['paymentmethodid'])){ + $users = $this->users->getUsers('row', ['users'],['id' => $post['user_id']]); + $userid = $users['id']; + $name = $users['name']; + $email = $users['email']; + $stripecustomerid = $users['stripe_customer_id']; + + $customerid = $this->stripe->customer($userid, $name, $email, $stripecustomerid); + + $piid = $this->stripe->createPaymentIntents($customerid, $totaldue, $transactionfee=0, $accountid=''); + //} $resultdata = [ 'event_id' => $event_id, 'event_name' => $event_name, @@ -195,6 +211,7 @@ public function index() 'timer' => $timer, 'count' => $count, 'paymentmethod' => $paymentmethod, + 'piid' => $piid['id'], 'stripepublickey' => $setting['stripepublickey'], ]; @@ -350,4 +367,26 @@ public function action(){ die(); } + + public function stripesecretkey() + { + $requestData = $this->request->getPost(); + if(isset($requestData['paymentmethodid'])){ + $users = $this->users->getUsers('row', ['users'],['id' => $requestData['user_id']]); + $userid = $users['id']; + $name = $users['name']; + $email = $users['email']; + $stripecustomerid = $users['stripe_customer_id']; + + $customerid = $this->stripe->customer($userid, $name, $email, $stripecustomerid); + + $resturnresult = $this->stripe->createPaymentIntents($customerid, $requestData['price'], $transactionfee=0, $accountid=''); + + + $json = ['1', 'Booking Successfully', $resturnresult['id']]; + }else{ + $json = ['0', 'Try Later', []]; + } + + } } \ No newline at end of file diff --git a/app/Controllers/Api/Myaccount/Currentreservation/Index.php b/app/Controllers/Api/Myaccount/Currentreservation/Index.php index c6b97a7..a7e2f88 100644 --- a/app/Controllers/Api/Myaccount/Currentreservation/Index.php +++ b/app/Controllers/Api/Myaccount/Currentreservation/Index.php @@ -194,8 +194,56 @@ public function paidunpaid(){ die; } - public function cancelsubscription(){ - $post = $this->request->getPost(); + public function striperefunds(){ + $post = $this->request->getPost(); + $validation = \Config\Services::validation(); + + $validation->setRules( + [ + 'paymentintentid' => 'required', + 'paymentid' => 'required', + 'id' => 'required', + 'amount' => 'required', + ], + + [ + 'paymentintentid' => [ + 'required' => 'paymentintentid Id is required.', + ], + 'paymentid' => [ + 'required' => 'Payment Id status is required.', + ], + 'id' => [ + 'required' => 'Id status is required.', + ], + 'amount' => [ + 'required' => 'amount status is required.', + ], + ] + ); + + if ($validation->withRequest($this->request)->run()) { + $data = $this->stripe->striperefunds($post); + if($data){ + $json = ['1', ' Reservation Cancelled.', $data]; + }else{ + $json = ['0', ' Try Later.', []]; + } + }else{ + $json = ['0', $validation->getErrors(), []]; + } + + echo json_encode([ + 'status' => $json[0], + 'message' => $json[1], + 'result' => $json[2], + ]); + + die; + } + + public function cancelsubscription(){ + $requestdata = $this->request->getPost(); $validation = \Config\Services::validation(); $validation->setRules( @@ -206,7 +254,7 @@ public function cancelsubscription(){ [ 'bookingid' => [ - 'required' => 'Booking Id is required.', + 'required' => 'paymentintentid Id is required.', ], 'paymentid' => [ 'required' => 'Payment Id status is required.', @@ -215,13 +263,15 @@ public function cancelsubscription(){ ); if ($validation->withRequest($this->request)->run()) { - - $payment = $this->payments->getPayments('row', ['payment'], ['id' => $post['paymentid']]); + $payment = $this->payments->getPayments('row', ['payment'], ['id' => $requestdata['paymentid']]); if($payment){ - $this->stripe->cancelSchedule($payment['stripe_subscription_id']); - $this->bookingdetails->cancelsubscription(['booking_details_id' => $payment['booking_details_id']]); - echo "fasd";die; - $json = ['1', ' Subscription Cancelled.']; + $data = $this->stripe->cancelSchedule($payment['stripe_scheduled_id']); + $data = $this->bookingdetails->cancelsubscription(['booking_details_id' => $payment['booking_details_id']]); + if($data){ + $json = ['1', ' Subscription Cancelled.', $data]; + }else{ + $json = ['0', ' Try Later.', []]; + } }else{ $json = ['0', ' Try Later.', []]; }