payment-gateways/paypal-sdk
Composer 安装命令:
composer require payment-gateways/paypal-sdk
包简介
PayPal SDK
README 文档
README
PayPal SDK
This is an SDK for PayPal REST APIS. The following APIs are currently supported.
Installation
Use following command to install this library:
composer require payment-gateways/paypal-sdk
Usage
Authentication
Go to PayPal Developer site and get the Client ID and Secret for your app. These credentials can be used in the service authentication as follows:
use PaymentGateway\PayPalSdk\Api\SubscriptionsApi; $service = new SubscriptionsApi('https://api.sandbox.paypal.com'); $service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
Catalog Products API
Merchants can use the Catalog Products API to create products, which are goods and services.
List products
To get all products use the getProducts method.
use PaymentGateway\PayPalSdk\Api\CatalogProductsApi; $service = new CatalogProductsApi('https://api.sandbox.paypal.com'); $service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk'); $response = $service->getProducts()->toArray();
Get a product
To get a single products use the getProduct method.
use PaymentGateway\PayPalSdk\Api\CatalogProductsApi; $service = new CatalogProductsApi('https://api.sandbox.paypal.com'); $service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk'); $response = $service->getProduct('PROD-8R6565867F172242R')->toArray();
Create a product
To create a product use the createProduct method.
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();
Update a product
To update a product use the updateProduct method.
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);
Billing Plans API
You can use billing plans and billing agreements to create an agreement for a recurring PayPal or debit card payment for goods or services.
List plans
To get all plans use the getPlans method.
use PaymentGateway\PayPalSdk\Api\BillingPlansApi; $service = new BillingPlansApi('https://api.sandbox.paypal.com'); $service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk'); $response = $service->getPlans()->toArray();
Get a plan
To get a single plan use the getPlan method.
use PaymentGateway\PayPalSdk\Api\BillingPlansApi; $service = new BillingPlansApi('https://api.sandbox.paypal.com'); $service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk'); $response = $service->getPlan('P-18T532823A424032WL7NIVUA')->toArray();
Create a plan
To create a product use the createPlan method.
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();
Update a plan
To update a product use the updatePlan method.
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);
Subscriptions API
You can use this API to create subscriptions that process recurring PayPal payments for physical or digital goods, or services.
Get a subscription
To get a single subscription use the getSubscription method.
use PaymentGateway\PayPalSdk\Api\SubscriptionsApi; $service = new SubscriptionsApi('https://api.sandbox.paypal.com'); $service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk'); $response = $service->getSubscription('I-18T532823A424032WL7NIVUA')->toArray();
Create a subscription
To create a subscription use the createSubscription method.
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);
Error Handling
In general, You can use the toArray method in all responses to get the service response data (except for patch operations),
otherwise you'll get an array with errors if something fails. You can check for a successful response using the isSuccessful method.
.
$response = $service->getProducts(); if (!$response->isSuccessful()) { var_dump($response->toArray()); // check the errors } else { // array with data $response->toArray(); }
Mocking
You can create your own PayPal API Mock for use with PayPalService like this
use PaymentGateway\PayPalSdk\Api\SubscriptionsApi; $service = new SubscriptionsApi('https://api.sandbox.paypal.com'); $service->withHandler($handler);
The handler must be a callable type. If you prefer, you can use the PayPal API Mock
adding the following to your project
composer require --dev payment-gateways/paypal-api-mock
After, you could use the class PayPalApiMock as a handler
use PaymentGateway\PayPalSdk\Api\SubscriptionsApi; use PaymentGateway\PayPalApiMock\PayPalApiMock; $service = new SubscriptionsApi('https://api.sandbox.paypal.com'); $service->withHandler(new PayPalApiMock());
payment-gateways/paypal-sdk 适用场景与选型建议
payment-gateways/paypal-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.35k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 payment-gateways/paypal-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 payment-gateways/paypal-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-11