// Import required PayPal classes at the beginning use PayPal\Api\Payer; use PayPal\Api\Amount; use PayPal\Api\Item; use PayPal\Api\ItemList; use PayPal\Api\Transaction; use PayPal\Api\Payment; use PayPal\Api\RedirectUrls; use PayPal\Auth\OAuthTokenCredential; use PayPal\Rest\ApiContext; // PayPal API context setup $apiContext = new ApiContext( new OAuthTokenCredential( 'AUbibg3Irl5DseZvbOcXpV9PoSI1gMwO4MOvtO2fPb7koBg_czAstXw0_kSvqBMavguV1d_RnVuOfoxo', // ClientID 'EK1MYIy-2G91pmoUZfdjejxquyD9LWSx0kpSuz5Arusw63F1w5zFBvXxUeUxZJVj4lUp9uxcWME3mo1b' // ClientSecret ) ); $apiContext->setConfig(['mode' => 'live']); // Set mode to live for production // Define the item being purchased $item = new Item(); $item->setName("Aya Blocks Ghana Game App") ->setDescription("A fun and educational game app inspired by Ghanaian culture.") ->setCurrency("USD") ->setQuantity(1) ->setPrice("2.99"); // Price per item // Create an item list and add the item $itemList = new ItemList(); $itemList->setItems([$item]); // Define the payment amount $amount = new Amount(); $amount->setCurrency("USD")->setTotal("2.99"); // Create a transaction and add the item list to it $transaction = new Transaction(); $transaction->setAmount($amount) ->setItemList($itemList) ->setDescription("Purchase of Aya Blocks Ghana Game App"); // Set up payer, redirect URLs, and payment $payer = new Payer(); $payer->setPaymentMethod("paypal"); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl("https://ayablocks.com/success.php") ->setCancelUrl("https://ayablocks.com/cancel.php"); $payment = new Payment(); $payment->setIntent("sale") ->setPayer($payer) ->setTransactions([$transaction]) ->setRedirectUrls($redirectUrls); try { // Create the payment on PayPal $payment->create($apiContext); $approvalUrl = $payment->getApprovalLink(); header("Location: $approvalUrl"); exit(); } catch (Exception $ex) { echo "Error: " . $ex->getMessage(); exit(); }