Skip to content

Instantly share code, notes, and snippets.

@manniru
Last active November 9, 2024 13:46
Show Gist options
  • Save manniru/d8584640b3bca6b46abde9a40cc30f86 to your computer and use it in GitHub Desktop.
Save manniru/d8584640b3bca6b46abde9a40cc30f86 to your computer and use it in GitHub Desktop.
<?php
{
"school_name": "ABC High School",
"school_id": "SCH123",
"email": "[email protected]",
"contact_person": "Jane Doe"
}
curl -X POST https://yoursite.com/manniredu/onboard-school \
-H "Content-Type: application/json" \
-d '{
"school_name": "ABC High School",
"school_id": "SCH123",
"email": "[email protected]",
"contact_person": "Jane Doe"
}'
{
"amount": "5000",
"regNo": "REG456",
"school_id": "SCH123",
"payment_channel": "WEB_PAY",
"accessKey": "hashed_key_example"
}
curl -X POST https://yoursite.com/manniredu/create-transaction \
-H "Content-Type: application/json" \
-d '{
"amount": "5000",
"regNo": "REG456",
"school_id": "SCH123",
"payment_channel": "WEB_PAY",
"accessKey": "hashed_key_example"
}'
<?php
/**
* Implements an endpoint for onboarding a school.
*/
function manniredu_onboard_school() {
// Ensure this is a POST request
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
return manniredu_json_output(['error' => 'Invalid request method. Only POST is allowed.'], 405);
}
// Get the JSON request data
$data = json_decode(file_get_contents('php://input'), TRUE);
// Validate required fields
$required_fields = ['school_name', 'school_id', 'email', 'contact_person'];
foreach ($required_fields as $field) {
if (empty($data[$field])) {
return manniredu_json_output(['error' => "The field $field is required."], 400);
}
}
// Add additional onboarding logic here (e.g., saving school data to the database)
// Respond with success and send a welcome email
$response = [
'status' => 'success',
'message' => 'School onboarded successfully. A welcome email has been sent with the API key.',
// Add additional response data as needed
];
return manniredu_json_output($response, 201);
}
/**
* Implements an endpoint for creating a transaction.
*/
function manniredu_create_transaction() {
// Ensure this is a POST request
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
return manniredu_json_output(['error' => 'Invalid request method. Only POST is allowed.'], 405);
}
// Get the JSON request data
$data = json_decode(file_get_contents('php://input'), TRUE);
// Validate required fields
$required_fields = ['amount', 'regNo', 'school_id', 'payment_channel', 'accessKey'];
foreach ($required_fields as $field) {
if (empty($data[$field])) {
return manniredu_json_output(['error' => "The field $field is required."], 400);
}
}
// Validate payment_channel
$allowed_channels = ['BANK_BRANCH', 'WEB_PAY'];
if (!in_array($data['payment_channel'], $allowed_channels)) {
return manniredu_json_output(['error' => 'Invalid payment channel. Use either BANK_BRANCH or WEB_PAY.'], 400);
}
// Access key validation (example hashing)
$raw_data = $data['amount'] . $data['regNo'] . $data['school_id'] . $data['redirect_url'] . API_KEY;
$hashed_key = hash('sha512', $raw_data);
if ($hashed_key !== $data['accessKey']) {
return manniredu_json_output(['error' => 'Invalid access key.'], 401);
}
// Add transaction creation logic here (e.g., saving transaction data to the database)
// Respond with success
$response = [
'status' => 'success',
'txnRef' => uniqid('txn_'), // Generate unique transaction reference
// Add additional response data as needed
];
return manniredu_json_output($response, 201);
}
/**
* Outputs a JSON response.
*
* @param array $data
* The response data.
* @param int $status_code
* The HTTP status code.
*/
function manniredu_json_output(array $data, $status_code = 200) {
drupal_set_header('Content-Type: application/json');
drupal_set_header("HTTP/1.1 $status_code");
print json_encode($data);
drupal_exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment