/library/PHPMerchant/Billing/Gateway/PayPal.php

https://github.com/ad2joe/php-merchant-1 · PHP · 267 lines · 113 code · 31 blank · 123 comment · 6 complexity · c7c9509a048e4d16506f69fb67506ee7 MD5 · raw file

  1. <?php
  2. /**
  3. * PHP Merchant
  4. *
  5. * Copyright (C) 2009 Ramon Torres
  6. *
  7. * This Software is released under the MIT License.
  8. * See license.txt for more details.
  9. *
  10. * @author Ramon Torres
  11. * @copyright Copyright (c) 2009 Ramon Torres
  12. * @license http://www.opensource.org/licenses/mit-license.php
  13. * @version $Id$
  14. */
  15. /**
  16. * PHPMerchant_Billing_Gateway_PayPal
  17. *
  18. * @package PHPMerchant
  19. * @author Ramon Torres
  20. * @copyright 2009 Ramon Torres
  21. * @license http://www.opensource.org/licenses/mit-license.php
  22. * @access public
  23. */
  24. class PHPMerchant_Billing_Gateway_PayPal extends PHPMerchant_Billing_Gateway {
  25. const TEST_URL = 'https://api-3t.sandbox.paypal.com/nvp';
  26. const LIVE_URL = 'https://api-3t.paypal.com/nvp';
  27. /**
  28. * undocumented function
  29. *
  30. * @param mixed $money
  31. * @param PHPMerchant_Billing_CreditCard $creditcard
  32. * @param array $options
  33. * @return PHPMerchant_Billing_Response
  34. */
  35. public function purchase($money, PHPMerchant_Billing_CreditCard $creditcard, $options = array()) {
  36. return $this->_commit('DoDirectPayment', $this->_buildSaleOrAuthorizationRequest('Sale', $money, $creditcard, $options));
  37. }
  38. /**
  39. * PHPMerchant_Billing_Gateway_PayPal::authorize()
  40. *
  41. * @param mixed $money
  42. * @param PHPMerchant_Billing_CreditCard $creditcard
  43. * @param array $options
  44. * @return PHPMerchant_Billing_Response
  45. */
  46. public function authorize($money, PHPMerchant_Billing_CreditCard $creditcard, $options = array()) {
  47. return $this->_commit('DoDirectPayment', $this->_buildSaleOrAuthorizationRequest('Authorization', $money, $creditcard, $options));
  48. }
  49. /**
  50. * PHPMerchant_Billing_Gateway_PayPal::capture()
  51. *
  52. * @param integer $money
  53. * @param string $authorization
  54. * @param array $options
  55. * @return PHPMerchant_Billing_Response
  56. */
  57. public function capture($money, $authorization, $options = array()) {
  58. return $this->_commit('DoCapture', $this->_buildCaptureRequest($money, $authorization, $options));
  59. }
  60. /**
  61. * PHPMerchant_Billing_Gateway_PayPal::void()
  62. *
  63. * @param string $authorization
  64. * @param array $options
  65. * @return PHPMerchant_Billing_Response
  66. */
  67. public function void($authorization, $options = array()) {
  68. return $this->_commit('DoVoid', $this->_buildVoidRequest($authorization, $options));
  69. }
  70. /**
  71. * PHPMerchant_Billing_Gateway_PayPal::credit()
  72. *
  73. * @param integer $money
  74. * @param string $identification
  75. * @param array $options
  76. * @return PHPMerchant_Billing_Response
  77. */
  78. public function credit($money, $identification, $options = array()) {
  79. return $this->_commit('RefundTransaction', $this->_buildCreditRequest($money, $identification, $options));
  80. }
  81. /**
  82. * PHPMerchant_Billing_Gateway_PayPal::_commit()
  83. *
  84. * @param mixed $action
  85. * @param mixed $request
  86. * @return PHPMerchant_Billing_Response
  87. */
  88. protected function _commit($method, $request) {
  89. $request = array_merge(array(
  90. 'METHOD' => $method,
  91. 'VERSION' => '3.0',
  92. 'USER' => $this->_options['login'],
  93. 'PWD' => $this->_options['password'],
  94. 'SIGNATURE' => $this->_options['signature'],
  95. ), $request);
  96. $response = PHPMerchant_Utils::httpPost($this->_getEndpointUrl(), $request);
  97. $response = $this->_parseResponse($response);
  98. $success = ($response['ACK'] == 'Success' || $response['ACK'] == 'SuccessWithWarning');
  99. $message = isset($response['L_LONGMESSAGE0']) ? $response['L_LONGMESSAGE0'] : $response['ACK'];
  100. $responseObject = new PHPMerchant_Billing_Response($success, $message, $response, array(
  101. 'test' => $this->isTest(),
  102. 'authorization' => self::_authorizationFrom($response),
  103. 'avs_result' => isset($response['AVSCODE']) ? array('code' => $response['AVSCODE']) : null,
  104. 'cvv_result' => isset($response['CVV2MATCH']) ? array('code' => $response['CVV2MATCH']) : null
  105. ));
  106. return $responseObject;
  107. }
  108. /**
  109. * PHPMerchant_Billing_Gateway_PayPal::_authorizationFrom()
  110. *
  111. * @param mixed $response
  112. * @return
  113. */
  114. protected static function _authorizationFrom($response) {
  115. if (isset($response['TRANSACTIONID'])) {
  116. return $response['TRANSACTIONID'];
  117. }
  118. if (isset($response['AUTHORIZATIONID'])) {
  119. return $response['AUTHORIZATIONID'];
  120. }
  121. if (isset($response['REFUNDTRANSACTIONID'])) {
  122. return $response['REFUNDTRANSACTIONID'];
  123. }
  124. return null;
  125. }
  126. /**
  127. * PHPMerchant_Billing_Gateway_PayPal::_buildSaleOrAuthorizationRequest()
  128. *
  129. * @param mixed $action
  130. * @param mixed $money
  131. * @param mixed $creditcard
  132. * @param mixed $options
  133. * @return void
  134. */
  135. protected function _buildSaleOrAuthorizationRequest($action, $money, $creditcard, $options) {
  136. $request = array(
  137. 'PAYMENTACTION' => $action,
  138. 'AMT' => sprintf("%.2f", $money / 100),
  139. 'CURRENCYCODE' => isset($options['currency_code']) ? $options['currency_code'] : 'USD',
  140. 'CREDITCARDTYPE' => $this->_creditCardType($creditcard->type),
  141. 'ACCT' => $creditcard->number,
  142. 'EXPDATE' => sprintf('%02d%02d', $creditcard->month, $creditcard->year),
  143. 'CVV2' => $creditcard->verificationValue,
  144. );
  145. return $request;
  146. }
  147. /**
  148. * PHPMerchant_Billing_Gateway_PayPal::_buildCaptureRequest()
  149. *
  150. * @param integer $money
  151. * @param string $authorization
  152. * @param array $options
  153. * @return array
  154. */
  155. protected function _buildCaptureRequest($money, $authorization, $options) {
  156. $request = array(
  157. 'AUTHORIZATIONID' => $authorization,
  158. 'AMT' => sprintf("%.2f", $money / 100),
  159. 'CURRENCYCODE' => isset($options['currency_code']) ? $options['currency_code'] : 'USD',
  160. 'COMPLETETYPE' => 'Complete',
  161. 'NOTE' => isset($options['description']) ? $options['description'] : ''
  162. );
  163. return $request;
  164. }
  165. /**
  166. * PHPMerchant_Billing_Gateway_PayPal::_buildVoidRequest()
  167. *
  168. * @param string $authorization
  169. * @param array $options
  170. * @return array
  171. */
  172. protected function _buildVoidRequest($authorization, $options) {
  173. $request = array(
  174. 'AUTHORIZATIONID' => $authorization,
  175. 'NOTE' => isset($options['description']) ? $options['description'] : ''
  176. );
  177. return $request;
  178. }
  179. /**
  180. * PHPMerchant_Billing_Gateway_PayPal::_buildCreditRequest()
  181. *
  182. * @param integer $money
  183. * @param string $identification
  184. * @param array $options
  185. * @return array
  186. */
  187. protected function _buildCreditRequest($money, $identification, $options) {
  188. $request = array(
  189. 'TRANSACTIONID' => $identification,
  190. 'AMT' => sprintf("%.2f", $money / 100),
  191. 'NOTE' => isset($options['note']) ? $options['note'] : '',
  192. 'REFUNDTYPE' => 'Partial'
  193. );
  194. return $request;
  195. }
  196. /**
  197. * PHPMerchant_Billing_Gateway_PayPal::_creditCardType()
  198. *
  199. * @param string $type
  200. * @return string
  201. */
  202. protected function _creditCardType($type) {
  203. $map = array(
  204. PHPMerchant_Billing_CreditCard::TYPE_VISA => 'Visa',
  205. PHPMerchant_Billing_CreditCard::TYPE_MASTER_CARD => 'MasterCard',
  206. PHPMerchant_Billing_CreditCard::TYPE_AMEX => 'Amex',
  207. PHPMerchant_Billing_CreditCard::TYPE_DISCOVER => 'Discover'
  208. );
  209. return isset($map[$type]) ? $map[$type] : null;
  210. }
  211. /**
  212. * PHPMerchant_Billing_Gateway_PayPal::getEndpointUrl()
  213. *
  214. * @return string
  215. */
  216. protected function _getEndpointUrl() {
  217. return $this->isTest() ? self::TEST_URL : self::LIVE_URL;
  218. }
  219. /**
  220. * PHPMerchant_Billing_Gateway_PayPal::_parseResponse()
  221. *
  222. * @param mixed $response
  223. * @return array
  224. */
  225. protected function _parseResponse($response) {
  226. $results = array();
  227. $pairs = explode('&', $response);
  228. foreach ($pairs as $pair) {
  229. list($k, $v) = explode('=', $pair);
  230. $k = trim(urldecode($k));
  231. $v = trim(urldecode($v));
  232. $results[$k] = $v;
  233. }
  234. return $results;
  235. }
  236. }