├── .editorconfig ├── .github └── FUNDING.yml ├── README.md ├── composer.json ├── migrations └── 2020_04_11_000000_create_payments_table.php └── src ├── Events └── PaymentUpdated.php ├── Payable.php └── Payment.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = false 10 | 11 | [*.{vue,js,scss}] 12 | charset = utf-8 13 | indent_style = space 14 | indent_size = 2 15 | end_of_line = lf 16 | insert_final_newline = true 17 | trim_trailing_whitespace = true 18 | 19 | [*.md] 20 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [] 2 | patreon: overtrue 3 | custom: https://www.easywechat.com/img/pay/wechat.jpg 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
Payment for Laravel
4 | 5 | 6 | ## Installing 7 | 8 | ```shell 9 | $ composer require overtrue/laravel-payable -vvv 10 | ``` 11 | 12 | ## Usage 13 | 14 | TODO 15 | 16 | ## Contributing 17 | 18 | You can contribute in one of three ways: 19 | 20 | 1. File bug reports using the [issue tracker](https://github.com/vendor/package/issues). 21 | 2. Answer questions or fix bugs on the [issue tracker](https://github.com/vendor/package/issues). 22 | 3. Contribute new features or update the wiki. 23 | 24 | _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._ 25 | 26 | ## License 27 | 28 | MIT 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "overtrue/laravel-payable", 3 | "description": "Payment for laravel.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "overtrue", 8 | "email": "anzhengchao@gmail.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=7.1.11", 13 | "laravel/framework": "^7.1" 14 | }, 15 | "require-dev": { 16 | "brainmaestro/composer-git-hooks": "^2.7", 17 | "friendsofphp/php-cs-fixer": "^2.15", 18 | "mockery/mockery": "^1.0", 19 | "phpunit/phpunit": "^8.5" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Overtrue\\LaravelPayable\\": "src" 24 | } 25 | }, 26 | "extra": { 27 | "hooks": { 28 | "pre-commit": [ 29 | "composer test", 30 | "composer fix-style" 31 | ], 32 | "pre-push": [ 33 | "composer test", 34 | "composer check-style" 35 | ] 36 | } 37 | }, 38 | "scripts": { 39 | "post-update-cmd": [ 40 | "cghooks update" 41 | ], 42 | "post-merge": "composer install", 43 | "post-install-cmd": [ 44 | "cghooks add --ignore-lock", 45 | "cghooks update" 46 | ], 47 | "cghooks": "vendor/bin/cghooks", 48 | "check-style": "php-cs-fixer fix --using-cache=no --diff --config=.php_cs --dry-run --ansi", 49 | "fix-style": "php-cs-fixer fix --using-cache=no --config=.php_cs --ansi", 50 | "test": "vendor/bin/phpunit" 51 | }, 52 | "scripts-descriptions": { 53 | "test": "Run all tests.", 54 | "check-style": "Run style checks (only dry run - no fixing!).", 55 | "fix-style": "Run style checks and fix violations." 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /migrations/2020_04_11_000000_create_payments_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | $table->string('user_id')->index()->nullable(); 19 | $table->morphs('payable'); 20 | $table->unsignedDouble('amount', 2)->default(0.00); 21 | $table->unsignedDouble('paid_amount')->default(0); 22 | $table->string('description'); 23 | $table->string('transaction_id')->index(); 24 | $table->string('currency')->index()->default('CNY'); 25 | $table->string('status')->index()->default('pending')->comment('pending/paid/cancelled/failed'); 26 | $table->string('gateway')->index(); 27 | $table->json('gateway_order')->nullable(); 28 | $table->json('context')->nullable(); 29 | $table->json('original_result')->nullable(); 30 | $table->timestamp('paid_at')->nullable(); 31 | $table->timestamp('expired_at')->nullable(); 32 | $table->timestamp('failed_at')->nullable(); 33 | $table->timestamps(); 34 | $table->softDeletes(); 35 | }); 36 | } 37 | 38 | /** 39 | * Reverse the migrations. 40 | * 41 | * @return void 42 | */ 43 | public function down() 44 | { 45 | Schema::dropIfExists('payments'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Events/PaymentUpdated.php: -------------------------------------------------------------------------------- 1 | payment = $payment; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Payable.php: -------------------------------------------------------------------------------- 1 | '待支付', 40 | self::STATUS_PAID => '已支付', 41 | self::STATUS_CANCELLED => '已取消', 42 | self::STATUS_FAILED => '已失败', 43 | ]; 44 | 45 | /** 46 | * @var string[] 47 | */ 48 | protected $fillable = [ 49 | 'user_id', 'payable', 'amount', 'paid_amount', 50 | 'description', 'currency', 'status', 'gateway', 51 | 'gateway_order', 'context', 'original_result', 52 | 'paid_at', 'expired_at', 'failed_at', 53 | ]; 54 | 55 | /** 56 | * @var string[] 57 | */ 58 | protected $casts = [ 59 | 'amount' => 'double', 60 | 'paid_amount' => 'double', 61 | 'gateway_order' => 'array', 62 | 'context' => 'array', 63 | 'original_result' => 'array', 64 | ]; 65 | 66 | /** 67 | * @var string[] 68 | */ 69 | protected $dates = [ 70 | 'paid_at', 'expired_at', 'failed_at', 71 | ]; 72 | 73 | public static function boot() 74 | { 75 | parent::boot(); 76 | 77 | static::creating(function (Payment $payment) { 78 | $payment->status = $payment->status ?: self::STATUS_PENDING; 79 | $payment->user_id = $payment->user_id ?: auth()->id(); 80 | $payment->transaction_id = $payment->transaction_id ?: Str::orderedUuid(); 81 | }); 82 | 83 | static::saved(function (Payment $payment) { 84 | \event(new PaymentUpdated($payment)); 85 | }); 86 | } 87 | 88 | /** 89 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 90 | */ 91 | public function payable() 92 | { 93 | return $this->morphTo(); 94 | } 95 | } 96 | --------------------------------------------------------------------------------