注册paypal账号
@H_419_5@www.paypal.com 注册商家账户选择,”创建商家用户”,根据要求填写信息,注册完得去邮箱激活注册paypal开发者账号
@H_419_5@developer.paypal.com 使用上一步注册的账号登录在左侧导航栏点击Accounts,创建两个sandBox账号,一个个人账号(personal)用于付款,一个商家账号(business)用于收款,系统已经默认创建了两个,可以自己选择创建,方便记忆; 创建账号后记得给当前账号添加余额用于接下来的测试。用刚才创建的测试账号登录沙盒测试站点,查看金额和交易信息www.sandBox.paypal.com
创建沙盒应用APP
@H_419_5@账号默认给创建好了一个Default Application,点击查看获取 ClIEntID和Secret;保存起来,接下来接口调用会使用到;(上线时请重新创建一个live的应用)下载SDK
@H_419_5@本人使用laravel框架,使用composer下载paypal包composer require paypal/rest-API-sdk-PHP
实现支付
@H_419_5@以下是生成支付的代码片段,伪代码
@H_301_93@function createPayPal(){ $shipPingPrice = 2; $taxPrice = 0; $subTotal = 26; $item1 = new PayPal\API\Item(); $item1->setname("产品2")->setCurrency("USD")->setQuantity(1)->setPrice(10); $item2 = new PayPal\API\Item(); $item2->setname("产品1")->setCurrency("USD")->setQuantity(2)->setPrice(8); $itemList = new PayPal\API\ItemList(); $itemList->addItems([$item1,$item2]); // Set payment details $details = new PayPal\API\Details(); $details->setShipPing($shipPingPrice)->setTax($taxPrice)->setSubtotal($subTotal); // Set payment amount //注意,此处的subtotal,必须是产品数*产品价格,所有值必须是正确的,否则会报错 $total = $shipPingPrice + $subTotal + $taxPrice; $amount = new PayPal\API\Amount(); $amount->setCurrency("USD")->setTotal($total)->setDetails($details); // Set transaction object $transaction = new PayPal\API\Transaction(); $transaction->setAmount($amount)->setItemList($itemList)->setDescription("这是交易描述") ->setInvoiceNumber(uniqID());//setInvoiceNumber为支付唯一标识符,在使用时建议改成订单号 $payer = new PayPal\API\Payer(); $payer->setPaymentMethod('paypal');//["credit_card", "paypal"] $redirectUrls = new PayPal\API\RedirectUrls(); $redirectUrl = "http://test.com/redirect/success";//支付成功跳转的回调 $cancelUrl = "http://test.com/redirect/cancel";//取消支付的回调 $redirectUrls->setReturnUrl($redirectUrl)->setCancelUrl($cancelUrl); // Create the full payment object $payment = new PayPal\API\Payment(); $payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->addTransaction($transaction); try { $clIEntID = "xxxxxx";//上面应用的clIEntID和secret $secret = "XXXXXX"; $oAuth = new \PayPal\Auth\OAuthTokenCredential($clIEntID, $secret); $APIContext = new \PayPal\Rest\APIContext($oAuth); if(env('APP_DEBUG') === false ){ $APIContext->setConfig(['mode' => 'live']);//设置线上环境,默认是sandBox } $payment->create($APIContext); $approvalUrl = $payment->getApprovallink(); dd($approvalUrl);//这个是请求支付的链接,在浏览器中请求此链接就会跳转到支付页面 } catch (\Exception $e) { dd($e->getMessage());//错误提示 } }以下是支付成功的回调代码片段,伪代码
@H_301_93@function payRedirect(Request $request) { $paymentID = $request->get('paymentID'); $payerID = $request->get('PayerID'); $clIEntID = "xxxxxx";//上面应用的clIEntID和secret $secret = "XXXXXX"; $oAuth = new \PayPal\Auth\OAuthTokenCredential($clIEntID, $secret); $APIContext = new \PayPal\Rest\APIContext($oAuth); if(env('APP_DEBUG') === false ){ $APIContext->setConfig(['mode' => 'live']);//设置线上环境,默认是sandBox } $payment = \PayPal\API\Payment::get($paymentID, $APIContext); $execute = new \PayPal\API\PaymentExecution(); $execute->setPayerID($payerID); try{ $payment = $payment->execute($execute, $APIContext);//执行,从paypal获取支付结果 $paymentState = $payment->getState();//Possible values: created, approved, Failed. $invoiceNum = $payment->getTransactions()[0]->getInvoiceNumber(); $payNum = $payment->getTransactions()[0]->getRelatedResources()[0]->getSale()->getID();//这是支付的流水单号,必须保存,在退款时会使用到 $total = $payment->getTransactions()[0]->getRelatedResources()[0]->getSale()->getAmount()->getTotal();//支付总金额 $transactionState = $payment->getTransactions()[0]->getRelatedResources()[0]->getSale()->getState();//Possible values: completed, partially_refunded, pending, refunded, denIEd. if($paymentState == 'approved' && $transactionState == 'completed'){ //处理成功的逻辑,例如:判断支付金额与订单金额,更新订单状态等 return "success";//返回成功标识 }else{ //paypal回调错误,paypal状态不正确 return "error";//返回错误标识 } }catch(\Exception $e){ dd($e->getMessage()); } }退款
@H_419_5@退款必须要有支付成功的流水单号
一个流水单号可以进行多次退款
退款成功之后,在账号的交易记录中不体现,测试时请登录测试账号自己记录一下余额的变化情况,以确定退款是否成功
商家账号会减少余额并增加手续费(手续费是之前付款多收的),用户账号会增加余额
@H_301_93@function refundOrder() { try{ $refundRequest = new \PayPal\API\refundRequest(); $amount = new \PayPal\API\Amount(); $amount->setCurrency("USD")->setTotal(10);//退总金额 $refundRequest->setAmount($amount); $refundRequest->setDescription("退款测试"); $sale = new \PayPal\API\Sale(); $sale->setID("XXXXXXXXX");//支付单号,支付成功时保存的支付流水单号 $clIEntID = "xxxxxx";//上面应用的clIEntID和secret $secret = "XXXXXX"; $oAuth = new \PayPal\Auth\OAuthTokenCredential($clIEntID, $secret); $APIContext = new \PayPal\Rest\APIContext($oAuth); if(env('APP_DEBUG') === false ){ $APIContext->setConfig(['mode' => 'live']);//设置线上环境,默认是sandBox } $detailedrefund = $sale->refundSale($refundRequest, $APIContext);//调接口 $refundState = $detailedrefund->getState();//Possible values: pending, completed, cancelled, Failed. //var_dump($refundedSale); if($refundState == 'completed'){ //退款成功,返回 }else{ dd('paypal 退款失败, 状态不正确'); } }catch (\Exception $exception){ dd($exception->getMessage());//发生异常 } }更多接口代码示例http://paypal.github.io/PayPal-PHP-SDK/sample/
推荐:《PHP视频教程》总结
以上是内存溢出为你收集整理的详解PHP对接paypal支付全部内容,希望文章能够帮你解决详解PHP对接paypal支付所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)