golf_backend/app/Libraries/SquareService.php
2023-05-17 17:37:28 +05:30

61 lines
1.8 KiB
PHP

<?php
namespace App\Libraries;
use Square\SquareClient;
use Square\Exceptions\ApiException;
use Square\Models\ChargeRequest;
class SquareService
{
private $client;
public function __construct()
{
// Initialize the Square client with your sandbox credentials
$this->client = new SquareClient([
'accessToken' => getenv('SQUARE_ACCESS_TOKEN'),
'environment' => getenv('SQUARE_ENVIRONMENT') // Use 'sandbox' for testing
]);
}
public function processPayment($amount)
{
try {
// Create a charge request
$request = new ChargeRequest([
'amount_money' => [
'amount' => $amount,
'currency' => 'USD'
],
'source_id' => 'your-test-card-nonce' // Use a test card nonce for sandbox testing
]);
// Make the API call to charge the payment
$response = $this->client->getTransactionsApi()->charge(getenv('SQUARE_LOCATION_ID'), $request);
// Handle the payment response
// ...
return $response->getResult();
} catch (ApiException $e) {
// Handle API errors
return $e->getMessage();
// ...
}
}
public function getSquarePaymentForm()
{
// Create a payment form using the Square Payment Form library
$paymentForm = $this->client->getPaymentFormApi()->createPaymentForm([
'location_id' => getenv('SQUARE_LOCATION_ID'),
'amount_money' => [
'amount' => 100,
'currency' => 'USD'
],
'form_id' => 'your-form-id' // Unique ID for your payment form
]);
return $paymentForm->getResult();
}
}
?>