May Sale! All accounts are 40% off this week.

Norbertho's avatar

Payment API callback not working

Hi,

API i try to use is: https://www.paybear.io/

I can manage to get a payment address and invoice number from an api the and save it into database during user registration. But when I waiting for the payment confirmation callback it doesn't want to work. I would like to get a callback with the invoice number so i can compare my saved invoice number to the callbacks invoice number and if they are match then i can update my database that the customer paid.

Here is the working part where i can receive and save the generated invoice number and account number:

      $orderId = $user->slug;
        $secAPIKEY = 'secjjjjj5cd07jjj553kkkkk07ffa';
        $token = $user->paymentSelected;
        $callbackUrl = 'http://104.000.000.00/payment/update/'.$orderId;

          $url = sprintf('https://api.paybear.io/v2/%s/payment/%s?token=%s', $token, urlencode($callbackUrl), $secAPIKEY);

          if ($response = file_get_contents($url)) {
            $response = json_decode($response);

            if (isset($response->data->address)) {
              $invoice = $response->data->invoice;
              $addresstopay = $response->data->address;
            }
          }
          echo null;

        $payment = new Payment();
        $payment->invoice = $invoice;
        $payment->amount = $user->mennyit;
        $payment->address = $addresstopay;
        $payment->icoslug = $user->slug;
        $payment->mit = $user->mit;
        $payment->cim = $user->cim;
        $payment->mennyit = $user->mennyit;
        $payment->save();
        return $user;

Here is my web php

Route::get('/payment/update/{icoslug}', 'CallbackController@update');

And here is the controller to process the callback

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Payment;
use DB;

class CallbackController extends Controller
{
  public function update(Request $request, $icoslug)
  {

    $payment = Payment::whereIcoslug($icoslug)->firstOrFail();

    $invoicementett = $payment->invoice;

    $data = file_get_contents('php://input');
    if ($data) {
        $params = json_decode($data);
        $invoice = $params->invoice;

    if ($invoicementett == $invoice) {
          $payment->update();
          $payment->amount = 'paid';
          $payment->save();

      return $invoice;
    }
        }
            die("waiting for confirmations");
  }
}

I have also edited my VerifyCsrfToken.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'https://api.paybear.io/*',
        'https://api.paybear.io/v2/',
    ];
}
0 likes
3 replies
Norbertho's avatar
Norbertho
OP
Best Answer
Level 6

Just for further reference i have found the solution. Hope it will help to someone else.

Here is my web.php has to be post

Route::post('/payment/update/{icoslug}', 'CallbackController@update');

VerifyCsrfToken.php has to be like this for this route

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [

      '/payment/update/*',

    ];
}

Now the callback woks as expected

here is the contoroller

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use App\User;
use App\Payment;
use DB;

class CallbackController extends Controller
{
  public function update(Request $request, $icoslug)
  {

    $payment = Payment::whereIcoslug($icoslug)->firstOrFail();
    $user = User::where('slug', $icoslug)->firstOrFail();

    $invoicementett = $payment->invoice;

    $data = file_get_contents('php://input');

    if ($data) {
        $params = json_decode($data);
        $invoice = $params->invoice;

    if ($invoicementett = $invoice) {
          $payment->update();
          $payment->amount = 'fizetve';
          $payment->save();

          $user->update();
          $user->activated = 'actve';
          $user->save();

      return $invoice;
    }
        }
            die("waiting for confirmations");
  }
}

3 likes
JohnyIrush's avatar

@Norbertho Thanks a lot, I am integrating payment Gateway and I have struggled for days till I found this solution.

1 like
13en's avatar

Hey Norbertho, I'm currently trying to get paybear.io to work can you share your payments/invoices model/controller/migrations please? so i can make the connection between them

Please or to participate in this conversation.