diff --git a/lib/PayPal/Api/Payment.php b/lib/PayPal/Api/Payment.php index c0747ae..3197485 100644 --- a/lib/PayPal/Api/Payment.php +++ b/lib/PayPal/Api/Payment.php @@ -544,7 +544,19 @@ class Payment extends PayPalResourceModel { return $this->getLink(PayPalConstants::APPROVAL_URL); } - + + /** + * Get token from Approval Link + * + * @return null|string + */ + public function getToken() + { + $parameter_name = "token"; + parse_str(parse_url($this->getApprovalLink(), PHP_URL_QUERY), $query); + return !isset($query[$parameter_name]) ? null : $query[$parameter_name]; + } + /** * Creates and processes a payment. In the JSON request body, include a `payment` object with the intent, payer, and transactions. For PayPal payments, include redirect URLs in the `payment` object. * diff --git a/tests/PayPal/Test/Api/PaymentTest.php b/tests/PayPal/Test/Api/PaymentTest.php index e74f093..9f2e67e 100644 --- a/tests/PayPal/Test/Api/PaymentTest.php +++ b/tests/PayPal/Test/Api/PaymentTest.php @@ -29,6 +29,27 @@ class PaymentTest extends \PHPUnit_Framework_TestCase return new Payment(self::getJson()); } + public function testGetToken_returnsNullIfApprovalLinkNull() + { + $payment = new Payment(); + $token = $payment->getToken(); + $this->assertNull($token); + } + + public function testGetToken_returnsNullIfApprovalLinkDoesNotHaveToken() + { + $payment = new Payment('{"links": [ { "href": "https://api.sandbox.paypal.com/v1/payments//cgi-bin/webscr?cmd=_express-checkout", "rel": "approval_url", "method": "REDIRECT" } ]}'); + $token = $payment->getToken(); + $this->assertNull($token); + } + + public function testGetToken_returnsNullIfApprovalLinkHasAToken() + { + $payment = new Payment('{"links": [ { "href": "https://api.sandbox.paypal.com/v1/payments//cgi-bin/webscr?cmd=_express-checkout&token=EC-60385559L1062554J", "rel": "approval_url", "method": "REDIRECT" } ]}'); + $token = $payment->getToken(); + $this->assertNotNull($token); + $this->assertEquals($token, 'EC-60385559L1062554J'); + } /** * Tests for Serialization and Deserialization Issues