Skip to content

Commit

Permalink
init commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 11, 2020
0 parents commit 18c80e7
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[*.{vue,js,scss}]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.php export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
.php_cs export-ignore
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github: []
patreon: overtrue
custom: https://www.easywechat.com/img/pay/wechat.jpg
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.idea
*.DS_Store
/vendor
/coverage
/hooks
sftp-config.json
composer.lock
.subsplit
.php_cs.cache
26 changes: 26 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'blank_line_after_opening_tag' => true,
'braces' => ['allow_single_line_closure' => true],
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'function_typehint_space' => true,
'new_with_braces' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_whitespace_in_blank_line' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_trait_insert_per_statement' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in([__DIR__.'/src/'])
)
;
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1 align="center">Laravel Payable</h1>

<p align="center"> Payment for Laravel</p>


## Installing

```shell
$ composer require overtrue/laravel-payable -vvv
```

## Usage

TODO

## Contributing

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/vendor/package/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/vendor/package/issues).
3. Contribute new features or update the wiki.

_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._

## License

MIT
57 changes: 57 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "overtrue/laravel-payable",
"description": "Payment for laravel.",
"license": "MIT",
"authors": [
{
"name": "overtrue",
"email": "anzhengchao@gmail.com"
}
],
"require": {
"php": ">=7.1.11",
"laravel/framework": "^7.1"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.7",
"friendsofphp/php-cs-fixer": "^2.15",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
"Overtrue\\LaravelPayable\\": "src"
}
},
"extra": {
"hooks": {
"pre-commit": [
"composer test",
"composer fix-style"
],
"pre-push": [
"composer test",
"composer check-style"
]
}
},
"scripts": {
"post-update-cmd": [
"cghooks update"
],
"post-merge": "composer install",
"post-install-cmd": [
"cghooks add --ignore-lock",
"cghooks update"
],
"cghooks": "vendor/bin/cghooks",
"check-style": "php-cs-fixer fix --using-cache=no --diff --config=.php_cs --dry-run --ansi",
"fix-style": "php-cs-fixer fix --using-cache=no --config=.php_cs --ansi",
"test": "vendor/bin/phpunit"
},
"scripts-descriptions": {
"test": "Run all tests.",
"check-style": "Run style checks (only dry run - no fixing!).",
"fix-style": "Run style checks and fix violations."
}
}
47 changes: 47 additions & 0 deletions migrations/2020_04_11_000000_create_payments_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePaymentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id')->index()->nullable();
$table->morphs('payable');
$table->unsignedDouble('amount', 2)->default(0.00);
$table->unsignedDouble('paid_amount')->default(0);
$table->string('description');
$table->string('transaction_id')->index();
$table->string('currency')->index()->default('CNY');
$table->string('status')->index()->default('pending')->comment('pending/paid/cancelled/failed');
$table->string('gateway')->index();
$table->json('gateway_order')->nullable();
$table->json('context')->nullable();
$table->json('original_result')->nullable();
$table->timestamp('paid_at')->nullable();
$table->timestamp('expired_at')->nullable();
$table->timestamp('failed_at')->nullable();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('payments');
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
28 changes: 28 additions & 0 deletions src/Events/PaymentUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Overtrue\LaravelPayable\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Overtrue\LaravelPayable\Payment;

class PaymentUpdated
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* @var \Overtrue\LaravelPayable\Payment
*/
protected $payment;

/**
* @param \Overtrue\LaravelPayable\Payment $payment
*/
public function __construct(Payment $payment)
{
$this->payment = $payment;
}
}
10 changes: 10 additions & 0 deletions src/Payable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Overtrue\LaravelPayable;

interface Payable
{
public function getPaymentTotal(): int;
public function getPaymentDescription(): int;
public function getUserId();
}
95 changes: 95 additions & 0 deletions src/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Overtrue\LaravelPayable;

use Overtrue\LaravelPayable\Events\PaymentUpdated;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

/**
* @property int $id
* @property string $user_id
* @property string $payable_id
* @property string $payable_type
* @property double $amount
* @property double $paid_amount
* @property string $description
* @property string $transaction_id
* @property string $currency
* @property string $status
* @property string $gateway
* @property array $gateway_order
* @property array $context
* @property array $original_result
* @property \Carbon\Carbon $paid_at
* @property \Carbon\Carbon $expired_at
* @property \Carbon\Carbon $failed_at
*/
class Payment extends Model
{
use SoftDeletes;

const STATUS_PENDING = 'pending';
const STATUS_PAID = 'paid';
const STATUS_CANCELLED = 'cancelled';
const STATUS_FAILED = 'failed';

const STATUSES = [
self::STATUS_PENDING => '待支付',
self::STATUS_PAID => '已支付',
self::STATUS_CANCELLED => '已取消',
self::STATUS_FAILED => '已失败',
];

/**
* @var string[]
*/
protected $fillable = [
'user_id', 'payable', 'amount', 'paid_amount',
'description', 'currency', 'status', 'gateway',
'gateway_order', 'context', 'original_result',
'paid_at', 'expired_at', 'failed_at',
];

/**
* @var string[]
*/
protected $casts = [
'amount' => 'double',
'paid_amount' => 'double',
'gateway_order' => 'array',
'context' => 'array',
'original_result' => 'array',
];

/**
* @var string[]
*/
protected $dates = [
'paid_at', 'expired_at', 'failed_at',
];

public static function boot()
{
parent::boot();

static::creating(function (Payment $payment) {
$payment->status = $payment->status ?: self::STATUS_PENDING;
$payment->user_id = $payment->user_id ?: auth()->id();
$payment->transaction_id = $payment->transaction_id ?: Str::orderedUuid();
});

static::saved(function (Payment $payment) {
\event(new PaymentUpdated($payment));
});
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function payable()
{
return $this->morphTo();
}
}
Empty file added tests/.gitkeep
Empty file.

0 comments on commit 18c80e7

Please sign in to comment.