第一步申请账号:
1.注册paypal开发者账号
2.创建自己的应用
live:是上线模式
sendbox:沙盒模式
3. 获取client id和 client secret
4.创建支付订单
初始化参数
public function _initialize()
{
parent::_initialize();
$PaymentModel = new PaymentModel();
$config = $PaymentModel->getCacheClass('paypal');
//dump($config);exit;
if(empty($config)) {
echo '支付参数未配置!';
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';//回调地址
$this->PayPal = new ApiContext(
new OAuthTokenCredential(
$this->clientId,
$this->clientSecret
)
);
// 如果是沙盒测试环境不设置,请注释掉
$this->PayPal->setConfig(
array(
'mode' => 'live',
)
);
}
生成订单并且跳转到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}");
}
/**
* 回调
*/
public function callback()
{
// 修改订单状态
$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();
}
}
跳转成功后,登入选择支付卡号就可以了