PHP Класс Omnipay\PayPal\RestGateway

This class forms the gateway class for PayPal REST requests via the PayPal REST APIs. The PayPal API uses HTTP verbs and a RESTful endpoint structure. OAuth 2.0 is used as the API Authorization framework. Request and response payloads are formatted as JSON. The PayPal REST APIs are supported in two environments. Use the Sandbox environment for testing purposes, then move to the live environment for production processing. When testing, generate an access token with your test credentials to make calls to the Sandbox URIs. When you’re set to go live, use the live credentials assigned to your app to generate a new access token to be used with the live URIs. ### Test Mode In order to use this for testing in sandbox mode you will need at least two sandbox test accounts. One will need to be a business account, and one will need to be a personal account with credit card details. To create these you will need to go to the sandbox accounts section of the PayPal developer dashboard, here: https://developer.paypal.com/webapps/developer/applications/accounts On that page click "Create Account" and follow the prompts. When you are creating the Personal account, ensure that it is created with a credit card -- either Visa or MasterCard or one of the other types. When you are testing in the sandbox, use the credit card details you will receive for this Personal account rather than any other commonly used test credit card numbers (e.g. visa card 4111111111111111 or 4444333322221111 both of which will result in Error 500 / INTERNAL_SERVICE_ERROR type errors from the PayPal gateway). With each API call, you’ll need to set request headers, including an OAuth 2.0 access token. Get an access token by using the OAuth 2.0 client_credentials token grant type with your clientId:secret as your Basic Auth credentials. For more information, see Make your first call (link). This class sets all of the headers associated with the API call for you, including making preliminary calls to create or update the OAuth 2.0 access token before each call you make, if required. All you need to do is provide the clientId and secret when you initialize the gateway, or use the set*() calls to set them after creating the gateway object. ### Credentials To create production and sandbox credentials for your PayPal account: * Log into your PayPal account. * Navigate to your Sandbox accounts at https://developer.paypal.com/webapps/developer/applications/accounts to ensure that you have a valid sandbox account to use for testing. If you don't already have a sandbox account, one can be created on this page. You will actually need 2 accounts, a personal account and a business account, the business account is the one you need to use for creating API applications. * Check your account status on https://developer.paypal.com/webapps/developer/account/status to ensure that it is valid for live transactions. * Navigate to the My REST apps page: https://developer.paypal.com/webapps/developer/applications/myapps * Click *Create App* * On the next page, enter an App name and select the sandbox account to use, then click *Create app*. * On the next page the sandbox account, endpoint, Client ID and Secret should be displayed. Record these. The Sandbox account should match the one that you selected on the previous page, and the sandbox endpoint should be ai.sandbox.paypal.com * Adjacent to *Live credentials* click *Show* to display your live credentials. The endpoint for these should be api.paypal.com, there should also be a Client ID and Secret. You can create additional REST APIs apps for other websites -- because the webhooks are stored per app then it pays to have one API app per website that you are using (and an additional one for things like command line testing, etc). ### Example #### Initialize Gateway Create a gateway for the PayPal RestGateway (routes to GatewayFactory::create) $gateway = Omnipay::create('PayPal_Rest'); Initialise the gateway $gateway->initialize(array( 'clientId' => 'MyPayPalClientId', 'secret' => 'MyPayPalSecret', 'testMode' => true, // Or false when you are ready for live transactions )); #### Direct Credit Card Payment Create a credit card object DO NOT USE THESE CARD VALUES -- substitute your own see the documentation in the class header. $card = new CreditCard(array( 'firstName' => 'Example', 'lastName' => 'User', 'number' => '4111111111111111', 'expiryMonth' => '01', 'expiryYear' => '2020', 'cvv' => '123', 'billingAddress1' => '1 Scrubby Creek Road', 'billingCountry' => 'AU', 'billingCity' => 'Scrubby Creek', 'billingPostcode' => '4999', 'billingState' => 'QLD', )); Do a purchase transaction on the gateway try { $transaction = $gateway->purchase(array( 'amount' => '10.00', 'currency' => 'AUD', 'description' => 'This is a test purchase transaction.', 'card' => $card, )); $response = $transaction->send(); $data = $response->getData(); echo "Gateway purchase response data == " . print_r($data, true) . "\n"; if ($response->isSuccessful()) { echo "Purchase transaction was successful!\n"; } } catch (\Exception $e) { echo "Exception caught while attempting authorize.\n"; echo "Exception type == " . get_class($e) . "\n"; echo "Message == " . $e->getMessage() . "\n"; } ### Dashboard Once you have processed some payments you can go to the PayPal sandbox site, at https://www.sandbox.paypal.com/ and log in with the email address and password of your PayPal sandbox business test account. You will then see the result of those transactions on the "My recent activity" list under the My Account tab.
Наследование: extends Omnipay\Common\AbstractGateway
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
authorize ( array $parameters = [] ) : RestAuthorizeRequest Create an authorization request.
cancelSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCancelSubscriptionRequest Cancel a subscription.
capture ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCaptureRequest Capture an authorization.
completePurchase ( array $parameters = [] ) : AbstractRestRequest Completes a purchase request.
completeSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCompleteSubscriptionRequest Complete (execute) a subscription.
createCard ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCreateCardRequest Store a credit card in the vault
createPlan ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCreatePlanRequest Create a billing plan.
createRequest ( string $class, array $parameters = [] ) : AbstractRestRequest Create Request
createSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCreateSubscriptionRequest Create a subscription.
createToken ( ) : RestTokenRequest Create OAuth 2.0 access token request.
deleteCard ( array $parameters = [] ) : Omnipay\PayPal\Message\RestDeleteCardRequest Delete a credit card from the vault.
fetchPurchase ( array $parameters = [] ) : Omnipay\PayPal\Message\RestFetchPurchaseRequest Fetch a purchase request.
fetchTransaction ( array $parameters = [] ) : Omnipay\PayPal\Message\RestFetchTransactionRequest Fetch a Sale Transaction
getClientId ( ) : string Get OAuth 2.0 client ID for the access token.
getDefaultParameters ( )
getName ( )
getSecret ( ) : string Get OAuth 2.0 secret for the access token.
getToken ( boolean $createIfNeeded = true ) : string Get OAuth 2.0 access token.
getTokenExpires ( ) : integer Get OAuth 2.0 access token expiry time.
hasToken ( ) : boolean Is there a bearer token and is it still valid?
listPurchase ( array $parameters = [] ) : Omnipay\PayPal\Message\RestListPurchaseRequest List purchase requests.
purchase ( array $parameters = [] ) : Omnipay\PayPal\Message\RestPurchaseRequest Create a purchase request.
reactivateSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestReactivateSubscriptionRequest Reactivate a suspended subscription.
refund ( array $parameters = [] ) : Omnipay\PayPal\Message\RestRefundRequest Refund a Sale Transaction
refundCapture ( array $parameters = [] ) : RestRefundCaptureRequest Refund a Captured Payment
searchTransaction ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCompleteSubscriptionRequest Search for transactions.
setClientId ( string $value ) : RestGateway Set OAuth 2.0 client ID for the access token.
setSecret ( string $value ) : RestGateway Set OAuth 2.0 secret for the access token.
setToken ( string $value ) : RestGateway Set OAuth 2.0 access token.
setTokenExpires ( integer $value ) : RestGateway Set OAuth 2.0 access token expiry time.
suspendSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestSuspendSubscriptionRequest Suspend a subscription.
updatePlan ( array $parameters = [] ) : Omnipay\PayPal\Message\RestUpdatePlanRequest Update a billing plan.
void ( array $parameters = [] ) : RestVoidRequest Void an authorization.

