第一步申請賬號:
1.注冊paypal開發(fā)者賬號
2.創(chuàng)建自己的應(yīng)用
live:是上線模式
sendbox:沙盒模式
3. 獲取client id和 client secret
4.創(chuàng)建支付訂單
初始化參數(shù)
public function _initialize()
{
parent::_initialize();
$PaymentModel = new PaymentModel();
$config = $PaymentModel->getCacheClass('paypal');
//dump($config);exit;
if(empty($config)) {
echo '支付參數(shù)未配置!';
exit();
} else {
$this->clientId = $config['app_id'];
$this->clientSecret = $config['app_key'];
}
$request = Request::instance();
$base_url = $request->domain();
$this->accept_url = $base_url.'/paypal/callback';//回調(diào)地址
$this->PayPal = new ApiContext(
new OAuthTokenCredential(
$this->clientId,
$this->clientSecret
)
);
// 如果是沙盒測試環(huán)境不設(shè)置,請注釋掉
$this->PayPal->setConfig(
array(
'mode' => 'live',
)
);
}
生成訂單并且跳轉(zhuǎn)到paypal
public function pay($product, $price, $shipping = 0, $description,$currency)
{
$paypal = $this->PayPal;
$total = $price + $shipping;//總價
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName($product)->setCurrency($currency)->setQuantity(1)->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$details = new Details();
$details->setShipping($shipping)->setSubtotal($price);
$amount = new Amount();
$amount->setCurrency($currency)->setTotal($total)->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)->setItemList($itemList)->setDescription($description)->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
// $redirectUrls->setReturnUrl(self::accept_url . '?success=true')->setCancelUrl(self::accept_url . '/?success=false');
$redirectUrls->setReturnUrl($this->accept_url .'?success=true')->setCancelUrl($this->accept_url .'?success=false');
$payment = new Payment();
$payment->setIntent('sale')->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions([$transaction]);
try {
$payment->create($paypal);
} catch (PayPalConnectionException $e) {
echo $e->getData();
die();
}
$approvalUrl = $payment->getApprovalLink();
//echo $approvalUrl;
$this->redirect($approvalUrl);
//header("Location:{$approvalUrl}");
}
/**
* 回調(diào)
*/
public function callback()
{
// 修改訂單狀態(tài)
$success = trim($_GET['success']);
if ($success == 'false' && !isset($_GET['paymentId']) && !isset($_GET['PayerID'])) {
$this->error('Cancal Pay!!', 'member/order');
}
$paymentId = trim($_GET['paymentId']);
$PayerID = trim($_GET['PayerID']);
if (!isset($success, $paymentId, $PayerID)) {
echo 'Failure to pay。';
exit();
}
if ((bool)$_GET['success'] === 'false') {
echo 'Failure to pay,payment ID【' . $paymentId . '】,Payer ID【' . $PayerID . '】';
exit();
}
$payment = Payment::get($paymentId, $this->PayPal);
$execute = new PaymentExecution();
$execute->setPayerId($PayerID);
try {
$data = $payment->execute($execute, $this->PayPal);
$data_arr = $data->toArray();
$order_no = $data_arr['transactions'][0]['description'];
$total_money = $data_arr['transactions'][0]['amount']['total'];
// 實例化訂單模型
$model = $this->getOrderModel($order_no, OrderTypeEnum::MASTER);
// 訂單信息
$order = $model->getOrderInfo();
if(empty($order)){
echo 'Order not exist';
}
$update_data['transaction_id'] = $paymentId;
$status = $model->onPaySuccess(PayTypeEnum::PAYPAL, $update_data);
if ($status == false) {
echo $model->getError();
}
} catch (Exception $e) {
$this->error('Pay Error!', 'member/order');
//echo $e . ',支付失敗,支付ID【' . $paymentId . '】,支付人ID【' . $PayerID . '】';
//exit();
}
}
跳轉(zhuǎn)成功后,登入選擇支付卡號就可以了
如沒特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請注明來自http://www.sdlwjx666.com/news/6831.html