Skip to content

Commit b2a5e0d

Browse files
Add completeCard actions to complete the authorization process on a new paypal agreement
This submits the required paypal requests to complete the authorization of a stored token representing a paypal authorization - ie. like creating a re-usable card but when the card is not presented via the php layer
1 parent d580f7e commit b2a5e0d

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* PayPal REST Complete Subscription Request
4+
*/
5+
6+
namespace Omnipay\PayPal\Message;
7+
8+
/**
9+
* PayPal REST Complete Subscription Request
10+
*
11+
* Use this call to create a billing agreement after the buyer approves it.
12+
*
13+
* Note: This request is only necessary for PayPal payments.
14+
* https://developer.paypal.com/docs/limited-release/reference-transactions/#create-billing-agreement-token
15+
*
16+
* ### Request Data
17+
*
18+
* Pass the token in the URI of a POST call to execute the subscription
19+
* agreement after buyer approval. You can find the token in the execute
20+
* link returned by the request to create a billing agreement.
21+
*
22+
* No other data is required.
23+
*
24+
* ### Example
25+
*
26+
* To create the agreement, see the code example in RestCreateCardRequest.
27+
*
28+
* At the completion of a CreateCard call, the customer should be
29+
* redirected to the redirect URL contained in $response->getRedirectUrl(). Once
30+
* the customer has approved the agreement and be returned to the returnUrl
31+
* in the call. The returnUrl can contain the following code to complete
32+
* the agreement:
33+
*
34+
* <code>
35+
* // Create a gateway for the PayPal REST Gateway
36+
* // (routes to GatewayFactory::create)
37+
* $gateway = Omnipay::create('PayPal_Rest');
38+
*
39+
* // Initialise the gateway
40+
* $gateway->initialize(array(
41+
* 'clientId' => 'MyPayPalClientId',
42+
* 'secret' => 'MyPayPalSecret',
43+
* 'testMode' => true, // Or false when you are ready for live transactions
44+
* ));
45+
*
46+
* // Crate a card
47+
* $transaction = $gateway->completeCreateCard(array(
48+
* 'transactionReference' => $subscription_id,
49+
* ));
50+
* $response = $transaction->send();
51+
* if ($response->isSuccessful()) {
52+
* echo "Complete Subscription transaction was successful!\n";
53+
* $subscription_id = $response->getTransactionReference();
54+
* echo "Subscription reference = " . $subscription_id;
55+
* }
56+
* </code>
57+
*
58+
* Note that the subscription_id that you get from calling the response's
59+
* getTransactionReference() method at the end of the completeSubscription
60+
* call will be different to the one that you got after calling the response's
61+
* getTransactionReference() method at the end of the createSubscription
62+
* call. The one that you get from completeSubscription is the correct
63+
* one to use going forwards (e.g. for cancelling or updating the subscription).
64+
*
65+
* ### Request Sample
66+
*
67+
* This is from the PayPal web site:
68+
*
69+
* <code>
70+
* curl -v POST https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-0JP008296V451950C/agreement-execute \
71+
* -H 'Content-Type:application/json' \
72+
* -H 'Authorization: Bearer <Access-Token>' \
73+
* -d '{}'
74+
* </code>
75+
*
76+
* ### Response Sample
77+
*
78+
* This is from the PayPal web site:
79+
*
80+
* <code>
81+
* {
82+
* "id": "I-0LN988D3JACS",
83+
* "links": [
84+
* {
85+
* "href": "https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-0LN988D3JACS",
86+
* "rel": "self",
87+
* "method": "GET"
88+
* }
89+
* ]
90+
* }
91+
* </code>
92+
*
93+
* @link https://developer.paypal.com/docs/api/#execute-an-agreement
94+
* @see RestCreateSubscriptionRequest
95+
* @see Omnipay\PayPal\RestGateway
96+
*/
97+
class RestCompleteCreateCardRequest extends AbstractRestRequest
98+
{
99+
public function getData()
100+
{
101+
$this->validate('transactionReference');
102+
103+
return ['token_id' => $this->getTransactionReference()];
104+
}
105+
106+
/**
107+
* Get transaction endpoint.
108+
*
109+
* Subscriptions are executed using the /billing-agreements resource.
110+
*
111+
* @return string
112+
*/
113+
protected function getEndpoint()
114+
{
115+
return parent::getEndpoint() . '/billing-agreements/agreements';
116+
}
117+
}

src/RestGateway.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,20 @@ public function createCard(array $parameters = array())
547547
return $this->createRequest('\Omnipay\PayPal\Message\RestCreateCardRequest', $parameters);
548548
}
549549

550+
/**
551+
* Complete a card creation when using paypal account method.
552+
*
553+
* Use this call to create a billing agreement after the buyer approves it.
554+
*
555+
* @link https://developer.paypal.com/docs/limited-release/reference-transactions/#create-billing-agreement-token
556+
* @param array $parameters
557+
* @return \Omnipay\PayPal\Message\RestCompleteCreateCardRequest
558+
*/
559+
public function completeCreateCard(array $parameters = array())
560+
{
561+
return $this->createRequest('\Omnipay\PayPal\Message\RestCompleteCreateCardRequest', $parameters);
562+
}
563+
550564
/**
551565
* Delete a credit card from the vault.
552566
*

0 commit comments

Comments
 (0)