135 lines
3.7 KiB
PHP
135 lines
3.7 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;
|
|
|
|
|
|
class PaymentController extends BaseController
|
|
{
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$this->TransactionModel = new transaction_model();
|
|
|
|
}
|
|
|
|
|
|
public function index()
|
|
{
|
|
|
|
|
|
// Load the payment form view
|
|
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();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|