ricardo93borges/easy-paypal

此软件包的最新版本(1.4.1)没有提供许可信息。

一个用于简化PayPal使用的PHP库

1.4.1 2016-07-08 16:10 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:36:58 UTC


README

Easy Paypal

Easy paypal是一个SDK,用于简化PayPal NVP API和PayPal IPN与您的应用程序的集成。

先决条件

  • PHP >= 5.3
  • Curl扩展
  • Composer

示例

创建请求对象

API请求是通过一个请求对象进行的,此对象必须使用以下必选参数创建

  • $sandbox = boolean,如果为true,将使用端点sandbox.paypal.com/br/cgi-bin/webscr,否则使用paypal.com/br/cgi-bin/webscr
  • $user;
  • $password;
  • $signature;
  • $returnUrl = 买家确认购买后的返回URL

可选参数

  • $cancelUrl = 买家取消购买后的返回URL
  • $headerImage = 用于确认页面装饰的图片,此处可以使用您的商店的标志。
  • $buttonSource;
  • $localecode = 语言,默认值: 'pt_BR';
  • $version = API版本,默认值: '73.0';
  • $currencyCode = 货币,默认值: 'BRL'
  • $countryCode = 国家,默认值: 'BR'
  • $notifyUrl = 用于发送与此请求对象相关的交易通知的URL
include "autoload.php";

$sandbox = true;
$username = "conta-business_api1.test.com";
$password = "123456";
$signature = "AiPC9BjkCyDFQXbSkoZcgqH3hpacA-p.YLGfQjc0EobtODs.fMJNajCx";

$appUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$returnUrl = $appUrl;
$cancelUrl = $appUrl;

$request = new \easyPaypal\Request($sandbox, $username, $password, $signature, $returnUrl, $cancelUrl);
$request->setHeaderImage('path/to/my/image');
$request->setLocalecode('pt_BR');
快速结账

Express Checkout是PayPal的一个支付解决方案,适用于拥有中等和大型规模集成功能的网站和在线商店。要进行快速结账,创建一个Checkout对象并将其与一个请求对象关联。每个请求可以创建多达10个交易,这通过创建Seller对象并将Item对象分配给它们来实现。每个Seller将生成一个交易。Seller对象通过setParams()方法分配给Checkout。

创建Seller时使用以下可选参数

  • $reference = 商家用于控制的订单号。此字段描述了客户在其自己的商店中的订单号。它是您的内部标识符,可以用于PayPal在通知期间帮助您识别交易。如果省略或传递null,将生成一个随机字符串。
  • $paymentAction = 指定此交易的行动,默认值是'SALE'。
  • $currencyCode = 指定货币,默认值是'BRL'

必须在调用send()方法之前调用setParams()方法,以便如果需要,请求将使用附加参数更新。

每个请求的支付/卖家限制为10。

include "autoload.php";

//Referencia / Invoice ID: Campo para o acompanhamento e controle interno do comerciante
$ref=null;

$request = new \easyPaypal\Request($sandbox, $username, $password, $signature, $returnUrl, $cancelUrl, $logoUrl);
$nvp = new \easyPaypal\Checkout('expressCheckout');
$nvp->setRequest($request);

//Create sellers
$seller = new \easyPaypal\Seller($ref);
//Add itens to sellers
$item1 = new \easyPaypal\Item('Item 1', 'Description', 40.00, 1);
$item2 = new \easyPaypal\Item('Item 2', 'Description', 40.00, 1);

$seller->addItem($item1);
$seller->addItem($item2);

//Set request
$nvp->setParams($seller);
$response = $nvp->send();

快速结账的逐步示例可以在examples/express_checkout/checkout_step_by_step.php中找到。

创建重复性配置文件

要创建重复性配置文件,使用一个Recurring对象,此对象使用以下参数创建

  • $profileStartDate = 配置文件开始日期,默认值:当前日期 + 1小时
  • $billingPeriod = 期限,接受的值:Day,Week,Month,Year,默认值:Month
  • $billingFrequency = 构成1个支付周期的周期数,默认值:1
  • $amount = 每个支付周期将被收取的金额。必选参数。

可选参数

  • $totalBillingCycles = 在重复性配置文件关闭之前的总支付周期数,如果未提供,则配置文件将无限期存在。
  • $maxFailedPayments = 在配置文件自动取消之前可能失败的支付次数的最大值
  • 如果当前支付失败,则在下个周期内将自动向PayPal发出指令,收取“应付金额”。每当定期支付或首次支付失败时,应收取的金额将添加到应付金额中。

首次非定期支付

