-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-payment.php
More file actions
135 lines (117 loc) · 3.72 KB
/
process-payment.php
File metadata and controls
135 lines (117 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
/**
* Card Payment Processing Script
*
* This script demonstrates card payment processing using the Global Payments SDK.
* It handles tokenized card data and billing information to process payments
* securely through the Global Payments API.
*
* PHP version 7.4 or higher
*
* @category Payment_Processing
* @package GlobalPayments_Sample
* @author Global Payments
* @license MIT License
* @link https://github.com/globalpayments
*/
require_once 'vendor/autoload.php';
use Dotenv\Dotenv;
use GlobalPayments\Api\Entities\Address;
use GlobalPayments\Api\Entities\Exceptions\ApiException;
use GlobalPayments\Api\PaymentMethods\CreditCardData;
use GlobalPayments\Api\ServiceConfigs\Gateways\PorticoConfig;
use GlobalPayments\Api\ServicesContainer;
ini_set('display_errors', '0');
/**
* Configure the SDK
*
* Sets up the Global Payments SDK with necessary credentials and settings
* loaded from environment variables.
*
* @return void
*/
function configureSdk(): void
{
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$config = new PorticoConfig();
$config->secretApiKey = $_ENV['SECRET_API_KEY'];
$config->developerId = '000000';
$config->versionNumber = '0000';
$config->serviceUrl = 'https://cert.api2.heartlandportico.com';
ServicesContainer::configureService($config);
}
/**
* Sanitize postal code by removing invalid characters
*
* @param string|null $postalCode The postal code to sanitize
*
* @return string Sanitized postal code containing only alphanumeric
* characters and hyphens, limited to 10 characters
*/
function sanitizePostalCode(?string $postalCode): string
{
if ($postalCode === null) {
return '';
}
$sanitized = preg_replace('/[^a-zA-Z0-9-]/', '', $postalCode);
return substr($sanitized, 0, 10);
}
// Initialize SDK configuration
configureSdk();
try {
// Validate required fields
if (!isset($_POST['payment_token'], $_POST['billing_zip'], $_POST['amount'])) {
throw new ApiException('Missing required fields');
}
// Parse and validate amount
$amount = floatval($_POST['amount']);
if ($amount <= 0) {
throw new ApiException('Invalid amount');
}
// Initialize payment data using tokenized card information
$card = new CreditCardData();
$card->token = $_POST['payment_token'];
// Create billing address for AVS verification
$address = new Address();
$address->postalCode = sanitizePostalCode($_POST['billing_zip']);
// Process the payment transaction with specified amount
$response = $card->charge($amount)
->withAllowDuplicates(true)
->withCurrency('USD')
->withAddress($address)
->execute();
// Verify transaction was successful
if ($response->responseCode !== '00') {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => 'Payment processing failed',
'error' => [
'code' => 'PAYMENT_DECLINED',
'details' => $response->responseMessage
]
]);
exit;
}
// Return success response with transaction ID
echo json_encode([
'success' => true,
'message' => 'Payment successful! Transaction ID: ' . $response->transactionId,
'data' => [
'transactionId' => $response->transactionId
]
]);
} catch (ApiException $e) {
// Handle payment processing errors
http_response_code(400);
echo json_encode([
'success' => false,
'message' => 'Payment processing failed',
'error' => [
'code' => 'API_ERROR',
'details' => $e->getMessage()
]
]);
}