Skip to content

Instantly share code, notes, and snippets.

@HallexCosta
Last active May 8, 2023 23:43
Show Gist options
  • Save HallexCosta/b239bfe996d6b5d0d83e316c03bfe967 to your computer and use it in GitHub Desktop.
Save HallexCosta/b239bfe996d6b5d0d83e316c03bfe967 to your computer and use it in GitHub Desktop.
CheckoutService.php
<?php
namespace app\modules\api\services\pagarme\orders;
class CheckoutService
{
private string $SECRET_API_KEY = '';
private array $headers;
public function __construct()
{
$username = $this->SECRET_API_KEY;
$password = '';
$this->headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic ' . base64_encode($username . ':' . $password)
];
}
public function createOrderCheckoutPagarme($data)
{
$client = new \yii\httpclient\Client();
$amount = $data['amount'] * 100;
$checkoutData = [
"metadata" => $data['metadata'],
"items" => [
[
"amount" => $amount,
"description" => $data['checkout_info'],
"quantity" => "1"
]
],
"payments" => [
[
"amount" => $amount,
"payment_method" => "checkout",
"checkout" => [
"billing_address_editable" => false,
"accepted_payment_methods" => [
"credit_card",
"pix"
],
"pix" => [
"expires_in" => 60 * 60
]
// "success_url" => "https://www.youtube.com"
]
]
]
];
$checkoutData['payments'][0]['checkout']['customer_editable'] = (bool) $data['customer_editable'];
if (isset($data['customer_id'])) {
$checkoutData['customer_id'] = $data['customer_id'];
} else {
$checkoutData['customer'] = $data['customer'];
}
$response = $client->createRequest()
->setMethod('POST')
->setUrl('https://api.pagar.me/core/v5/orders')
->addHeaders($this->headers)
->setContent(json_encode($checkoutData))
->send();
return (object) [
'data' => json_decode($response->getContent()),
'status' => $response->getStatusCode()
];
}
}
<?php
$metadata = [
"event_id" => $eventId,
"event_offer_id" => $eventOfferId,
"event_date" => $date,
"start_time" => $startTime,
"end_time" => $endTime,
"contractor_id" => $contractorId,
"musician_id" => $musicianId
];
$data = [
'amount' => $amount,
'checkout_info' => $checkoutInfo,
'metadata' => $metadata
];
// if contractor don't has customer_id
if (empty($contractor->customer_id)) {
$customer = [
'code' => $contractor->id,// external_id reference
"name" => $contractor->full_name,
"email" => $contractor->email
];
$data['customer'] = $customer;
$data['customer_editable'] = true;
} else {
$data['customer_id'] = $contractor->customer_id;
$data['customer_editable'] = false;
}
$checkoutService = new CheckoutService();
$response = $checkoutService->createOrderCheckoutPagarme($data);
$orderResponse = $response->data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment