Hey there, fellow developer! Ready to dive into the world of cryptocurrency payments? You're in the right place. We're going to walk through integrating the Coinbase API into your PHP project using the nifty coinbase/coinbase-commerce package. This powerful combo will let you accept crypto payments like a pro. Let's get started!
Before we jump in, make sure you've got:
First things first, let's get that package installed. Fire up your terminal and run:
composer require coinbase/coinbase-commerce
Easy peasy, right? Composer's got your back.
Now, let's set things up:
putenv('COINBASE_COMMERCE_API_KEY=your_api_key_here');
Time to get our hands dirty! Let's initialize the Coinbase Commerce client:
use CoinbaseCommerce\Client; $client = Client::init('your_api_key_here');
Pro tip: Always wrap your API calls in try-catch blocks. The Coinbase API can throw exceptions, and we want to handle those gracefully.
Let's create a charge – this is how you'll accept payments:
$chargeData = [ 'name' => 'The Awesome Product', 'description' => 'Your customers will love it!', 'local_price' => [ 'amount' => '100.00', 'currency' => 'USD' ], 'pricing_type' => 'fixed_price' ]; try { $charge = $client->createCharge($chargeData); echo "Charge created with ID: " . $charge->id; } catch (\Exception $e) { echo "Oops! " . $e->getMessage(); }
Need to check on a charge? We've got you covered:
try { $charge = $client->getCharge('charge_id_here'); echo "Charge status: " . $charge->status; } catch (\Exception $e) { echo "Uh-oh! " . $e->getMessage(); }
Webhooks are your friends – they'll keep you updated on payment statuses:
use CoinbaseCommerce\Webhook; $rawBody = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_CC_WEBHOOK_SIGNATURE']; try { $event = Webhook::buildEvent($rawBody, $signature, 'your_webhook_secret'); // Process the event } catch (\Exception $e) { // Handle invalid signatures }
Want to create checkouts or manage products? The coinbase/coinbase-commerce package has got you covered. Check out the documentation for more details – there's a whole world of possibilities!
A few quick tips to keep you on the right track:
Running into issues? Don't sweat it! Here are some common hiccups:
And there you have it! You're now equipped to integrate Coinbase payments into your PHP project. Remember, the official Coinbase Commerce documentation is your best friend for diving deeper. Now go forth and crypto-fy those payments!
Happy coding, and may your transactions always confirm quickly! 🚀