当前位置: 首页 > 后端技术 > PHP

在Laravel中集成PayPal

时间:2023-03-29 20:55:59 PHP

最近在写一个国外买家的shoppingmall项目。既然是给国外的买家,就必须要用PayPal这个支付平台。因为在对接PayPal的过程中遇到了一些问题,花费了一些时间,所以写下对接过程,希望对使用PayPal的朋友有所帮助。我集成了paypal/rest-api-sdk-php。PayPal的api有v1和v2两个版本,我用的是v1版本。以下均以代码形式展示,没有截图,但我尽量用代码解释清楚。假设你已经有一个用laravel编写的项目:步骤:1安装扩展包composerrequirepaypal/rest-api-sdk-php步骤:2创建一个payment.php配置文件在Config目录下新建一个payment.php配置文件,内容如下配置*/'settings'=>array(/***沙箱测试'sandbox'或'live'*/'mode'=>'sandbox',/***请求超时*/'http.ConnectionTimeOut'=>1000,/***是否启用日志:true启用,false不启用*/'log.LogEnabled'=>true,/***日志存储文件*/'log.FileName'=>storage_path().'/logs/paypal.log',/***日志级别'DEBUG','INFO','WARN'或'ERROR'**/'log.LogLevel'=>'INFO'),],'2checkout'=>[//]];Step:3Createaroute//Step1:显示表单,这是一个简单的表单页面,我们输入金额,然后点击SubmitRoute::get('paypal-form','Payment\PayPalController@payPalShow')->name('paypal-form');//Step2:第一步提交后,发送请求请求该方法,用于创建订单和创建支付流程Route::post('paypal-pay','Payment\PayPalController@pay')->name('payment.paypay.pay');//第三步:异步回调Route::post('paypal-notify','Payment\PayPalController@payPalNotify')->name('payment.paypal.notify');//第三步:前端回调Route::get('paypal-return','Payment\PayPalController@payPalReturn')->name('payment.paypal.return');//第三步:取消Route::get('paypal-cancel','Payment\PayPalController@payPalCancel')->name('payment.paypal.cancel');Step:3创建控制器_api_context=newApiContext(newOAuthTokenCredential($payPal_config['client_id'],$payPal_config['secret']));$this->_api_context->setConfig($payPal_config['setting']);}//显示表单publicfunctionpayPalShow(){returnview('payment.paypal');}//第二步,在这里请求publicfunctionpay(Request$request){$payer=newPayer();$payer->setPaymentMethod('paypal');//商品名称、币种、数量、单品金额$item1=newItem();$item1->setName('item1')->setCurrency('USD')->setQuantity(1)->setPrice($request->get('金额'));//将所有产品收集到ItemList$item_list=newItemList();$item_list->setItems([$item1]);$金额=新金额();$amount->setCurrency('USD')->setTotal($request->get('amount'));//生成交易$transaction=newTransaction();$transaction->setAmount($amount)->setItemList($item_list)->setDescription('yourtransaction')->setNotifyUrl(route('notify_url'))//注意,这里设置异步回调地址->setInvoiceNumber($order->order_number);//这里设置订单号//设置前端回调地址和取消支付地址$redirect_urls=newRedirectUrls();$redirect_urls->setReturnUrl(route('payment.paypal.return'))->setCancelUrl(route('payment.paypal.cancel'));$付款=新付款();$payment->setIntent('销售')->setPayer($payer)->setRedirectUrls($redirect_urls)->setTransactions(array($transaction));try{//这里生成支持支付流程$payment->create($this->_api_context);}catch(\PayPal\Exception\PayPalConnectionException$ex){if(config('app.debug')){session()->put('error','Connectiontimeout');returnredirect()->route('paypal-form');/**回显“异常:”。$ex->getMessage()。PHP_EOL;**//**$err_data=json_decode($ex->getData(),true);**//**出口;**/}else{session()->put('error','发生错误,不便之处敬请谅解');returnredirect()->route('paypal-form');}}foreach($payment->getLinks()as$link){if($link->getRel()=='approval_url'){$redirect_url=$link->getHref();休息;}}//$payment->getid()获取支付流水号session()->put('paypal_payment_id',$payment->getId());if(isset($redirect_url)){//跳转到支付页面returnredirect()->away($redirect_url);}session()->put('错误','发生未知错误');returnsession()->route('paypal-form');}publicfunctionpayPalReturn(Request$request){//支付成功后,前端页面跳回时,url地址会有paymentID和PayerID$payment_id=session()->get('paypal_payment_id');session()->forget('paypal_payment_id');if(empty(Input::get('PayerID'))||empty(Input::get('token'))){session()->put('error','支付失败');returnredirect()->route('paypal-form');$payment=Payment::get($payment_id,$this->_api_context);$execution=newPaymentExecution();$execution->setPayerId(Input::get('PayerID'));$result=$payment->execute($execution,$this->_api_context);//当获取到status时,approved表示支付成功returnredirect()->route('paypal-form');}session()->put('错误','支付失败');returnredirect()->route('paypal-form');}publicfunctionpayPalNotify(){Log::info(12312,Input::get());//在这里写我们的业务逻辑,订单状态更新,物流信息等}}Step:4创建表单{{csrf_field()}}has('amount')?'has-error':''}}">金额@if($errors->has('amount')){{$errors->first('amount')}}@endif

PaywithPaypal
以上是我在做商城项目时对接PayPal的过程。因为英语不好的问题,开发中会遇到很多问题。如果是英文嘛,如果想了解更多用法,可以看PayPal的开发者文档和demo