除了定义定期支付配置文件和试用期外,我们还可以定义在创建配置文件时需要执行的支付。为此,需要定义以下参数

  • $initAmt = 应收取的初始金额。

  • $failedInitAmtAction = 如果首次支付失败时采取的操作,接受的值有ContinueOnFailureCancelOnFailure

  • ContinueOnFailure – 如果支付失败,将创建定期支付配置文件,并将初始金额放入配置文件的应付金额中。

  • CancelOnFailure – 如果首次支付失败,则不会创建定期支付配置文件。

用例示例

客户购买每月价值10.00元的杂志订阅,有效期为1年。每3个月收取一次费用。

  • $billingPeriod = 月
  • $billingFrequency = 3
  • $amount = 10.00
  • $totalBillingCycles = 4

这将为每月创建一个支付配置文件,客户每3个月支付相当于30.00元的订阅费,为期1年。

每个请求的支付/卖家限制为10。

$request = new \easyPaypal\Request(true, $username, $password, $signature, $returnUrl, $cancelUrl, $logoUrl);

//Criando objeto Recurring usando parâmetros default
$method = 'expressCheckout';
$amount = 100;
$description = 'Recurring payment test';
$nvp = new \easyPaypal\Recurring($method, $amount, $description);

//Alterando parâmetro do objeto Recurring
$nvp->setTotalBillingCycles(12);
$nvp->setBillingPeriod('Week');
$nvp->setBillingFrequency(4);

$nvp->setRequest($request);
//Create sellers
$seller = new \easyPaypal\Seller();
//Add itens to sellers
$item1 = new \easyPaypal\Item('Item 1', 'Description', 40.00, 1, 'RecurringPayments', 'Recurring payment item');
$seller->addItem($item1);
//Set request
$nvp->setParams($seller);
$response = $nvp->send($seller);
带试用期的定期配置文件

对于此类配置文件,需要指定更多4个参数

  • $trialBillingPeriod = 试用期周期的频率,接受的值有:Day、Week、Month、Year。
  • $trialBillingFrequency = 构成1个周期的周期数。
  • $trialAmt = 试用期期间将收取的金额。
  • $trialTotalBillingCycles = 试用期总周期数。与常规配置文件创建不同,此参数是必需的。
$request = new \easyPaypal\Request(true, $username, $password, $signature, $returnUrl, $cancelUrl, $logoUrl);
$nvp = new \easyPaypal\Recurring('expressCheckout', 100, 'Recurring payment test');
$nvp->setRequest($request);
//Trial
$nvp->setTrialAmt(0);
$nvp->setTrialBillingFrequency(1);
$nvp->setTrialBillingPeriod('Month');//(Month|Day|Week|Year)
$nvp->setTrialTotalBillingCycles(1);
//Create sellers
$seller = new \easyPaypal\Seller();
//Add itens to sellers
$item1 = new \easyPaypal\Item('Item 1', 'Description', 40.00, 1, 'RecurringPayments', 'Recurring payment item');
$seller->addItem($item1);
//Set request
$nvp->setParams($seller);
$response = $nvp->send($seller);
退款

全额或部分退款只能针对60天内创建的交易进行。退款是通过Transaction类的refundTransaction方法进行的,此方法接受以下参数

  • $transactionId = 交易ID,必需参数。
  • $refundType = 退款类型,必需参数,接受的值有:Full或Partial。
  • $amount = 将退还的金额,部分退款(Partial)时必需。
  • $currencyCode = 退款使用的货币,部分退款(Partial)时必需。例如:BRL。
  • $note = 退款原因,可选。
  • $payerId = 买家ID,可选。
  • $invoiceId = 购买内部标识符(参考),可选。

以下示例中,搜索过去30天内进行的所有交易,然后对其中一个执行全额退款,对另一个执行部分退款。

$request = new \easyPaypal\Request(true, $username, $password, $signature, $returnUrl, $cancelUrl, $logoUrl);
//Create Transaction Object
$transaction = new \easyPaypal\Transaction();
//Set start and end date
$startDate = new \DateTime();
$endtDate = new \DateTime();
$startDate->sub(new DateInterval('P30D'));
$transaction->setStartDate($startDate);
$transaction->setEndDate($endtDate);
//Set request
$transaction->setRequest($request);

//Search transactions by start and end dates
$transactions = $transaction->transactionSearch();

//Get transaction details by ID
$details = $transaction->getTransactionDetails($transactions[0]->getTxnId());
//Full Refund
$response = $transaction->refundTransaction($details->getTxnId(), 'Full');
if(isset($response['ACK']) && $response['ACK'] == "Success"){
    echo "Success on Full Refund transaction. REFUNDTRANSACTIONID: ".$response['REFUNDTRANSACTIONID']."<br/><br/>";
}

