|
10 | 10 | use Illuminate\Http\Request; |
11 | 11 | use Illuminate\Support\Facades\Auth; |
12 | 12 | use Illuminate\Support\Facades\Http; |
| 13 | +use Illuminate\Support\Str; |
13 | 14 | use Mail; |
14 | 15 |
|
15 | 16 | class PagarmeController extends Controller |
16 | 17 | { |
| 18 | + private const CODANDO_COM_IA_PRICE_IN_CENTS = 58800; |
| 19 | + private const CODANDO_COM_IA_PRODUCT_SLUG = 'curso-ao-vivo-codando-com-ia-v1'; |
| 20 | + |
17 | 21 | public function __construct() |
18 | 22 | { |
19 | | - $this->middleware('auth:sanctum'); |
| 23 | + $this->middleware('auth:sanctum')->only([ |
| 24 | + 'createOrderAndGetCheckoutLink', |
| 25 | + 'getSubscriptionByPagarmeOrderId', |
| 26 | + ]); |
| 27 | + } |
| 28 | + |
| 29 | + public function createCodandoComIaCheckout(Request $request) |
| 30 | + { |
| 31 | + $validated = $request->validate([ |
| 32 | + 'name' => 'required|string|max:255', |
| 33 | + 'email' => 'required|email', |
| 34 | + 'phone' => 'required|string|max:30', |
| 35 | + 'tag' => 'nullable|string|max:255', |
| 36 | + ]); |
| 37 | + |
| 38 | + $rawPhone = preg_replace('/\D/', '', $validated['phone']); |
| 39 | + |
| 40 | + if (Str::startsWith($rawPhone, '55') && strlen($rawPhone) > 11) { |
| 41 | + $rawPhone = substr($rawPhone, 2); |
| 42 | + } |
| 43 | + |
| 44 | + $phonesPayload = null; |
| 45 | + |
| 46 | + if (strlen($rawPhone) >= 10) { |
| 47 | + $areaCode = substr($rawPhone, 0, 2); |
| 48 | + $number = substr($rawPhone, 2); |
| 49 | + |
| 50 | + $phonesPayload = [ |
| 51 | + 'country_code' => '55', |
| 52 | + 'area_code' => $areaCode, |
| 53 | + 'number' => $number, |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + $customerPayload = [ |
| 58 | + 'name' => $validated['name'], |
| 59 | + 'email' => $validated['email'], |
| 60 | + 'type' => 'individual', |
| 61 | + 'metadata' => [ |
| 62 | + 'product_slug' => self::CODANDO_COM_IA_PRODUCT_SLUG, |
| 63 | + 'lead_tag' => $validated['tag'] ?? null, |
| 64 | + ], |
| 65 | + ]; |
| 66 | + |
| 67 | + if ($phonesPayload) { |
| 68 | + $customerPayload['phones'] = [ |
| 69 | + 'mobile_phone' => $phonesPayload, |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + $installments = collect(range(1, 12))->map(function ($number) { |
| 74 | + return [ |
| 75 | + 'number' => $number, |
| 76 | + 'total' => self::CODANDO_COM_IA_PRICE_IN_CENTS, |
| 77 | + ]; |
| 78 | + })->values()->all(); |
| 79 | + |
| 80 | + $checkoutPayload = [ |
| 81 | + 'code' => 'codando-com-ia-'.Str::uuid()->toString(), |
| 82 | + 'customer' => $customerPayload, |
| 83 | + 'items' => [ |
| 84 | + [ |
| 85 | + 'id' => 'codando-com-ia', |
| 86 | + 'amount' => self::CODANDO_COM_IA_PRICE_IN_CENTS, |
| 87 | + 'description' => 'Curso Codando com IA - Compra Única', |
| 88 | + 'quantity' => 1, |
| 89 | + 'code' => self::CODANDO_COM_IA_PRODUCT_SLUG, |
| 90 | + ], |
| 91 | + ], |
| 92 | + 'payments' => [ |
| 93 | + [ |
| 94 | + 'payment_method' => 'checkout', |
| 95 | + 'checkout' => [ |
| 96 | + 'customer_editable' => true, |
| 97 | + 'accepted_payment_methods' => [ |
| 98 | + 'credit_card', |
| 99 | + 'boleto', |
| 100 | + 'pix', |
| 101 | + ], |
| 102 | + 'success_url' => config('app.frontend_url').'/curso-ao-vivo/codando-com-ia/sucesso', |
| 103 | + 'pix' => [ |
| 104 | + 'expires_in' => 86400, |
| 105 | + ], |
| 106 | + 'boleto' => [ |
| 107 | + 'due_at' => Carbon::now() |
| 108 | + ->addDays(3) |
| 109 | + ->toIso8601String(), |
| 110 | + ], |
| 111 | + 'credit_card' => [ |
| 112 | + 'installments' => $installments, |
| 113 | + 'operation_type' => 'auth_and_capture', |
| 114 | + ], |
| 115 | + ], |
| 116 | + ], |
| 117 | + ], |
| 118 | + 'metadata' => [ |
| 119 | + 'product_slug' => self::CODANDO_COM_IA_PRODUCT_SLUG, |
| 120 | + 'lead_phone' => $validated['phone'], |
| 121 | + 'lead_tag' => $validated['tag'] ?? null, |
| 122 | + 'lead_email' => $validated['email'], |
| 123 | + 'lead_name' => $validated['name'], |
| 124 | + 'lead_phone_digits' => $phonesPayload |
| 125 | + ? $phonesPayload['area_code'].$phonesPayload['number'] |
| 126 | + : null, |
| 127 | + ], |
| 128 | + ]; |
| 129 | + |
| 130 | + $response = Http::withBasicAuth( |
| 131 | + config('services.pagarme.api_key'), |
| 132 | + '' |
| 133 | + )->post('https://api.pagar.me/core/v5/orders', $checkoutPayload); |
| 134 | + |
| 135 | + if ($response->failed()) { |
| 136 | + $status = $response->status(); |
| 137 | + $errorPayload = $response->json(); |
| 138 | + |
| 139 | + logger()->error('Pagarme order checkout creation failed', [ |
| 140 | + 'status' => $status, |
| 141 | + 'payload' => $checkoutPayload, |
| 142 | + 'response' => $errorPayload, |
| 143 | + 'response_body' => $response->body(), |
| 144 | + ]); |
| 145 | + |
| 146 | + return response()->json([ |
| 147 | + 'message' => 'Não foi possível iniciar o checkout no momento.', |
| 148 | + 'details' => $errorPayload, |
| 149 | + ], $status > 0 ? $status : 400); |
| 150 | + } |
| 151 | + |
| 152 | + $order = $response->json(); |
| 153 | + |
| 154 | + return [ |
| 155 | + 'checkoutLink' => $order['checkouts'][0]['payment_url'] ?? null, |
| 156 | + 'pagarmeOrderID' => $order['id'] ?? null, |
| 157 | + 'amount' => $order['amount'] ?? self::CODANDO_COM_IA_PRICE_IN_CENTS, |
| 158 | + 'status' => $order['status'] ?? null, |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + public function getCodandoComIaOrderStatus(string $orderId) |
| 163 | + { |
| 164 | + $endpoint = "https://api.pagar.me/core/v5/orders/{$orderId}"; |
| 165 | + |
| 166 | + $response = Http::withBasicAuth( |
| 167 | + config('services.pagarme.api_key'), |
| 168 | + '' |
| 169 | + )->get($endpoint); |
| 170 | + |
| 171 | + if ($response->failed()) { |
| 172 | + $status = $response->status(); |
| 173 | + |
| 174 | + return response()->json([ |
| 175 | + 'message' => 'Não foi possível recuperar o status do pedido.', |
| 176 | + ], $status > 0 ? $status : 400); |
| 177 | + } |
| 178 | + |
| 179 | + $order = $response->json(); |
| 180 | + $productSlug = $order['metadata']['product_slug'] ?? null; |
| 181 | + |
| 182 | + if ($productSlug !== self::CODANDO_COM_IA_PRODUCT_SLUG) { |
| 183 | + return response()->json(['message' => 'Pedido não encontrado'], 404); |
| 184 | + } |
| 185 | + |
| 186 | + return [ |
| 187 | + 'id' => $order['id'] ?? null, |
| 188 | + 'status' => $order['status'] ?? null, |
| 189 | + 'amount' => $order['amount'] ?? null, |
| 190 | + 'charges' => $order['charges'] ?? [], |
| 191 | + 'customer' => [ |
| 192 | + 'name' => $order['customer']['name'] ?? null, |
| 193 | + 'email' => $order['customer']['email'] ?? null, |
| 194 | + ], |
| 195 | + ]; |
20 | 196 | } |
21 | 197 |
|
22 | 198 | public function createOrderAndGetCheckoutLink(Request $request) |
|
0 commit comments