Описание методов

authorize() публичный Метод

To collect payment at a later time, first authorize a payment using the /payment resource. You can then capture the payment to complete the sale and collect payment.
public authorize ( array $parameters = [] ) : RestAuthorizeRequest
$parameters array
Результат Omnipay\PayPal\Message\RestAuthorizeRequest

cancelSubscription() публичный Метод

Use this call to cancel an agreement.
public cancelSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCancelSubscriptionRequest
$parameters array
Результат Omnipay\PayPal\Message\RestCancelSubscriptionRequest

capture() публичный Метод

Use this resource to capture and process a previously created authorization. To use this resource, the original payment call must have the intent set to authorize.
public capture ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCaptureRequest
$parameters array
Результат Omnipay\PayPal\Message\RestCaptureRequest

completePurchase() публичный Метод

Completes a purchase request.
public completePurchase ( array $parameters = [] ) : AbstractRestRequest
$parameters array
Результат Omnipay\PayPal\Message\AbstractRestRequest

completeSubscription() публичный Метод

Use this call to execute an agreement after the buyer approves it.
public completeSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCompleteSubscriptionRequest
$parameters array
Результат Omnipay\PayPal\Message\RestCompleteSubscriptionRequest

createCard() публичный Метод

You can currently use the /vault API to store credit card details with PayPal instead of storing them on your own server. After storing a credit card, you can then pass the credit card id instead of the related credit card details to complete a payment.
public createCard ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCreateCardRequest
$parameters array
Результат Omnipay\PayPal\Message\RestCreateCardRequest