//Get transaction details
$details = $transaction->getTransactionDetails($transactions[1]->getTxnId());
//Partial Refund
$response = $transaction->refundTransaction($details->getTxnId(), 'Partial', 1, $details->getCurrencyCode(), "Partial Refund test", $details->getCustomer()->getPaypalId());
if(isset($response['ACK']) && $response['ACK'] == "Success"){
    echo "Success on Full Refund transaction. REFUNDTRANSACTIONID: ".$response['REFUNDTRANSACTIONID']."<br/><br/>";
}
与IPN集成

即时支付通知(IPN)是一种消息服务,它通知您的应用程序有关PayPal交易的有关事件。它可以用于自动化您的后台办公室或管理功能。

流程

  • 1- PayPal通知服务向您的应用程序发送包含IPN消息的HTTP POST请求;
  • 2- 您的通知处理程序返回一个HTTP 200状态,不带内容;
  • 3- 您的通知处理程序按照相同的顺序,使用接收到的相同字段和编码,向PayPal发送一个HTTP POST请求;
  • 4- PayPal返回一个只包含VERIFIED(如果消息有效)或INVALID(如果消息无效)的简单字符串;

Ipn类具有一个处理程序(上述描述的处理程序),它简化了此流程。

配置通知URL

  • 访问您的PayPal账户
  • 访问配置文件 > 更多选项
  • 访问通知偏好
  • 告知处理程序的URL

接收通知时采取的操作非常具体于应用程序。

在 examples/ipn/ 目录下有一个示例处理程序,该处理程序将接收到的通知数据存储在数据库中。在 ipn.php 文件中,期望一个 HTTP POST 请求发送到 Ipn 类的 handleIpn() 方法。在这个方法中,如果发生错误,则返回错误;如果没有错误,则返回一个包含 NotificationCustomerTransaction 对象的数组,并将这些对象存储在数据库中。

获取一个重复性配置文件的详细信息
$request = new \easyPaypal\Request(true, $username, $password, $signature, $returnUrl, $cancelUrl, $logoUrl);
$nvp = new \easyPaypal\Recurring();
$nvp->setRequest($request);

$response = $nvp->getRecurringProfileDetails($profileId);
var_dump($response);
获取交易记录
$request = new \easyPaypal\Request(true, $username, $password, $signature, $returnUrl, $cancelUrl, $logoUrl);
//Create Transaction Object
$transaction = new \easyPaypal\Transaction();
//Set start and end date
$startDate = new \DateTime();
$endtDate = new \DateTime();
$startDate->sub(new DateInterval('P30D'));
$transaction->setStartDate($startDate);
$transaction->setEndDate($endtDate);
//Set request
$transaction->setRequest($request);
//Search transaction
$response = $transaction->transactionSearch();

foreach($response as $t){
    echo $t->getCustomer()->getFirstName()."<br/>";
    echo $t->getCustomer()->getLastName()."<br/>";
    echo $t->getCustomer()->getEmail()."<br/>";
    echo $t->getPaymentDate()."<br/>";
    echo $t->getTxnId()."<br/>";
    echo $t->getPaymentStatus()."<br/>";
    echo $t->getTxnType()."<br/>";
    echo $t->getGross()."<br/>";
    echo $t->getCurrencyCode()."<br/>";
    echo $t->getFee()."<br/>";
    echo "<br/><br/>";
}
获取交易详情
$request = new \easyPaypal\Request(true, $username, $password, $signature, $returnUrl, $cancelUrl, $logoUrl);
//Create Transaction Object
$transaction = new \easyPaypal\Transaction();
//Set start and end date
$startDate = new \DateTime();
$endtDate = new \DateTime();
$startDate->sub(new DateInterval('P30D'));
$transaction->setStartDate($startDate);
$transaction->setEndDate($endtDate);
//Set request
$transaction->setRequest($request);
//Search transaction
$transactions = $transaction->transactionSearch();

//Get transaction details
$details = $transaction->getTransactionDetails($transactions[0]->getTxnId());

echo $details->getCustomer()->getFirstName()."<br/>";
echo $details->getCustomer()->getLastName()."<br/>";
echo $details->getCustomer()->getEmail()."<br/>";
echo $details->getPaymentDate()."<br/>";
echo $details->getTxnId()."<br/>";
echo $details->getTxnType()."<br/>";
echo $details->getPaymentStatus()."<br/>";
echo $details->getGross()."<br/>";
echo $details->getCurrencyCode()."<br/>";
echo $details->getFee()."<br/>";