mirror of
https://github.com/myronblair/tomtomgames-app
synced 2026-06-30 17:49:57 -05:00
116 lines
4.3 KiB
PHP
116 lines
4.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
class SquarePayment {
|
|
private string $baseUrl;
|
|
private string $token;
|
|
|
|
public function __construct() {
|
|
$this->token = SQUARE_ACCESS_TOKEN;
|
|
$this->baseUrl = SQUARE_ENV === 'production'
|
|
? 'https://connect.squareup.com/v2'
|
|
: 'https://connect.squareupsandbox.com/v2';
|
|
}
|
|
|
|
public function charge(
|
|
string $sourceId,
|
|
int $amountCents,
|
|
string $note = '',
|
|
string $cardholderName= '',
|
|
array $billingAddress= [],
|
|
string $buyerEmail = ''
|
|
): array {
|
|
$body = [
|
|
'idempotency_key' => uniqid('tg_', true),
|
|
'source_id' => $sourceId,
|
|
'amount_money' => ['amount' => $amountCents, 'currency' => 'USD'],
|
|
'location_id' => SQUARE_LOCATION_ID,
|
|
'note' => $note ?: 'TomGames Token Purchase',
|
|
'autocomplete' => true,
|
|
];
|
|
|
|
if ($cardholderName) {
|
|
$body['buyer_email_address'] = $buyerEmail ?: null;
|
|
}
|
|
if (!empty($billingAddress)) {
|
|
$body['billing_address'] = array_filter($billingAddress);
|
|
}
|
|
if ($buyerEmail && filter_var($buyerEmail, FILTER_VALIDATE_EMAIL)) {
|
|
$body['buyer_email_address'] = $buyerEmail;
|
|
$body['receipt_email'] = $buyerEmail;
|
|
}
|
|
|
|
$ch = curl_init($this->baseUrl . '/payments');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'Authorization: Bearer ' . $this->token,
|
|
'Square-Version: 2024-01-18',
|
|
],
|
|
CURLOPT_POSTFIELDS => json_encode(array_filter($body, fn($v) => $v !== null)),
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curlErr = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($curlErr) return ['success'=>false,'error'=>'Connection error. Please try again.'];
|
|
|
|
$data = json_decode($response, true);
|
|
|
|
if ($httpCode === 200 && isset($data['payment']['id'])) {
|
|
return [
|
|
'success' => true,
|
|
'payment_id' => $data['payment']['id'],
|
|
'status' => $data['payment']['status'],
|
|
'receipt_url'=> $data['payment']['receipt_url'] ?? null,
|
|
'card_brand' => $data['payment']['card_details']['card']['card_brand'] ?? null,
|
|
'last_4' => $data['payment']['card_details']['card']['last_4'] ?? null,
|
|
];
|
|
}
|
|
|
|
$errorMsg = $data['errors'][0]['detail'] ?? ($data['errors'][0]['code'] ?? 'Payment failed. Please try again.');
|
|
return ['success'=>false,'error'=>$errorMsg];
|
|
}
|
|
|
|
public static function sdkUrl(): string {
|
|
return SQUARE_ENV === 'production'
|
|
? 'https://web.squarecdn.com/v1/square.js'
|
|
: 'https://sandbox.web.squarecdn.com/v1/square.js';
|
|
}
|
|
|
|
// Generic POST for Square APIs (gift cards, etc.)
|
|
public static function post(string $path, array $body): array {
|
|
$baseUrl = SQUARE_ENV === 'production'
|
|
? 'https://connect.squareup.com'
|
|
: 'https://connect.squareupsandbox.com';
|
|
$url = $baseUrl . $path;
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($body),
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'Square-Version: 2024-01-18',
|
|
'Authorization: Bearer ' . SQUARE_ACCESS_TOKEN,
|
|
],
|
|
CURLOPT_TIMEOUT => 30,
|
|
]);
|
|
$resp = curl_exec($ch);
|
|
$httpCode= curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
if ($err) throw new Exception('Square connection error: ' . $err);
|
|
$data = json_decode($resp, true);
|
|
if (isset($data['errors'])) {
|
|
throw new Exception($data['errors'][0]['detail'] ?? 'Square API error');
|
|
}
|
|
return $data;
|
|
}
|
|
}
|