189 lines
6.1 KiB
PHP
189 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use Square\SquareClient;
|
|
use Square\Models\Money;
|
|
use Square\Models\CreatePaymentRequest;
|
|
use Square\Exceptions\ApiException;
|
|
use Ramsey\Uuid\Uuid;
|
|
use App\Models\transaction_model;
|
|
use App\Models\Member_model;
|
|
use App\Models\Dashboard_model;
|
|
use App\Models\Common_model;
|
|
use App\Models\Member_credits_model;
|
|
use App\Models\Packages_and_pricing_model;
|
|
use App\Models\Cart_model;
|
|
use App\Models\Credits_usage_tracking_model;
|
|
|
|
class PaymentController extends BaseController
|
|
{
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$this->session = session();
|
|
$this->MemberModel = new Member_model();
|
|
$this->dModel = new Dashboard_model();
|
|
$this->cModel = new Common_model();
|
|
$this->creditsModel = new Member_credits_model();
|
|
$this->PackagesModel = new Packages_and_pricing_model();
|
|
$this->cartModel = new Cart_model();
|
|
$this->CreditsUsageTrackingModel = new Credits_usage_tracking_model();
|
|
$this->TransactionModel = new transaction_model();
|
|
|
|
}
|
|
|
|
|
|
public function index()
|
|
{
|
|
|
|
if($this->request->getmethod() == 'get')
|
|
{
|
|
echo 'EGF'.rand(10,100).rand(10,100).rand(10,100);
|
|
} else if($this->request->getmethod() == 'post') {
|
|
|
|
$count = count($this->request->getVar('pack_id'));
|
|
|
|
$amount = 0;
|
|
for($i=0;$i<$count;$i++)
|
|
{
|
|
|
|
$packageData = $this->PackagesModel->where('id',$this->request->getVar('pack_id')[$i])->find();
|
|
|
|
$today = date("Y-m-d");
|
|
$cost = $packageData[0]['cost'] * $this->request->getVar('count')[$i];
|
|
|
|
$credits_data['member_id'] = session()->get('logged_user');
|
|
$credits_data['package_name'] = $packageData[0]['package_name'];
|
|
$credits_data['package_type'] = $packageData[0]['package_type'];
|
|
$credits_data['credit_points'] = $packageData[0]['credit'];
|
|
$credits_data['running_balance'] = $packageData[0]['credit'];
|
|
$credits_data['expired_points'] = 0;
|
|
$credits_data['expiring_date'] = date('Y-m-d', strtotime('+1 month', strtotime($today)));
|
|
$credits_data['cost'] = $packageData[0]['cost'];
|
|
$credits_data['is_active'] = 0;
|
|
|
|
$single_pack_no_of_count = $this->request->getVar('count')[$i];
|
|
for($j=0;$j<$single_pack_no_of_count;$j++)
|
|
{
|
|
$insert_data = $this->creditsModel->save($credits_data);
|
|
}
|
|
|
|
$amount = $amount + $cost;
|
|
}
|
|
$data['amount'] = $amount;
|
|
|
|
echo view('payment_form',$data);
|
|
}
|
|
|
|
|
|
}
|
|
public function payment_form()
|
|
{
|
|
return view('payment_form');
|
|
}
|
|
|
|
public function processPayment()
|
|
{
|
|
try {
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
|
|
error_log('Received a non-POST request');
|
|
echo 'Request not allowed';
|
|
http_response_code(405);
|
|
return;
|
|
}
|
|
|
|
include 'App\Libraries\LocationInfo.php';
|
|
|
|
$json = file_get_contents('php://input');
|
|
$data = json_decode($json);
|
|
|
|
$token = $data->token;
|
|
$idempotencyKey = $data->idempotencyKey;
|
|
$amount = $data->amount;
|
|
|
|
|
|
$square_client = new SquareClient([
|
|
'accessToken' => getenv('SQUARE_ACCESS_TOKEN'),
|
|
'environment' => getenv('ENVIRONMENT'),
|
|
'userAgentDetail' => 'golf_evo', // Remove or replace this detail when building your own app
|
|
]);
|
|
|
|
$payments_api = $square_client->getPaymentsApi();
|
|
|
|
// To learn more about splitting payments with additional recipients,
|
|
// see the Payments API documentation on our [developer site]
|
|
// (https://developer.squareup.com/docs/payments-api/overview).
|
|
|
|
$money = new Money();
|
|
// Monetary amounts are specified in the smallest unit of the applicable currency.
|
|
// This amount is in cents. It's also hard-coded for $1.00, which isn't very useful.
|
|
$money->setAmount($amount);
|
|
|
|
// Set currency to the currency for the location
|
|
$money->setCurrency($location_info->getCurrency());
|
|
|
|
//print_r($money); die();
|
|
|
|
try {
|
|
// Every payment you process with the SDK must have a unique idempotency key.
|
|
// If you're unsure whether a particular payment succeeded, you can reattempt
|
|
// it with the same idempotency key without worrying about double charging
|
|
// the buyer.
|
|
$create_payment_request = new CreatePaymentRequest($token, $idempotencyKey, $money);
|
|
$create_payment_request->setLocationId($location_info->getId());
|
|
|
|
$response = $payments_api->createPayment($create_payment_request);
|
|
|
|
if ($response->isSuccess()) {
|
|
echo json_encode($response->getResult());
|
|
|
|
|
|
$TrancactionData['member_id'] = 0;
|
|
$TrancactionData['transaction'] = json_encode($response->getResult());
|
|
$this->TransactionModel->save($TrancactionData);
|
|
|
|
|
|
} else {
|
|
echo json_encode(array('errors' => $response->getErrors()));
|
|
}
|
|
} catch (ApiException $e) {
|
|
echo json_encode(array('errors' => $e));
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Handle exceptions
|
|
|
|
// Example: Logging the exception
|
|
log_message('error', 'Exception: ' . $e->getMessage());
|
|
|
|
// Example: Displaying a custom error message
|
|
echo 'An exception occurred: ' . $e->getMessage();
|
|
} catch (\Error $e) {
|
|
// Handle errors
|
|
|
|
// Example: Logging the error
|
|
log_message('error', 'Error: ' . $e->getMessage());
|
|
|
|
// Example: Displaying a custom error message
|
|
echo 'An error occurred: ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|