我在paypal创建了一个帐户以在我的应用中进行测试。
composer require paypal/rest-api-sdk-php
将客户端ID和密码添加到.env
这就是我目前的看法
@if ($message = Session::get('success'))
<div class="w3-panel w3-green w3-display-container">
<span onclick="this.parentElement.style.display='none'"
class="w3-button w3-green w3-large w3-display-topright">×</span>
<p>{!! $message !!}</p>
</div>
<?php Session::forget('success');?>
@endif
@if ($message = Session::get('error'))
<div class="w3-panel w3-red w3-display-container">
<span onclick="this.parentElement.style.display='none'"
class="w3-button w3-red w3-large w3-display-topright">×</span>
<p>{!! $message !!}</p>
</div>
<?php Session::forget('error');?>
@endif
<form class="w3-container w3-display-middle w3-card-4 " method="POST" id="payment-form" action="{!! URL::to('paypal') !!}">
{{ csrf_field() }}
<h2 class="w3-text-blue">Payment Form</h2>
<p>
<label class="w3-text-blue"><b>Enter Amount</b></label>
<input class="w3-input w3-border" name="amount" type="text"></p>
<button class="w3-btn w3-blue">Pay with PayPal</button></p>
</form>
我的PaymentController如下所示:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use PayPal\Api\Amount;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\Details;
use PayPal\Api\ChargeModel;
use PayPal\Api\Currency;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\PaymentDefinition;
use PayPal\Api\Plan;
use PayPal\Api\Patch;
use PayPal\Api\PatchRequest;
use PayPal\Common\PayPalModel;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\RedirectUrls;
use PayPal\Api\PaymentExecution;
use URL;
use Redirect;
use Illuminate\Support\Facades\Input;
class PaymentController extends Controller
{
private $apiContext;
private $mode;
private $client_id;
private $secret;
public function __construct()
{
/** 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']);
}
public function payWithpaypal(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('status')) /** Specify return URL **/
->setCancelUrl(URL::route('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 (\PayPal\Exception\PPConnectionException $ex) {
if (\Config::get('app.debug')) {
\Session::put('error', 'Connection timeout');
return Redirect::route('paywithpaypal');
} else {
\Session::put('error', 'Some error occur, sorry for inconvenient');
return Redirect::route('paywithpaypal');
}
}
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('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('/');
}
$payment = Payment::get($payment_id, $this->_api_context);
$execution = new PaymentExecution();
$execution->setPayerId(Input::get('PayerID'));
/**Execute the payment **/
$result = $payment->execute($execution, $this->_api_context);
if ($result->getState() == 'approved') {
/*************************************************
PAYMENT SUCCESSFULL - DO THE REST HERE
/************************************************/
\Session::put('success', 'Payment success');
return Redirect::route('/');
}
\Session::put('error', 'Payment failed');
return Redirect::route('/');
}
}
现在,当我输入金额并尝试付款时,出现以下错误:
Argument 1 passed to PayPal\Rest\ApiContext::setConfig() must be of the type array, null given, called in C:\xampp\htdocs\owlproperty\app\Http\Controllers\PaymentController.php on line 47
我在config文件夹下创建了paypal文件,如下所示:
<?php
return [
'client_id' => env('PAYPAL_CLIENT_ID',''),
'secret' => env('PAYPAL_SECRET',''),
'settings' => array(
'mode' => env('PAYPAL_MODE','sandbox'),
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => storage_path() . '/logs/paypal.log',
'log.LogLevel' => 'ERROR'
),
];
我可能做错了什么吗?
答案 0 :(得分:0)
您没有正确加载配置文件:
可以使用“点”语法访问配置值,该语法 包括文件名和您希望访问的选项。
假设您的confile文件名为paypal.php,请在构造函数中使用它:
public function __construct()
{
/** PayPal api context **/
$settings = config('paypal.settings');
$client_id = config('paypal.client_id');
$secret = config('paypal.secret');
$this->_api_context = new ApiContext(new OAuthTokenCredential(
$client_id,
$secret)
);
$this->_api_context->setConfig($settings);
}