Square API Inquiry

You can either revert the changes you made to figure out where you broke it. Then make all the other necessary changes. Otherwise you can remove it from the form. :slightly_smiling_face:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Square\SquareClient;
use Square\Exceptions\ApiException;
use Square\Models\CreatePaymentRequest;
use Square\Models\Payment;
use Square\Models\CreatePaymentRequestSource;
use Square\Models\CreatePaymentRequestAmountMoney;
use Square\Environment;

class SquarepaymentController extends Controller
{
    protected $squareClient;

    public function __construct()
    {
        $this->squareClient = new SquareClient([
            'accessToken' => 'ACCESS_TOKEN',
            'environment' => Environment::SANDBOX, // Use SquareClient::ENVIRONMENT_PRODUCTION for production
        ]);
    }

    public function charge(Request $request)
    {
        $requestBody = [
            'source_id' => $request->input('nonce'), // Nonce from the Square payment form
            'amount_money' => [
                'amount' => $request->input('amount'), // Amount in the smallest denomination (e.g., cents)
                'currency' => 'USD', // Change this to match your currency
            ],
            'idempotency_key' => uniqid(), // Unique ID for the request
        ];

        try {
            $response = $this->squareClient->paymentsApi()->createPayment($requestBody);
            $payment = $response->getResult();
            // Handle successful payment
            return back()->with("success", "Payment is successful. Your payment id is: " . $payment->getPayment()->getId());
        } catch (ApiException $e) {
            // Handle API errors
            return back()->with("error", $e->getMessage());
        }
    }

    public function confirm(Request $request)
    {
        $paymentIntentId = $request->input('payment_intent');

        try {
            $response = $this->squareClient->paymentsApi()->completePayment($paymentIntentId);
            $payment = $response->getResult();
            // Handle successful payment confirmation
            return back()->with("success", "Payment is confirmed. Your payment id is: " . $payment->getPayment()->getId());
        } catch (ApiException $e) {
            // Handle API errors
            return back()->with("error", $e->getMessage());
        }
    }
}

error:Call to undefined method Square\SquareClient::paymentsApi()

In the future please don’t provide your access token. They are secret. All other values are safe to share.

The error message “Call to undefined method Square\SquareClient::paymentsApi()” indicates that the paymentsApi() method does not exist on the SquareClient object. The issue here is likely due to an incorrect method call.

public function charge(Request $request)
{
    // ... existing code ...

    try {
        // Use getPaymentsApi() instead of paymentsApi()
        $response = $this->squareClient->getPaymentsApi()->createPayment($requestBody);
        $payment = $response->getResult();
        // Handle successful payment
        return back()->with("success", "Payment is successful. Your payment id is: " . $payment->getId());
    } catch (ApiException $e) {
        // Handle API errors
        return back()->with("error", $e->getMessage());
    }
}

public function confirm(Request $request)
{
    $paymentIntentId = $request->input('payment_intent');

    try {
        // Use getPaymentsApi() instead of paymentsApi()
        $response = $this->squareClient->getPaymentsApi()->completePayment($paymentIntentId);
        $payment = $response->getResult();
        // Handle successful payment confirmation
        return back()->with("success", "Payment is confirmed. Your payment id is: " . $payment->getId());
    } catch (ApiException $e) {
        // Handle API errors
        return back()->with("error", $e->getMessage());
    }
}

:slightly_smiling_face: