speckcommerce/

speck-paypal

一个通用的模块,用于将PayPal支持添加到ZF2应用程序中。

dev-master 2015-04-30 13:27 UTC

This package is auto-updated.

Last update: 2024-09-13 01:58:15 UTC


README

一个通用的模块,用于将PayPal支付支持添加到ZF2应用程序中。

Build Status

简介

SpeckPaypal是一个模块,可以在Speck Commerce之外使用,以接受PayPal支付。此模块目前支持PayPal Payments Pro和Express Checkout API操作。

请参阅:Paypal API文档

要集成此模块,您需要在PayPal上注册沙箱账户。请参阅开发者网站以获取说明。

此模块目前支持以下API版本95.0的调用

  • 回调
  • 执行授权
  • 执行捕获
  • 执行直接支付
  • 执行Express Checkout支付
  • 执行撤销
  • 获取余额
  • 获取Express Checkout详情
  • 获取交易详情
  • 退款交易
  • 设置Express Checkout
  • 交易搜索
  • 更新周期性支付配置文件
  • 管理周期性支付配置文件状态
  • 创建周期性支付配置文件

需求

SpeckCommerce的依赖关系设置为Git子模块,因此您不应遇到

  • PHP 5.4+(注意:此库应与PHP 5.3.3+兼容,但不再提供官方支持)
  • Zend Framework 2(最新master版)

贡献者

社区

加入Freenode IRC网络:#speckcommerce。我们目前人数不多,但我们是一个全职致力于此项目的专注小团体。

示例用法

创建PayPal请求对象

//setup config object
$config = array(
    'username'      => 'your_username',
    'password'      => 'your_password',
    'signature'     => 'your_signature',
    'endpoint'      => 'https://api-3t.sandbox.paypal.com/nvp' //this is sandbox endpoint
)
$paypalConfig = new \SpeckPaypal\Element\Config($config);

//set up http client
$client = new \Zend\Http\Client;
$client->setMethod('POST');
$client->setAdapter(new \Zend\Http\Client\Adapter\Curl);
$paypalRequest = new \SpeckPaypal\Service\Request;
$paypalRequest->setClient($client);
$paypalRequest->setConfig($paypalConfig);

直接支付示例(默认情况下,请求以“销售”发送,相当于授权捕获)

$paymentDetails = new \SpeckPaypal\Element\PaymentDetails(array(
    'amt' => '10.00'
));

$payment = new \SpeckPaypal\Request\DoDirectPayment(array('paymentDetails' => $paymentDetails));
$payment->setCardNumber('4744151425799438');
$payment->setExpirationDate('112017');
$payment->setFirstName('John');
$payment->setLastName('Canyon');
$payment->setIpAddress('255.255.255.255');
$payment->setCreditCardType('Visa');
$payment->setCvv2('345');

$address = new \SpeckPaypal\Element\Address;
$address->setStreet('27 Your Street');
$address->setStreet2('Apt 23');
$address->setCity('Some City');
$address->setState('California');
$address->setZip('92677');
$address->setCountryCode('US');
$payment->setAddress($address);

$response = $paypalRequest->send($payment);

echo $response->getTransactionId();

Express Checkout示例

在使用此API之前,了解PayPal的Express Checkout流程非常重要。Paypal Express Checkout Flow

为了将用户重定向到PayPal,我们首先需要获取一个令牌。

$paymentDetails = new \SpeckPaypal\Element\PaymentDetails(array(
    'amt' => '20.00'
));
$express = new \SpeckPaypal\Request\SetExpressCheckout(array('paymentDetails' => $paymentDetails));
$express->setReturnUrl('http://www.someurl.com/return');
$express->setCancelUrl('http://www.someurl.com/cancel');

$response = $paypalRequest->send($express);

echo $response->isSuccess();

$token = $response->getToken();

收到令牌后,您需要将用户转发到PayPal服务器,包括您收到的令牌。请参阅文档中的URL。一旦用户在PayPal完成结账流程,他们将被重定向到您在SetExpressCheckout中提供的URL。当用户到达该页面时,您需要向PayPal发出回调,包括令牌以接收买家的付款详情。您将使用响应中包含的payerId通过DoExpressCheckoutPayment捕获付款。

$details = new \SpeckPaypal\Request\GetExpressCheckoutDetails(array('token' => $token));

$response = $paypalRequest->send($details);

$payerId = $response->getPayerId();

现在,您可以通过调用DoExpressCheckoutPayment来捕获付款。

//To capture express payment
$captureExpress = new \SpeckPaypal\Request\DoExpressCheckoutPayment(array(
    'token'             => $token,
    'payerId'           => $payerId,
    'paymentDetails'    => $paymentDetails
));
$response = $paypalRequest->send($captureExpress);

echo $response->isSuccess();

交易搜索示例

$transactionSearch new \SpeckPaypal\Request\TransactionSearch();
$transactionSearch->setStartDate('2014-06-21T00:00:00Z');

$paypalRequest = $serviceManager->get('SpeckPaypal\Service\Request');
$response = $paypalRequest->send($transactionSearch);

var_dump($response->getResults());

待办事项

  • 根据PayPal要求进行更严格的验证(当前验证较为宽松)
  • 重构为相关的异常类
  • 添加对eBay物品、调查问题……以及其他缺失的支付专业API的支持