createPlan() публичный Метод

You can create an empty billing plan and add a trial period and/or regular billing. Alternatively, you can create a fully loaded plan that includes both a trial period and regular billing. Note: By default, a created billing plan is in a CREATED state. A user cannot subscribe to the billing plan unless it has been set to the ACTIVE state.
public createPlan ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCreatePlanRequest
$parameters array
Результат Omnipay\PayPal\Message\RestCreatePlanRequest

createRequest() публичный Метод

This overrides the parent createRequest function ensuring that the OAuth 2.0 access token is passed along with the request data -- unless the request is a RestTokenRequest in which case no token is needed. If no token is available then a new one is created (e.g. if there has been no token request or the current token has expired).
public createRequest ( string $class, array $parameters = [] ) : AbstractRestRequest
$class string
$parameters array
Результат Omnipay\PayPal\Message\AbstractRestRequest

createSubscription() публичный Метод

Use this call to create a billing agreement for the buyer.
public createSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCreateSubscriptionRequest
$parameters array
Результат Omnipay\PayPal\Message\RestCreateSubscriptionRequest

createToken() публичный Метод

Create OAuth 2.0 access token request.
public createToken ( ) : RestTokenRequest
Результат Omnipay\PayPal\Message\RestTokenRequest

deleteCard() публичный Метод

Updating a card in the vault is no longer supported -- see http://stackoverflow.com/questions/20858910/paypal-rest-api-update-a-stored-credit-card Therefore the only way to update a card is to remove it using deleteCard and then re-add it using createCard.
public deleteCard ( array $parameters = [] ) : Omnipay\PayPal\Message\RestDeleteCardRequest
$parameters array
Результат Omnipay\PayPal\Message\RestDeleteCardRequest

fetchPurchase() публичный Метод

Use this call to get details about payments that have not completed, such as payments that are created and approved, or if a payment has failed.
public fetchPurchase ( array $parameters = [] ) : Omnipay\PayPal\Message\RestFetchPurchaseRequest
$parameters array
Результат Omnipay\PayPal\Message\RestFetchPurchaseRequest

fetchTransaction() публичный Метод

To get details about completed payments (sale transaction) created by a payment request or to refund a direct sale transaction, PayPal provides the /sale resource and related sub-resources.
public fetchTransaction ( array $parameters = [] ) : Omnipay\PayPal\Message\RestFetchTransactionRequest
$parameters array
Результат Omnipay\PayPal\Message\RestFetchTransactionRequest

getClientId() публичный Метод

Get an access token by using the OAuth 2.0 client_credentials token grant type with your clientId:secret as your Basic Auth credentials.
public getClientId ( ) : string
Результат string

getDefaultParameters() публичный Метод

getName() публичный Метод

public getName ( )

getSecret() публичный Метод

Get an access token by using the OAuth 2.0 client_credentials token grant type with your clientId:secret as your Basic Auth credentials.
public getSecret ( ) : string
Результат string

getToken() публичный Метод

Get OAuth 2.0 access token.
public getToken ( boolean $createIfNeeded = true ) : string
$createIfNeeded boolean [optional] - If there is not an active token present, should we create one?
Результат string

