BESTFY Pagamentos (Payment) is a Brazilian payment gateway that allows merchants to accept payments using a variety of methods, including Pix, Boleto, and credit cards. In this tutorial, we will walk you through the procedures of integrating the BESTFY Pagamentos gateway into your PHP application.
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
BESTFY Pagamentos Gateway Integration
Before we begin, please ensure you have the following:
- BESTFY’s API credentials: To authenticate your requests, you’ll need the API key provided by BESTFY.
- PHP Environment: Make sure you have PHP 7.2 or higher installed.
- Composer: We’ll use Composer to install the Guzzle HTTP client, which will handle API requests.
1. Setting up the BESTFY API with Guzzle HTTP
BESTFY provides a REST API for creating transactions, handling payments, and receiving postback notifications. To make HTTP requests from PHP, we will use Guzzle, a popular PHP HTTP client.
Install Guzzle via Composer
If you haven’t already installed Guzzle, run the following command:
2 3 4 |
composer require guzzlehttp/guzzle |
This will install Guzzle and all necessary dependencies.
Suggested Read: Authorize.Net Payment Gateway Integration using PHP
Initialize Guzzle Client for BESTFY
Next, set up a Guzzle client in your PHP script to interact with the BESTFY API. Here’s how you can configure it:
2 3 4 5 6 7 8 9 |
require 'vendor/autoload.php'; use GuzzleHttp\Client; $client = new Client(); |
You can now use this client to make API requests.
2. Creating a Transaction in BESTFY
To accept payments through BESTFY, the first step is to create a transaction or checkout session. This will generate a payment URL that you can redirect the user to for completing the payment.
API Request to Create a Transaction
Here’s how you can make a POST request to the BESTFY API to create a transaction:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
$secretKey = 'YOUR_API_KEY'; $authorization = 'Basic ' . base64_encode("{$secretKey}:x"); $response = $client->request('POST', 'https://api.bestfybr.com.br/v1/checkouts', [ 'headers' => [ 'accept' => 'application/json', 'authorization' => $authorization, // Replace with your API key 'content-type' => 'application/json', ], 'body' => json_encode([ 'settings' => [ 'requestAddress' => false, 'requestPhone' => false, 'traceable' => false, 'boleto' => [ 'enabled' => true, 'expiresInDays' => 1 ], 'pix' => [ 'enabled' => true, 'expiresInDays' => 1 ], 'card' => [ 'enabled' => true, 'freeInstallments' => 0, 'maxInstallments' => 0 ], 'defaultPaymentMethod' => 'card' ], 'amount' => 500, 'postbackUrl' => 'https://yourdomain.com/postback.php', 'items' => [ [ 'tangible' => true, 'title' => 'Test', 'unitPrice' => 500, 'quantity' => 1 ] ] ]) ]); $checkoutResponse = json_decode($response->getBody(), true); $checkoutUrl = $checkoutResponse['secureUrl']; |
In this code:
- Replace
YOUR_API_KEY
with your actual BESTFY API key. - The
amount
field represents the total amount in cents (for example, 10000 cents = R$100.00). postbackUrl
is where BESTFY will send the payment status after processing. We’ll handle this later.
Once the request is successful, BESTFY will return a checkoutUrl
that you can redirect the user to for completing the payment.
Also Read: How to Integrate Paypal Payment System with Paypal API in PHP and MySql
Redirect User to BESTFY Checkout Page
After receiving the checkoutUrl
, you can redirect the user to that URL for payment:
2 3 4 5 |
header('Location: ' . $checkoutUrl); exit; |
3. Handling Postback and Order Status Updates
BESTFY will notify your server about the payment status via a postback request to the postbackUrl
you specified when creating the transaction.
Handling the Postback Request
Create a script at the postback URL (e.g., postback.php
) to handle the postback data. BESTFY will send a JSON payload with the transaction details and payment status.
Here’s how to handle the postback:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
// postback.php // Retrieve the postback data $postbackData = json_decode(file_get_contents('php://input'), true); // Example of what $postbackData might contain /* $postbackData = Array ( [id] => 686401 [type] => transaction [objectId] => 282 [url] => https://test.com [data] => Array ( [id] => 282 [amount] => 10000 [refundedAmount] => 0 [companyId] => 2 [installments] => 12 [paymentMethod] => credit_card [status] => paid [postbackUrl] => [metadata] => [traceable] => [secureId] => a4594817-be48-4a23-81aa-4bb01f95fe78 [secureUrl] => https://link.compra.com.br/pagar/a4594817-be48-4a23-81aa-4bb01f95fe78 [createdAt] => 2022-07-18T09:54:22.000Z [updatedAt] => 2022-07-18T09:54:22.000Z [paidAt] => 2022-07-18T09:54:22.000Z [ip] => [externalRef] => [customer] => Array ( [id] => 1 [externalRef] => [name] => Gabryel [email] => gabryel@hotmail.com [phone] => 11999999999 [birthdate] => [createdAt] => 2022-05-26T19:17:48.000Z [document] => Array ( [number] => 12345678910 [type] => cpf ) [address] => Array ( [street] => Rua República Argentina [streetNumber] => 4214 [complement] => [zipCode] => 11065030 [neighborhood] => Pompéia [city] => Santos [state] => SP [country] => BR ) ) [card] => Array ( [id] => 147 [brand] => visa [holderName] => GABRYEL FERREIRA [lastDigits] => 1111 [expirationMonth] => 3 [expirationYear] => 2028 [reusable] => 1 [createdAt] => 2022-07-17T18:08:11.000Z ) [boleto] => [pix] => [shipping] => [refusedReason] => [items] => Array ( [0] => Array ( [externalRef] => [title] => b456 [unitPrice] => 100 [quantity] => 1 [tangible] => ) ) [splits] => Array ( [0] => Array ( [recipientId] => 1 [amount] => 10000 [netAmount] => 9400 ) ) [refunds] => Array ( ) [delivery] => [fee] => Array ( [fixedAmount] => 200 [spreadPercentage] => 4 [estimatedFee] => 600 [netAmount] => 9400 ) ) ); */ if (isset($postbackData['data']['status'])) { $transactionId = $data['id']; // BESTFY transaction ID $paymentStatus = $postbackData['data']['status']; // 'success' or 'failed' // Handle success or failure if ($paymentStatus === 'paid') { // Payment success, update your order echo json_encode(['status' => 'received']); } else { // Payment failed, update your order echo json_encode(['status' => 'failed']); } } |
To store transaction information, such as the transaction ID and payment status, you can save it in your database using PHP’s database interaction functions.
Do you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
Conclusion
Integrating BESTFY Payment into your PHP application provides a seamless way to accept payments through various methods, including Pix, Boleto, and credit cards. By using Guzzle to communicate with the BESTFY API, you can easily create transactions, redirect users to secure payment pages, and handle postback notifications to update order statuses in real-time.
This integration enhances your checkout process, offering customers a smooth and secure experience. With careful implementation and testing, you can confidently provide a reliable payment solution that meets the needs of your business and customers alike.
Recommended Article: Integrate Recurring Stripe Subscription Payment with PHP
FAQs
BESTFY Payment Gateway is a Brazilian payment solution that enables merchants to accept payments through various methods, including Pix, Boleto, and credit cards. It provides a secure way to process transactions and offers multiple integration options for e-commerce websites and applications.
BESTFY supports multiple payment methods, including:
1. Pix (instant payment)
2. Boleto Bancário
3. Credit Cards
Yes, BESTFY provides a secure, SSL-encrypted payment page that customers are redirected to for completing their payments. This ensures that all payment information is transmitted securely.
BESTFY allows some level of customization, such as selecting which payment methods to offer (Pix, Boleto, credit card) and setting default payment methods. However, the checkout process itself is hosted by BESTFY, ensuring security and compliance with payment regulations.
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co