互联网+时代,说建站,谈运营与网络营销
第一步扬州做网站公司申请账号:
1.注册paypal开发者账号
2.创建自己的运用
live:是上线模式
sendbox:沙盒模式
3.获取client id和 client secret
4.创建支付订单
初始化参数
publicfunction_initialize()
{
parent::_initialize();
$paymentmodel= newpaymentmodel();
$config= $paymentmodel->getcacheclass('paypal');
//dump($config);exit;
if(empty($config)) {
echo'支付参数未配置!';
exit();
} else{
$this->clientid= $config['手机应用_id'];
$this->clientsecret= $config['手机应用_key'];
}
$request= request::instance();
$base_url= $request->domain();
$this->accept_url= $base_url.'/paypal/callback';//回调地址
$this->paypal= newapicontext(
newoauthtokencredential(
$this->clientid,
$this->clientsecret
)
);
// 如果是沙盒测试环境不设定,请注释掉
$this->paypal->setconfig(
array(
'mode'=> 'live',
)
);
}
生成订单而且跳转到paypal
publicfunctionpay($product, $price, $shipping= 0, $description,$currency)
{
$paypal= $this->paypal;
$total= $price+ $shipping;//总价
$payer= newpayer();
$payer->setpaymentmethod('paypal');
$item= newitem();
$item->setname($product)->setcurrency($currency)->setquantity(1)->setprice($price);
$itemlist= newitemlist();
$itemlist->setitems([$item]);
$details= newdetails();
$details->setshipping($shipping)->setsubtotal($price);
$amount= newamount();
$amount->setcurrency($currency)->settotal($total)->setdetails($details);
$transaction= newtransaction();
$transaction->setamount($amount)->setitemlist($itemlist)->setdescription($description)->setinvoicenumber(uniqid());
$redirecturls= newredirecturls();
// $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= newpayment();
$payment->setintent('sale')->setpayer($payer)->setredirecturls($redirecturls)->settransactions([$transaction]);
try{
$payment->create($paypal);
} catch(paypalconnectionexception$e) {
echo$e->getdata();
die();
}
$手机应用rovalurl= $payment->get手机应用rovallink();
//echo $手机应用rovalurl;
$this->redirect($手机应用rovalurl);
//header("location:{$手机应用rovalurl}");
}
/**
* 回调
*/
publicfunctioncallback()
{
// 修改订单状态
$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= newpaymentexecution();
$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();
}
}
跳转成功后,登进选择扬州做网站公司支付延迟大号就可以了