resq/src/Service/PayMongoConnector.php

151 lines
4.6 KiB
PHP

<?php
namespace App\Service;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
use App\Entity\Customer;
class PayMongoConnector
{
protected $base_url;
protected $public_key;
protected $secret_key;
protected $hash;
public function __construct($base_url, $public_key, $secret_key)
{
$this->base_url = $base_url;
$this->public_key = $public_key;
$this->secret_key = $secret_key;
$this->hash = $this->generateHash();
}
public function createCheckout(Customer $cust, $items, $ref_no = null, $description = null, $success_url = null, $cancel_url = null, $metadata = [])
{
// build billing info
$billing = [
'name' => implode(" ", [$cust->getFirstName(), $cust->getLastName()]),
'phone' => $cust->getPhoneMobile(),
];
if ($cust->getEmail()) {
$billing['email'] = $cust->getEmail();
}
// build the request body
$body = [
'data' => [
'attributes' => [
'description' => $description,
'billing' => $billing,
// NOTE: this may be variable later, hardcoding for now
'payment_method_types' => [
'card',
'paymaya',
'gcash',
],
/* NOTE: format for line items:
* ['name', 'description', 'quantity', 'amount', 'currency']
*/
'line_items' => $items,
'reference_number' => (string)$ref_no,
'cancel_url' => $cancel_url,
'success_url' => $success_url,
'statement_descriptor' => $description,
'send_email_receipt' => true,
'show_description' => true,
'show_line_items' => false,
],
],
];
if (!empty($metadata)) {
$body['data']['attributes']['metadata'] = $metadata;
}
return $this->doRequest('/v1/checkout_sessions', 'POST', $body);
}
public function getCheckout($checkout_id)
{
return $this->doRequest('/v1/checkout_sessions/' . $checkout_id, 'GET');
}
protected function generateHash()
{
return base64_encode($this->secret_key);
}
protected function doRequest($url, $method, $request_body = [])
{
$client = new Client();
$headers = [
'Content-Type' => 'application/json',
'accept' => 'application/json',
'authorization' => 'Basic '. $this->hash,
];
try {
$response = $client->request($method, $this->base_url . '/' . $url, [
'json' => $request_body,
'headers' => $headers,
]);
} catch (RequestException $e) {
$error = ['message' => $e->getMessage()];
ob_start();
//var_dump($request_body);
$varres = ob_get_clean();
error_log($varres);
error_log("--------------------------------------");
error_log($e->getResponse()->getBody()->getContents());
error_log("PayMongo API Error: " . $error['message']);
error_log(Psr7\Message::toString($e->getRequest()));
// log this error
$this->log($url, Psr7\Message::toString($e->getRequest()), Psr7\Message::toString($e->getResponse()), 'error');
if ($e->hasResponse()) {
$error['response'] = Psr7\Message::toString($e->getResponse());
}
return [
'success' => false,
'error' => $error,
];
}
$result_body = $response->getBody();
// log response
$this->log($url, json_encode($request_body), $result_body);
return [
'success' => true,
'response' => json_decode($response->getBody(), true)
];
}
// TODO: make this more elegant
public function log($title, $request_body = "[]", $result_body = "[]", $type = 'api')
{
$filename = '/../../var/log/paymongo_' . $type . '.log';
$date = date("Y-m-d H:i:s");
// build log entry
$entry = implode("\r\n", [
$date,
$title,
"REQUEST:\r\n" . $request_body,
"RESPONSE:\r\n" . $result_body,
"\r\n----------------------------------------\r\n\r\n",
]);
@file_put_contents(__DIR__ . $filename, $entry, FILE_APPEND);
}
}