payment-gateways/paypal-sdk

v1.2.0 2021-02-03 13:56 UTC

This package is auto-updated.

Last update: 2024-09-17 19:40:11 UTC


README

Build Status Code Quality Code Coverage

PayPal SDK

这是一个用于 PayPal REST API 的 SDK。当前支持的 API 如下。

Bugs Bugs Bugs

安装

使用以下命令安装此库

composer require payment-gateways/paypal-sdk

使用方法

身份验证

前往 PayPal 开发者网站 并获取您的应用的 客户端 ID密钥。这些凭据可以在以下服务身份验证中使用

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

商品目录 API

商家可以使用商品目录 API 创建产品,这些产品是商品和服务。

列出产品

要获取所有产品,请使用 getProducts 方法。

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getProducts()->toArray();

获取单个产品

要获取单个产品,请使用 getProduct 方法。

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getProduct('PROD-8R6565867F172242R')->toArray();

创建产品

要创建产品,请使用 createProduct 方法。

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;
use PaymentGateway\PayPalSdk\Products\Constants\ProductType;
use PaymentGateway\PayPalSdk\Products\Constants\ProductCategory;
use PaymentGateway\PayPalSdk\Products\Requests\StoreProductRequest;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$productRequest = new StoreProductRequest('My new product', ProductType::SERVICE);
$productRequest->setProductDescription('product description')
    ->setProductCategory(ProductCategory::SOFTWARE)
    ->setImageUrl('https://example.com/productimage.jpg')
    ->setHomeUrl('https://example.com');

// ['id' => 'PROD-XY...', 'name' => 'My new product', ...]
$response = $service->createProduct($productRequest)->toArray();

更新产品

要更新产品,请使用 updateProduct 方法。

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;
use PaymentGateway\PayPalSdk\Products\Constants\ProductCategory;
use PaymentGateway\PayPalSdk\Products\Requests\UpdateProductRequest;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$productRequest = new UpdateProductRequest('PROD-XY458712546854478');
$productRequest->setProductDescription('product description')
    ->setProductCategory(ProductCategory::ACADEMIC_SOFTWARE)
    ->setImageUrl('https://example.com/productimage.jpg')
    ->setHomeUrl('https://example.com');

$service->updateProduct($productRequest);

计费计划 API

您可以使用计费计划和计费协议创建一个协议,用于为商品或服务创建重复的 PayPal 或借记卡支付。

列出计划

要获取所有计划,请使用 getPlans 方法。

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getPlans()->toArray();

获取单个计划

要获取单个计划,请使用 getPlan 方法。

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getPlan('P-18T532823A424032WL7NIVUA')->toArray();

创建计划

要创建产品,请使用 createPlan 方法。

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;
use PaymentGateway\PayPalSdk\Subscriptions\Frequency;
use PaymentGateway\PayPalSdk\Subscriptions\BillingCycles\BillingCycleSet;
use PaymentGateway\PayPalSdk\Subscriptions\BillingCycles\RegularBillingCycle;
use PaymentGateway\PayPalSdk\Subscriptions\Constants\CurrencyCode;
use PaymentGateway\PayPalSdk\Subscriptions\Money;
use PaymentGateway\PayPalSdk\Subscriptions\PricingSchema;
use PaymentGateway\PayPalSdk\Plans\Requests\StorePlanRequest;
use PaymentGateway\PayPalSdk\Subscriptions\Constants\IntervalUnit;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$frequency = new Frequency(IntervalUnit::MONTH, 1);
$pricingSchema = new PricingSchema(new Money(CurrencyCode::UNITED_STATES_DOLLAR, '350'));
$billingCycle = new RegularBillingCycle($frequency, $pricingSchema);
$billingCycleSet = new BillingCycleSet();
$billingCycleSet->addBillingCycle($billingCycle);
$planRequest = new StorePlanRequest('PROD-8R6565867F172242R', 'New Plan', $billingCycleSet);

// ['id' => 'P-XY...', 'product_id' => 'PROD-8R6565867F172242R', 'name' => 'My Plan', ...]
$response = $service->createPlan($planRequest)->toArray();

更新计划

要更新产品,请使用 updatePlan 方法。

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;
use PaymentGateway\PayPalSdk\Subscriptions\Constants\CurrencyCode;
use PaymentGateway\PayPalSdk\Subscriptions\Money;
use PaymentGateway\PayPalSdk\Plans\Requests\UpdatePlanRequest;
use PaymentGateway\PayPalSdk\Subscriptions\PaymentPreferences;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$paymentPreferences = new PaymentPreferences();
$paymentPreferences->setSetupFee(new Money(CurrencyCode::UNITED_STATES_DOLLAR, '250'));
$planRequest = new UpdatePlanRequest('P-18T532823A424032WL7NIVUA');
$planRequest->setPaymentPreferences($paymentPreferences);

$service->updatePlan($planRequest);

订阅 API

您可以使用此 API 创建订阅,该订阅用于处理实物或数字商品或服务的重复 PayPal 支付。

获取订阅

要获取单个订阅,请使用 getSubscription 方法。

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getSubscription('I-18T532823A424032WL7NIVUA')->toArray();

创建订阅

要创建订阅,请使用 createSubscription 方法。

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;
use PaymentGateway\PayPalSdk\Subscriptions\Requests\StoreSubscriptionRequest;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$subscriptionRequest = new StoreSubscriptionRequest('P-18T532823A424032WL7NIVUA');

// ["status": "APPROVAL_PENDING", "id": "I-GFCGPH9100S7", ...]
$response = $service->createSubscription($subscriptionRequest);

错误处理

通常,您可以在所有响应中使用 toArray 方法获取服务响应数据(除补丁操作外),否则如果出现问题,您将获得包含错误的数组。您可以使用 isSuccessful 方法检查响应是否成功。

$response = $service->getProducts();

if (!$response->isSuccessful()) {
    var_dump($response->toArray());     // check the errors
} else {
    // array with data
    $response->toArray();
}

模拟

您可以创建自己的 PayPal API 模拟以用于 PayPalService,如下所示

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->withHandler($handler);

处理程序必须为 callable 类型。如果您愿意,可以使用 PayPal API 模拟,将以下内容添加到您的项目中

composer require --dev payment-gateways/paypal-api-mock

然后,您可以使用 PayPalApiMock 类作为处理程序

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;
use PaymentGateway\PayPalApiMock\PayPalApiMock;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->withHandler(new PayPalApiMock());