getTokenExpires() публичный Метод

Get OAuth 2.0 access token expiry time.
public getTokenExpires ( ) : integer
Результат integer

hasToken() публичный Метод

Is there a bearer token and is it still valid?
public hasToken ( ) : boolean
Результат boolean

listPurchase() публичный Метод

Use this call to get a list of payments in any state (created, approved, failed, etc.). The payments returned are the payments made to the merchant making the call.
public listPurchase ( array $parameters = [] ) : Omnipay\PayPal\Message\RestListPurchaseRequest
$parameters array
Результат Omnipay\PayPal\Message\RestListPurchaseRequest

purchase() публичный Метод

PayPal provides various payment related operations using the /payment resource and related sub-resources. Use payment for direct credit card payments and PayPal account payments. You can also use sub-resources to get payment related details.
public purchase ( array $parameters = [] ) : Omnipay\PayPal\Message\RestPurchaseRequest
$parameters array
Результат Omnipay\PayPal\Message\RestPurchaseRequest

reactivateSubscription() публичный Метод

Use this call to reactivate or un-suspend an agreement.
public reactivateSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestReactivateSubscriptionRequest
$parameters array
Результат Omnipay\PayPal\Message\RestReactivateSubscriptionRequest

refund() публичный Метод

To get details about completed payments (sale transaction) created by a payment request or to refund a direct sale transaction, PayPal provides the /sale resource and related sub-resources.
public refund ( array $parameters = [] ) : Omnipay\PayPal\Message\RestRefundRequest
$parameters array
Результат Omnipay\PayPal\Message\RestRefundRequest

refundCapture() публичный Метод

To refund captured payments (authorization transaction) created by a authorize request.
public refundCapture ( array $parameters = [] ) : RestRefundCaptureRequest
$parameters array
Результат Omnipay\PayPal\Message\RestRefundCaptureRequest

searchTransaction() публичный Метод

Use this call to search for the transactions within a billing agreement. Note that this is not a generic transaction search function -- for that see RestListPurchaseRequest. It only searches for transactions within a billing agreement. This should be used on a regular basis to determine the success / failure state of transactions on active billing agreements.
public searchTransaction ( array $parameters = [] ) : Omnipay\PayPal\Message\RestCompleteSubscriptionRequest
$parameters array
Результат Omnipay\PayPal\Message\RestCompleteSubscriptionRequest

setClientId() публичный Метод

Get an access token by using the OAuth 2.0 client_credentials token grant type with your clientId:secret as your Basic Auth credentials.
public setClientId ( string $value ) : RestGateway
$value string
Результат RestGateway provides a fluent interface

setSecret() публичный Метод

Get an access token by using the OAuth 2.0 client_credentials token grant type with your clientId:secret as your Basic Auth credentials.
public setSecret ( string $value ) : RestGateway
$value string
Результат RestGateway provides a fluent interface

setToken() публичный Метод

Set OAuth 2.0 access token.
public setToken ( string $value ) : RestGateway
$value string
Результат RestGateway provides a fluent interface

setTokenExpires() публичный Метод

Set OAuth 2.0 access token expiry time.
public setTokenExpires ( integer $value ) : RestGateway
$value integer
Результат RestGateway provides a fluent interface

suspendSubscription() публичный Метод

Use this call to suspend an agreement.
public suspendSubscription ( array $parameters = [] ) : Omnipay\PayPal\Message\RestSuspendSubscriptionRequest
$parameters array
Результат Omnipay\PayPal\Message\RestSuspendSubscriptionRequest

updatePlan() публичный Метод

You can update the information for an existing billing plan. The state of a plan must be active before a billing agreement is created.
public updatePlan ( array $parameters = [] ) : Omnipay\PayPal\Message\RestUpdatePlanRequest
$parameters array
Результат Omnipay\PayPal\Message\RestUpdatePlanRequest

void() публичный Метод

To to void a previously authorized payment.
public void ( array $parameters = [] ) : RestVoidRequest
$parameters array
Результат Omnipay\PayPal\Message\RestVoidRequest