paypal的使用

1,下载包:

{
"require" : {
"paypal/rest-api-sdk-php" : "1.7.4"
},
}

composer update;

2,这是laravel的代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\PaymentExecution;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Auth;
use PayPal\Api\ShippingAddress;
use Illuminate\Support\Facades\DB;

class PayController extends Controller
{

//发起支付的方法
public function getapproval()
{
//检查订单是否存在,不存在就返回提示页面

$apiContext = new ApiContext(
new OAuthTokenCredential(
config('app.CLIENTID'),
config('app.CLIENTSECRET')
)
);
$identifier=request('identifier');

$total=0;

$freight=0;

//得到订单信息

$orders=\App\Orderdetail::with('good')->where([['identifier','=',$identifier],['status','=',0],['user_id','=',Auth::id()]])->get();

if($orders->first()==null)
return view('order/error');

$list=array();

for($i=0;$i<count($orders);$i++)
{
$a='item'.$i;
$$a=new Item();

$tmptotal=$orders[$i]->total;

$total= $total+$tmptotal;

$freight=$freight+$orders[$i]->freight;

$$a->setName($orders[$i]->good->goodname)
->setCurrency('USD')
->setQuantity($orders[$i]->num)
->setPrice($tmptotal/$orders[$i]->num);

$list[]=$$a;
}
$payer = new Payer();
$payer->setPaymentMethod("paypal");

$itemList = new ItemList();
$itemList->setItems($list);

// 自定义用户收货地址,避免用户在paypal上账单的收货地址和销售方收货地址有出入
// 这里定义了收货地址,用户在支付过程中就不能更改收货地址,否则,用户可以自己更改收货地址
$address = new ShippingAddress();
$address->setRecipientName('none')
->setLine1('none')
->setLine2('none')
->setCity('none')
->setState('none')
->setPhone('123456789')
->setPostalCode('none')
->setCountryCode('CN');

$itemList->setShippingAddress($address);


// 设置运费和税费
$details = new Details();
$details->setShipping($freight)
->setSubtotal($total);

$amount = new Amount();
$amount->setCurrency("USD")
->setTotal($total+$freight)
->setDetails($details);

$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("http://127.0.0.1:8888/pay?tl=$total&ft=$freight&success=true&identifier=$identifier") //成功支付之后
->setCancelUrl("http://127.0.0.1:8888/pay?success=false"); //取消支付后

$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
//创建付款
try {
$payment->create($apiContext);
} catch (Exception $ex) {
exit(1);
}

$approvalUrl = $payment->getApprovalLink();
//重定向支付页面
return redirect($approvalUrl);

}

//开始支付
public function startPay()
{

$apiContext = new ApiContext(
new OAuthTokenCredential(
config('app.CLIENTID'),
config('app.CLIENTSECRET')
)
);

$status=request('success');
$identifier=request('identifier');
if (isset($status) && $status == 'true') {


$paymentId =request('paymentId');
$payment = Payment::get($paymentId, $apiContext);

$execution = new PaymentExecution();

$execution->setPayerId($_GET['PayerID']);

$transaction = new Transaction();
$amount = new Amount();
$details = new Details();



$details->setShipping(request('ft'))
->setSubtotal(request('tl'));

$amount->setCurrency('USD');
$amount->setTotal(request('tl')+request('ft'));
$amount->setDetails($details);
$transaction->setAmount($amount);

$execution->addTransaction($transaction);


try {

//执行付款
$result = $payment->execute($execution, $apiContext);

try {
$payment = Payment::get($paymentId, $apiContext);

//更新数据库订单付款状态
$time=date('Y-m-d H:i:s',time());
DB::table('orderdetails')->where([['identifier', '=',$identifier],['status','=',0]])->update(['status' => 1,'updated_at'=>$time]);

return redirect('/su');
} catch (Exception $ex) {
exit(1);
}
} catch (Exception $ex) {
exit(1);
}



} else {
return redirect('/fail');
}

}


//支付成功页面
public function su()
{
return view('pay/success');
}

//提示已经支付过的页面
public function alreadyPay()
{
return view('pay/alreadyPay');
}

public function fail()
{
return view('pay/fail');
}
}

可以看看包里面的rest-api-sdk-php/sample/index.php

猜你喜欢

转载自www.cnblogs.com/lvchengda/p/9077981.html