|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Examples\Payment; |
| 4 | + |
| 5 | +// Step 1: Require the Composer library |
| 6 | +require_once '../../vendor/autoload.php'; |
| 7 | + |
| 8 | +use MercadoPago\Client\Common\RequestOptions; |
| 9 | +use MercadoPago\Client\Payment\PaymentClient; |
| 10 | +use MercadoPago\Exceptions\MPApiException; |
| 11 | +use MercadoPago\MercadoPagoConfig; |
| 12 | + |
| 13 | +// Step 2: Set the production or sandbox access token |
| 14 | +MercadoPagoConfig::setAccessToken('<ACCESS_TOKEN>'); |
| 15 | +// Step 2.1 (optional - default is SERVER): Set your runtime enviroment from MercadoPagoConfig::RUNTIME_ENVIROMENTS |
| 16 | +// In case you want to test in your local machine first, set runtime enviroment to LOCAL |
| 17 | +MercadoPagoConfig::setRuntimeEnviroment(MercadoPagoConfig::LOCAL); |
| 18 | + |
| 19 | +// Step 3: Initialize the API client |
| 20 | +$client = new PaymentClient(); |
| 21 | + |
| 22 | +try { |
| 23 | + // Step 4: Create the request array |
| 24 | + $request = [ |
| 25 | + "transaction_amount" => 1000, |
| 26 | + "description" => "Product title", |
| 27 | + "payment_method_id" => "master", |
| 28 | + "token" => "{{CARD_TOKEN}}", |
| 29 | + "installments" => 1, |
| 30 | + "application_fee" => null, |
| 31 | + "binary_mode" => true, |
| 32 | + "capture" => true, |
| 33 | + "external_reference" => "Pedido01", |
| 34 | + "metadata" => new \stdClass(), |
| 35 | + "notification_url" => "https://www.mercadopago.com", |
| 36 | + "sponsor_id" => null, |
| 37 | + "statement_descriptor" => "LOJA 123", |
| 38 | + "payer" => [ |
| 39 | + "first_name" => "Name", |
| 40 | + "last_name" => "Surname", |
| 41 | + "email" => "{{EMAIL}}", |
| 42 | + "identification" => [ |
| 43 | + "number" => "{{DOCUMENT_NUMBER}}", |
| 44 | + "type" => "CPF" |
| 45 | + ], |
| 46 | + "phone" => [ |
| 47 | + "area_code" => "11", |
| 48 | + "number" => "{{PHONE_NUMBER}}" |
| 49 | + ], |
| 50 | + "address" => [ |
| 51 | + "street_name" => "Av. das Nações Unidas", |
| 52 | + "street_number" => "3003", |
| 53 | + "zip_code" => "206233-2002" |
| 54 | + ] |
| 55 | + ], |
| 56 | + "forward_data" => [ |
| 57 | + "sub_merchant" => [ |
| 58 | + "sub_merchant_id" => "1234183712", |
| 59 | + "mcc" => "5462", |
| 60 | + "country" => "BRA", |
| 61 | + "address_door_number"=> 123, |
| 62 | + "zip" => "2222222", |
| 63 | + "document_number" => "222222222222222", |
| 64 | + "city" => "SÃO PAULO", |
| 65 | + "address_street" => "RUA A", |
| 66 | + "legal_name" => "LOJINHA DO ZÉ", |
| 67 | + "region_code_iso" => "BR-MG", |
| 68 | + "region_code" => "BR", |
| 69 | + "document_type" => "CNPJ", |
| 70 | + "phone" => "{{PHONE}}", |
| 71 | + "url" => "www.mercadopago.com" |
| 72 | + ] |
| 73 | + ], |
| 74 | + "additional_info" => [ |
| 75 | + "items" => [ |
| 76 | + [ |
| 77 | + "id" => "1941", |
| 78 | + "title" => "25/08/2022 | Pista Inteira5 lote - GREEN VALLEY GRAMADO 2022", |
| 79 | + "description" => "25/08/2022 | Pista Inteira5 lote - GREEN VALLEY GRAMADO 2022", |
| 80 | + "category_id" => "Tickets", |
| 81 | + "quantity" => 1, |
| 82 | + "unit_price" => 1000.00, |
| 83 | + "event_date" => "2019-12-25T19:30:00.000-03:00", |
| 84 | + "picture_url" => "{{IMAGE_URL}}", |
| 85 | + "type" => "my_items_type", |
| 86 | + "warranty" => true, |
| 87 | + "category_descriptor" => [ |
| 88 | + "passenger" => [ |
| 89 | + "first_name" => "Guest name", |
| 90 | + "last_name" => "Guest surname", |
| 91 | + "identification"=> [ |
| 92 | + "type" => "DNI", |
| 93 | + "number" => "{{DOCUMENT}}" |
| 94 | + ] |
| 95 | + ], |
| 96 | + "route" => [ |
| 97 | + "arrival_date_time" => "2022-03-14T12:58:41.425-04:00", |
| 98 | + "company" => "Companhia", |
| 99 | + "departure" => "Osasco", |
| 100 | + "departure_date_time" => "2022-03-12T12:58:41.425-04:00", |
| 101 | + "destination" => "Sao Paulo" |
| 102 | + ] |
| 103 | + ] |
| 104 | + ] |
| 105 | + ], |
| 106 | + "payer" => [ |
| 107 | + "first_name" => "Name", |
| 108 | + "last_name" => "Surname", |
| 109 | + "is_prime_user" => true, |
| 110 | + "is_first_purchase_online" => true, |
| 111 | + "last_purchase" => "2019-10-25T19:30:00.000-03:00", |
| 112 | + "phone" => [ |
| 113 | + "area_code" => "11", |
| 114 | + "number" => "{{PHONE_NUMBER}}" |
| 115 | + ], |
| 116 | + "address" => [ |
| 117 | + "street_name" => "Av. das Nações Unidas", |
| 118 | + "street_number" => "3003", |
| 119 | + "zip_code" => "206233-2002" |
| 120 | + ], |
| 121 | + "registration_date" => "2020-08-06T09:25:04.000-03:00", |
| 122 | + "authentication_type" => "Gmail" |
| 123 | + ], |
| 124 | + "shipments" => [ |
| 125 | + "local_pickup" => "1", |
| 126 | + "express_shipment" => true, |
| 127 | + "receiver_address" => [ |
| 128 | + "zip_code" => "306233-2003", |
| 129 | + "street_name" => "Av. das Nações Unidas", |
| 130 | + "street_number"=> "3003", |
| 131 | + "floor" => "5", |
| 132 | + "apartment" => "502", |
| 133 | + "state_name" => "DF", |
| 134 | + "city_name" => "Bogota" |
| 135 | + ] |
| 136 | + ] |
| 137 | + ], |
| 138 | + ]; |
| 139 | + |
| 140 | + // Step 5: Create the request options, setting X-Idempotency-Key |
| 141 | + $request_options = new RequestOptions(); |
| 142 | + $request_options->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]); |
| 143 | + |
| 144 | + // Step 6: Make the request |
| 145 | + $payment = $client->create($request, $request_options); |
| 146 | + echo "Payment ID: " . $payment->id . "\n"; |
| 147 | + echo "Status: " . $payment->status . "\n"; |
| 148 | + |
| 149 | + // Step 7: Handle exceptions |
| 150 | +} catch (MPApiException $e) { |
| 151 | + echo "Status code: " . $e->getApiResponse()->getStatusCode() . "\n"; |
| 152 | + echo "Content: "; |
| 153 | + var_dump($e->getApiResponse()->getContent()); |
| 154 | + echo "\n"; |
| 155 | +} catch (\Exception $e) { |
| 156 | + echo $e->getMessage(); |
| 157 | +} |
0 commit comments