内容纲要

我们添加两个表 products 和 payments 来准备例子。

[code]
php artisan make:migration create_products_table
php artisan make:migration create_payments_table
[/code]

[code language=”php”]
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create(‘products’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->text(‘details’);
$table->float(‘price’);
$table->timestamps();
});
}
public function down()
{
Schema::drop("products");
}
}
[/code]

[code language=”php”]
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePaymentsTable extends Migration
{
public function up()
{
Schema::create(‘payments’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘item_number’);
$table->string(‘transaction_id’);
$table->string(‘currency_code’);
$table->string(‘payment_status’);
$table->timestamps();
});
}
public function down()
{
Schema::drop("payments");
}
}

[/code]

Product 模型

[code language=”php”]
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public $fillable = [‘name’,’details’,’price’];
}
[/code]

Payment 模型

[code language=”php”]
namespace App;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model
{
public $fillable = [‘item_number’,’transaction_id’,’currency_code’,’payment_status’];
}
[/code]

修改路由 routes.php

[code language=”php”]
Route::get(‘payment-status’,array(‘as’=>’payment.status’,’uses’=>’PaymentController@paymentInfo’));
Route::get(‘payment’,array(‘as’=>’payment’,’uses’=>’PaymentController@payment’));
Route::get(‘payment-cancel’, function () {
return ‘Payment has been canceled’;
});

[/code]

Payment Controller

[code language=”php”]
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Payment;
class PaymentController extends Controller {
public function payment(Request $request){
$product=Product::find($request->id);
return view(‘payment’,compact(‘product’));
}
public function paymentInfo(Request $request){
if($request->tx){
if($payment=Payment::where(‘transaction_id’,$request->tx)->first()){
$payment_id=$payment->id;
}else{
$payment=new Payment;
$payment->item_number=$request->item_number;
$payment->transaction_id=$request->tx;
$payment->currency_code=$request->cc;
$payment->payment_status=$request->st;
$payment->save();
$payment_id=$payment->id;
}
return ‘Pyament has been done and your payment id is : ‘.$payment_id;

}else{
return ‘Payment has failed’;
}
}
}
[/code]

支付表单

[code language=”html”]
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" name="frmTransaction" id="frmTransaction">
<input type="hidden" name="business" value="{{$paypal_id}}">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="item_name" value="{{$product->name}}">
<input type="hidden" name="item_number" value="{{$product->id}}">
<input type="hidden" name="amount" value="{{$product->price}}">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="cancel_return" value="http://demo.expertphp.in/payment-cancel">
<input type="hidden" name="return" value="http://demo.expertphp.in/payment-status">
</form>
<SCRIPT>document.frmTransaction.submit();</SCRIPT>
[/code]

支付成功后返回

[code language=”js”]
{"tx":"4V936557BM418705U","st":"Completed","amt":"10.00","cc":"USD","cm":"","item_number":"292"}
[/code]

发表评论

电子邮件地址不会被公开。 必填项已用*标注