Skip to content

Commit

Permalink
Add pending transactions (#2)
Browse files Browse the repository at this point in the history
* Add pending transactions + Handle transaction failed webhook event
  • Loading branch information
darbaoui committed Nov 28, 2022
1 parent 1d798ba commit 0b4d132
Show file tree
Hide file tree
Showing 28 changed files with 781 additions and 167 deletions.
67 changes: 42 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
- [Configuration](#configuration)
- [Billable Model](#Billable-Model)
- [YouCanPay Keys](#YouCanPay-Keys)
- [Customers](#Customers)
- [Retrieving Customers](#Retrieving-Customers)
- [Generate Token](#Generate-Token)
- [Generate Payment URL](#Generate-Payment-URL)
- [Usage](#Usage)
- [Customers](#Customers)
- [Retrieving Customers](#Retrieving-Customers)
- [Generate Token](#Generate-Token)
- [Generate Payment URL](#Generate-Payment-URL)
- [Tokenization](#Create-a-payment)
- [Get Token id](#Get-token-id)
- [Get Payment url](#Get-Payment-url)
Expand Down Expand Up @@ -75,20 +75,20 @@ If you want the package to manage the transactions based on the user model, add
This trait provides various methods to perform transaction tasks, such as creating a transaction, getting `paid`, `failed` and `pending` transactions.

```php
use Devinweb\LaravelYoucanPay\Traits\Billable;
use Devinweb\LaravelYouCanPay\Traits\Billable;

class User extends Authenticatable
{
use Billable;
}
```

LaravelYoucanPay assumes your user model will be `App\Models\User`, if you use a different user model namespace you should specify it using the method `useCustomerModel` method.
LaravelYouCanPay assumes your user model will be `App\Models\User`, if you use a different user model namespace you should specify it using the method `useCustomerModel` method.
This method should typically be called in the boot method of your `AppServiceProvider` class

```php
use App\Models\Core\User;
use Devinweb\LaravelYoucanPay\LaravelYoucanPay;;
use Devinweb\LaravelYouCanPay\LaravelYouCanPay;;

/**
* Bootstrap any application services.
Expand All @@ -97,7 +97,7 @@ use Devinweb\LaravelYoucanPay\LaravelYoucanPay;;
*/
public function boot()
{
LaravelYoucanPay::useCustomerModel(User::class);
LaravelYouCanPay::useCustomerModel(User::class);
}
```

Expand Down Expand Up @@ -147,9 +147,9 @@ You can retrieve a customer by their YouCanPay ID using the `findBillable` metho

```php

use Devinweb\LaravelYoucanPay\Facades\LaravelYoucanPay;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;

$user = LaravelYoucanPay::findBillable($order_id);
$user = LaravelYouCanPay::findBillable($order_id);

```

Expand Down Expand Up @@ -206,7 +206,7 @@ The first step we need is to create a token based on the credentails get it from
```php

use Devinweb\LaravelYoucanPay\Facades\LaravelYoucanPay;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
use Illuminate\Support\Str;


Expand All @@ -217,7 +217,7 @@ public function tokenization(Request $request)
'amount' => 200
];

$token= LaravelYoucanPay::createTokenization($order_data, $request)->getId();
$token= LaravelYouCanPay::createTokenization($order_data, $request)->getId();
$public_key = config('youcanpay.public_key');
$isSandbox = config('youcanpay.sandboxMode');
$language = config('app.locale');
Expand All @@ -236,7 +236,7 @@ public function tokenization(Request $request)
Standalone Integration, you can generate the payment url using the method `getPaymentUrl()`

```php
$paymentUrl= LaravelYoucanPay::createTokenization($data, $request)->getPaymentURL();
$paymentUrl= LaravelYouCanPay::createTokenization($data, $request)->getPaymentURL();
```

Then you can put that url in your html page
Expand All @@ -250,7 +250,7 @@ Then you can put that url in your html page
If you need to add the customer data during the tokenization, Please keep these array keys(`name`, `address`, `zip_code`, `city`, `state`, `country_code`, `phone` and `email`). you can use

```php
use Devinweb\LaravelYoucanPay\Facades\LaravelYoucanPay;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;

$customerInfo = [
'name' => '',
Expand All @@ -263,15 +263,15 @@ $customerInfo = [
'email' => '',
];

$token= LaravelYoucanPay::setCustomerInfo($customerInfo)->createTokenization($data, $request)->getId();
$token= LaravelYouCanPay::setCustomerInfo($customerInfo)->createTokenization($data, $request)->getId();
```

#### Metadata

You can use the metadata to send data that can be retrieved after the response or in the webhook.

```php
use Devinweb\LaravelYoucanPay\Facades\LaravelYoucanPay;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;

$customerInfo = [
'name' => '',
Expand All @@ -289,7 +289,7 @@ $metadata = [
'key' => 'value'
];

$token= LaravelYoucanPay::setMetadata($metadata)
$token= LaravelYouCanPay::setMetadata($metadata)
->setCustomerInfo($customerInfo)
->createTokenization($data, $request)->getId();
```
Expand Down Expand Up @@ -344,6 +344,23 @@ Then to display the form your logic it's will be looks like the code below

```

To start the payment you need an action, you can use a button

```javascript
document.getElementById("pay").addEventListener("click", function () {
// execute the payment
ycPay.pay(tokenId).then(successCallback).catch(errorCallback);
});

function successCallback(response) {
//your code here
}

function errorCallback(response) {
//your code here
}
```

For more information please check this [link](https://github.com/NextmediaMa/youcan-payment-php-sdk).

### Handling YouCanPay Webhooks
Expand Down Expand Up @@ -395,7 +412,7 @@ class WebHookController extends Controller

LaravelYouCanPay handles the common YouCanPay webhook events, if you need to handle the webhook events that you need you can listen to the event that is dispatched by the package.

- Devinweb\LaravelYoucanPay\Events\WebhookReceived
- Devinweb\LaravelYouCanPay\Events\WebhookReceived

You need to register a listener that can handle the event:

Expand All @@ -404,14 +421,14 @@ You need to register a listener that can handle the event:

namespace App\Listeners;

use Devinweb\LaravelYoucanPay\Events\WebhookReceived;
use Devinweb\LaravelYouCanPay\Events\WebhookReceived;

class YouCanPayEventListener
{
/**
* Handle received Stripe webhooks.
*
* @param \Devinweb\LaravelYoucanPay\Events\WebhookReceived $event
* @param \Devinweb\LaravelYouCanPay\Events\WebhookReceived $event
* @return void
*/
public function handle(WebhookReceived $event)
Expand All @@ -433,7 +450,7 @@ namespace App\Providers;

use App\Listeners\YouCanPayEventListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Devinweb\LaravelYoucanPay\Events\WebhookReceived;
use Devinweb\LaravelYouCanPay\Events\WebhookReceived;

class EventServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -509,7 +526,7 @@ To verify the webhook signature before processing any logic or action.

namespace App\Http\Controllers;

use Devinweb\LaravelYoucanPay\Facades\LaravelYoucanPay;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
use Illuminate\Http\Request;

class YouCanPayWebhooksController extends Controller
Expand All @@ -518,7 +535,7 @@ class YouCanPayWebhooksController extends Controller
{
$signature = $request->header('x-youcanpay-signature');
$payload = json_decode($request->getContent(), true);
if (LaravelYoucanPay::verifyWebhookSignature($signature, $payload)) {
if (LaravelYouCanPay::verifyWebhookSignature($signature, $payload)) {
// you code here
}
}
Expand All @@ -534,14 +551,14 @@ The validation has the same impact as the verification, but the validation throw

namespace App\Http\Controllers;

use Devinweb\LaravelYoucanPay\Facades\LaravelYoucanPay;
use Devinweb\LaravelYouCanPay\Facades\LaravelYouCanPay;
use Illuminate\Http\Request;

class YouCanPayWebhooksController extends Controller
{
public function handle(Request $request)
{
LaravelYoucanPay::validateWebhookSignature($signature, $payload)
LaravelYouCanPay::validateWebhookSignature($signature, $payload)

// you code here
}
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
},
"autoload": {
"psr-4": {
"Devinweb\\LaravelYoucanPay\\": "src",
"Devinweb\\LaravelYoucanPay\\Database\\Factories\\": "database/factories/"
"Devinweb\\LaravelYouCanPay\\": "src",
"Devinweb\\LaravelYouCanPay\\Database\\Factories\\": "database/factories/"
}
},
"autoload-dev": {
"psr-4": {
"Devinweb\\LaravelYoucanPay\\Tests\\": "tests"
"Devinweb\\LaravelYouCanPay\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -48,10 +48,10 @@
"extra": {
"laravel": {
"providers": [
"Devinweb\\LaravelYoucanPay\\Providers\\LaravelYoucanPayServiceProvider"
"Devinweb\\LaravelYouCanPay\\Providers\\LaravelYouCanPayServiceProvider"
],
"aliases": {
"LaravelYoucanPay": "Devinweb\\LaravelYoucanPay\\Facades\\LaravelYoucanPay"
"LaravelYouCanPay": "Devinweb\\LaravelYouCanPay\\Facades\\LaravelYouCanPay"
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions database/factories/TransactionFactory.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Devinweb\LaravelYoucanPay\Database\Factories;
namespace Devinweb\LaravelYouCanPay\Database\Factories;

use Devinweb\LaravelYoucanPay\Enums\YouCanPayStatus;
use Devinweb\LaravelYoucanPay\LaravelYoucanPay;
use Devinweb\LaravelYoucanPay\Models\Transaction;
use Devinweb\LaravelYouCanPay\Enums\YouCanPayStatus;
use Devinweb\LaravelYouCanPay\LaravelYouCanPay;
use Devinweb\LaravelYouCanPay\Models\Transaction;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

Expand All @@ -24,9 +24,10 @@ class TransactionFactory extends Factory
*/
public function definition()
{
$model = LaravelYoucanPay::$customerModel;
$model = LaravelYouCanPay::$customerModel;

return [
// 'id' => Str::uuid()->toString(),
(new $model)->getForeignKey() => ($model)::factory(),
'name' => 'default',
'order_id' => Str::random(40),
Expand Down
4 changes: 2 additions & 2 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Devinweb\LaravelYoucanPay\Database\Factories;
namespace Devinweb\LaravelYouCanPay\Database\Factories;

use Devinweb\LaravelYoucanPay\Tests\Fixtures\User;
use Devinweb\LaravelYouCanPay\Tests\Fixtures\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
public function up()
{
Schema::create('transactions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->uuid('id')->primary();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('name');
$table->string('order_id')->unique();
$table->string('youcanpay_id')->unique();
$table->string('youcanpay_id')->nullable();
$table->string('status');
$table->string('price')->nullable();
$table->string('refund')->nullable();
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Devinweb\LaravelYoucanPay\Http\Controllers\WebHookController;
use Devinweb\LaravelYouCanPay\Http\Controllers\WebHookController;
use Illuminate\Support\Facades\Route;

Route::post('webhook', [WebHookController::class, 'handleWebhook'])->name('webhook');
2 changes: 1 addition & 1 deletion src/Actions/CreateToken.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Devinweb\LaravelYoucanPay\Actions;
namespace Devinweb\LaravelYouCanPay\Actions;

use YouCan\Pay\YouCanPay;

Expand Down
2 changes: 1 addition & 1 deletion src/Enums/YouCanPayStatus.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Devinweb\LaravelYoucanPay\Enums;
namespace Devinweb\LaravelYouCanPay\Enums;

use Spatie\Enum\Enum;

Expand Down
2 changes: 1 addition & 1 deletion src/Events/WebhookReceived.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Devinweb\LaravelYoucanPay\Events;
namespace Devinweb\LaravelYouCanPay\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
Expand Down
21 changes: 21 additions & 0 deletions src/Facades/LaravelYouCanPay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Devinweb\LaravelYouCanPay\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @see \Devinweb\LaravelYouCanPay\Skeleton\SkeletonClass
*/
class LaravelYouCanPay extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'laravel-youcan-pay';
}
}
6 changes: 3 additions & 3 deletions src/Facades/LaravelYoucanPay.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Devinweb\LaravelYoucanPay\Facades;
namespace Devinweb\LaravelYouCanPay\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @see \Devinweb\LaravelYoucanPay\Skeleton\SkeletonClass
* @see \Devinweb\LaravelYouCanPay\Skeleton\SkeletonClass
*/
class LaravelYoucanPay extends Facade
class LaravelYouCanPay extends Facade
{
/**
* Get the registered name of the component.
Expand Down
Loading

0 comments on commit 0b4d132

Please sign in to comment.