henshall / php-paypal-wrapper
该软件包最新版本(1.0.0)没有可用的许可证信息。
简单的Paypal包装器,用于购买,附带大量示例
1.0.0
2020-09-01 18:13 UTC
Requires
- paypal/rest-api-sdk-php: ^1.13
This package is auto-updated.
Last update: 2024-09-10 18:49:12 UTC
README
版本:1.0.0
这是一个用于Paypal PHP软件包'paypal/rest-api-sdk-php'的包装器。
与Paypal合作可能会非常令人沮丧。他们的沙盒不总是正常工作,他们的文档过时或冗余,如果他们不喜欢任何交易(例如金额过高),他们非常快地封禁和冻结您的账户。
我想创建一个带有基本文档的包装器,以便用户可以尽快接受付款。以下软件包允许用户向您的Paypal账户发送付款,并允许发送参数(如用户ID),这样您就可以轻松跟踪谁已经付款,以便您可以升级他们的账户,或者向他们发送产品,或者您需要做的任何事情。
使用Composer安装
composer require henshall/php-paypal-wrapper
先决条件
- 在https://developer.paypal.com获取Paypal客户端ID/密钥
- 将以下配置文件放在您的代码中的某个位置,并根据您的应用程序更改设置。
return [ /** * get from https://developer.paypal.com */ 'client_id' => 'PAYPAL_CLIENT_ID', /** * get from https://developer.paypal.com */ 'secret' => 'PAYPAL_CLIENT_SECRET', /** * after a user authorizes the payment, they will be directed to this url. This is where we will place the logic from step 2 below. */ 'return_url' => 'RETURN_URL', /** * // url the user is sent to if they cancel the transaction. Maybe you can bring the user back to your homepage with a message saying they cancelled the transaction. */ 'cancel_url' => 'CANCEL_URL', /** * SDK configuration */ 'settings' => array( /** * Available option 'sandbox' or 'live' */ 'mode' => "sandbox", // make sure to change this to live! /** * Specify the max request time in seconds */ 'http.ConnectionTimeOut' => 1000, /** * Whether want to log to a file */ 'log.LogEnabled' => false, /** * Specify the file that want to write on */ 'log.FileName' => '/logs/paypal.log', /** * Available option 'FINE', 'INFO', 'WARN' or 'ERROR' * * Logging is most verbose in the 'FINE' level and decreases as you * proceed towards ERROR */ 'log.LogLevel' => 'FINE', ), ];
使用方法
该软件包完全在后端 - 这样您就可以创建自己喜欢的任何html/css/js前端,当用户想要购买时,只需向应用程序的后端发送一个POST请求并使用此软件包。
步骤1.
用户点击您网站上的“支付”,然后您向后端发送GET/POST请求。
<a href="/pay">Pay</a>
步骤2.
在pay.php文件中,或您将他们重定向到的任何地方,您将看到以下代码。此代码将把他们重定向到Paypal的网站,他们将在那里登录并授权付款。
// Create the class $paypalWrapper = new PaypalWrapper; // Set the config file as shown above in the prerequisites section $config = config('paypal_conf'); $validate = $paypalWrapper->validateConfigFile($config); $paypalWrapper->setConfigFile($config); // Set a Parameter - Here you can set a user_id, name, or email address to track which user has paid. $paypalWrapper->setParam("useremail@email.com"); // Set the amount and the currency type (currency examples below) $paypalWrapper->RedirectToPaypal(100, "USD"); if ($paypalWrapper->error) { // Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.) // die($paypalWrapper->error); } //NOTE you can also check if any of the individual functions fail by see if they return false. // $setConfig = $paypalWrapper->setConfigFile($config); // if (!$setConfig) { // // LOGIC IF FAILED TO SET CONFIG // }
步骤3.
一旦他们授权了付款,他们将根据您在配置文件中设置的'redirect_url'变量被重定向回您的网站。他们将传递两个参数:1) PayerId 2) paymentID - 我们需要这些参数来执行付款。
// Create the class $paypalWrapper = new PaypalWrapper; // Set the config file as shown above in the prerequisites section $config = config('paypal_conf'); $validate = $paypalWrapper->validateConfigFile($config); $paypalWrapper->setConfigFile($config); // Set the payment $paypalWrapper->setPayment($data["PayerID"], $data["paymentId"] ); // Execute the payment $executePayment = $paypalWrapper->executePayment(); if ($paypalWrapper->error) { // Where to put your logic if there is an error with the wrapper itself, or its config } if ($executePayment == true) { // write code for when payment is successful } if ($executePayment == false) { // write code for when payment is NOT successful (ex. insufficient funds, or blocked for some reason) }