1. Go to this page and download the library: Download devinweb/laravel-youcan-pay library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
devinweb / laravel-youcan-pay example snippets
use Devinweb\LaravelYouCanPay\Traits\Billable;
class User extends Authenticatable
{
use Billable;
}
use App\Models\Core\User;
use Devinweb\LaravelYouCanPay\LaravelYouCanPay;;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
LaravelYouCanPay::useCustomerModel(User::class);
}
/**
* Get the customer info to send them when we generate the form token.
*
* @return array
*/
public function getCustomerInfo()
{
return [
'name' => $this->name,
'address' => '',
'zip_code' => '',
'city' => '',
'state' => '',
'country_code' => 'MA',
'phone' => $this->phone,
'email' => $this->email,
];
}
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
$user = LaravelYouCanPay::findBillable($order_id);
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
use Illuminate\Support\Str;
public function tokenization(Request $request)
{
$order_data = [
'order_id' => (string) Str::uuid(),
'amount' => 200
];
$token= LaravelYouCanPay::createTokenization($order_data, $request)->getId();
$public_key = config('youcanpay.public_key');
$isSandbox = config('youcanpay.sandboxMode');
$language = config('app.locale');
// You can at this point share a lot of data with the front end,
// the idea behind this is making the backend manage the environment keys,
// we don't need to store the keys in many places.
return response()->json(compact('token', 'public_key', 'isSandbox', 'language'));
}
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
$customerInfo = [
'name' => '',
'address' => '',
'zip_code' => '',
'city' => '',
'state' => '',
'country_code' => '',
'phone' => '',
'email' => '',
];
$metadata = [
// Can you insert what you want here...
'key' => 'value'
];
$token= LaravelYouCanPay::setMetadata($metadata)
->setCustomerInfo($customerInfo)
->createTokenization($data, $request)->getId();
> protected $except = [
> 'youcanpay/*',
> ]
>
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class WebHookController extends Controller
{
/**
* Create a new WebhookController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('verify-youcanpay-webhook-signature');
}
//...
}
namespace App\Listeners;
use Devinweb\LaravelYouCanPay\Events\WebhookReceived;
class YouCanPayEventListener
{
/**
* Handle received Stripe webhooks.
*
* @param \Devinweb\LaravelYouCanPay\Events\WebhookReceived $event
* @return void
*/
public function handle(WebhookReceived $event)
{
if ($event->payload['event_name'] === 'transaction.paid') {
// Handle the incoming event...
}
}
}
namespace App\Providers;
use App\Listeners\YouCanPayEventListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Devinweb\LaravelYouCanPay\Events\WebhookReceived;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
WebhookReceived::class => [
YouCanPayEventListener::class,
],
];
}
namespace App\Http\Controllers;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
use Illuminate\Http\Request;
class YouCanPayWebhooksController extends Controller
{
public function handle(Request $request)
{
$signature = $request->header('x-youcanpay-signature');
$payload = json_decode($request->getContent(), true);
if (LaravelYouCanPay::verifyWebhookSignature($signature, $payload)) {
// you code here
}
}
}
namespace App\Http\Controllers;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
use Illuminate\Http\Request;
class YouCanPayWebhooksController extends Controller
{
public function handle(Request $request)
{
LaravelYouCanPay::validateWebhookSignature($signature, $payload)
// you code here
}
}