如何在Laravel中集成Paypal支付网关?我试过了http://www.17educations.com/laravel/paypal-integration-in-laravel/但是我遇到了一些问题,请告诉我一些想法
Follow Bellow Few Step:
1)安装Laravel应用
2)数据库配置
4)配置paypal.php文件
5)创建路由
3)create controller
步骤1:安装Laravel应用程序
我们要从头开始,所以我们需要使用bellow命令获得新的Laravel应用程序,所以打开终端或命令提示符并运行bellow命令:
步骤2:数据库配置
在此步骤中,我们需要进行数据库配置,您必须在.env文件中添加
以下详细信息。
1。数据库用户名
1。数据库密码
1。数据库名称
在.env文件中也有主机和端口的详细信息,你可以在你的系统中配置所有的细节,所以你可以像下面这样: .env
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
步骤3:Install Required Packages
我们需要以下2包整合贝宝支付在我们的laravel应用程序。在编写器中添加以下两个包。json文件。
"guzzlehttp/guzzle": "~5.4",
"paypal/rest-api-sdk-php": "*",
然后在您的终端中运行以下命令。Vendor:publish命令用于从Vendor包文件中复制应用程序中的一些配置文件。
php artisan vendor:publish
运行此命令后,然后在您的config/paypal.php路径中自动创建paypal.php文件。
步骤4:配置paypal.php文件
<?php
return array(
/** set your paypal credential **/
'client_id' =>'paypal client_id',
'secret' => 'paypal secret ID',
/**
* SDK configuration
*/
'settings' => array(
/**
* Available option 'sandbox' or 'live'
*/
'mode' => 'sandbox',
/**
* Specify the max request time in seconds
*/
'http.ConnectionTimeOut' => 1000,
/**
* Whether want to log to a file
*/
'log.LogEnabled' => true,
/**
* Specify the file that want to write on
*/
'log.FileName' => storage_path() . '/logs/paypal.log',
/**
* Available option 'FINE', 'INFO', 'WARN' or 'ERROR'
*
* Logging is most verbose in the 'FINE' level and decreases as you
* proceed towards ERROR
*/
'log.LogLevel' => 'FINE'
),
);
步骤5:创建路由
在这一步,我们需要创建路线的贝宝支付。所以打开你的routes/web.php文件,添加下面的路由。路线/web.php
Route::get('paywithpaypal', array('as' => 'addmoney.paywithpaypal','uses' => 'AddMoneyController@payWithPaypal',));
Route::post('paypal', array('as' => 'addmoney.paypal','uses' => 'AddMoneyController@postPaymentWithpaypal',));
Route::get('paypal', array('as' => 'payment.status','uses' => 'AddMoneyController@getPaymentStatus',));
步骤6:创建控制器现在,我们应该在app/Http/Controllers/AddMoneyController.php路径中创建一个新的控制器AddMoneyController。这个控制器将管理布局和支付post请求,所以运行下面的命令生成新的控制器:
php artisan make:controller AddMoneyController
好的,现在把下面的内容放到控制器文件中:
应用程序/Http/控制器/AddMoneyController.php
<?php
namespace AppHttpControllers;
use AppHttpRequests;
use IlluminateHttpRequest;
use Validator;
use URL;
use Session;
use Redirect;
use Input;
/** All Paypal Details class **/
use PayPalRestApiContext;
use PayPalAuthOAuthTokenCredential;
use PayPalApiAmount;
use PayPalApiDetails;
use PayPalApiItem;
use PayPalApiItemList;
use PayPalApiPayer;
use PayPalApiPayment;
use PayPalApiRedirectUrls;
use PayPalApiExecutePayment;
use PayPalApiPaymentExecution;
use PayPalApiTransaction;
class AddMoneyController extends HomeController
{
private $_api_context;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
/** setup PayPal api context **/
$paypal_conf = Config::get('paypal');
$this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
$this->_api_context->setConfig($paypal_conf['settings']);
}
/**
* Show the application paywith paypalpage.
*
* @return IlluminateHttpResponse
*/
public function payWithPaypal()
{
return view('paywithpaypal');
}
/**
* Store a details of payment with paypal.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function postPaymentWithpaypal(Request $request)
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item_1 = new Item();
$item_1->setName('Item 1') /** item name **/
->setCurrency('USD')
->setQuantity(1)
->setPrice($request->get('amount')); /** unit price **/
$item_list = new ItemList();
$item_list->setItems(array($item_1));
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($request->get('amount'));
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Your transaction description');
$redirect_urls = new RedirectUrls();
$redirect_urls->setReturnUrl(URL::route('payment.status')) /** Specify return URL **/
->setCancelUrl(URL::route('payment.status'));
$payment = new Payment();
$payment->setIntent('Sale')
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions(array($transaction));
/** dd($payment->create($this->_api_context));exit; **/
try {
$payment->create($this->_api_context);
} catch (PayPalExceptionPPConnectionException $ex) {
if (Config::get('app.debug')) {
Session::put('error','Connection timeout');
return Redirect::route('addmoney.paywithpaypal');
/** echo "Exception: " . $ex->getMessage() . PHP_EOL; **/
/** $err_data = json_decode($ex->getData(), true); **/
/** exit; **/
} else {
Session::put('error','Some error occur, sorry for inconvenient');
return Redirect::route('addmoney.paywithpaypal');
/** die('Some error occur, sorry for inconvenient'); **/
}
}
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
/** add payment ID to session **/
Session::put('paypal_payment_id', $payment->getId());
if(isset($redirect_url)) {
/** redirect to paypal **/
return Redirect::away($redirect_url);
}
Session::put('error','Unknown error occurred');
return Redirect::route('addmoney.paywithpaypal');
}
public function getPaymentStatus()
{
/** Get the payment ID before session clear **/
$payment_id = Session::get('paypal_payment_id');
/** clear the session payment ID **/
Session::forget('paypal_payment_id');
if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
Session::put('error','Payment failed');
return Redirect::route('addmoney.paywithpaypal');
}
$payment = Payment::get($payment_id, $this->_api_context);
/** PaymentExecution object includes information necessary **/
/** to execute a PayPal account payment. **/
/** The payer_id is added to the request query parameters **/
/** when the user is redirected from paypal back to your site **/
$execution = new PaymentExecution();
$execution->setPayerId(Input::get('PayerID'));
/**Execute the payment **/
$result = $payment->execute($execution, $this->_api_context);
/** dd($result);exit; /** DEBUG RESULT, remove it later **/
if ($result->getState() == 'approved') {
/** it's all right **/
/** Here Write your database logic like that insert record or value in database if you want **/
Session::put('success','Payment success');
return Redirect::route('addmoney.paywithpaypal');
}
Session::put('error','Payment failed');
return Redirect::route('addmoney.paywithpaypal');
}
}
现在我们准备运行我们的示例,所以运行下面的命令来快速运行:
php artisan serve
现在你可以在浏览器上打开下面的URL:
http://localhost:8000/paywithpaypal
请访问此教程链接。
https://www.youtube.com/watch?v=ly2xm_NM46g http://laravelcode.com/post/how-to-integrate-paypal-payment-gateway-in-laravel-54你们想通过PayPal实现什么目标?你们遇到了什么问题?
我建议不要使用paypal SDK,而是使用PHP联盟的Omnipay。参见http://omnipay.thephpleague.com/和https://github.com/thephpleague/omnipay-paypal
如果您下载了omnipay库,您将在各种类头文件中看到一些示例。你需要实现对purchase()的调用,然后有一个returnUrl和一个cancelUrl,在你的returnUrl中,你需要实现对completePurchase()的调用。
如果你告诉我你想要实现什么,我可以给你一些代码示例。
到目前为止,这里有一些更简单的laravel选项:
- Omnipay -多网关,但没有订阅(伟大的文档)
- Payum -订阅的多网关(文档糟糕)
- Srmklive/Paypal -只有Paypal与订阅(伟大的文档并且非常活跃)
前两个是框架无关的,这意味着你可以直接使用它们,但你必须为laravel配置它,但如果你想要laravel的简单方法,你应该为laravel使用包装器。这意味着您只需要使用包装器而不是库本身。他们俩都有很多包装纸。
对于那些只想用paypal工作的人来说,不要再看了,Srmklive的paypal包真是太棒了。
到目前为止,在Laravel上集成Paypal支付网关有一些简单的选择:教程链接
Github Clone Paypal Gateway
$response = $provider->doExpressCheckoutPayment($data, $token, $payer_id)
$payment_status = $response['ACK']
$transaction_id = $response['PAYMENTINFO_0_TRANSACTIONID']