123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
- /**
- *
- * PHP Pro Bid $Id$ uBw224WvjeTz4R4u5UgnuqAig8adbQ58ZcEFqI9hEBg=
- *
- * @link http://www.phpprobid.com
- * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
- * @license http://www.phpprobid.com/license Commercial License
- *
- * @version 7.7
- */
- /**
- * paypal payment gateway model class
- */
- namespace Ppb\Model\PaymentGateway;
- use Cube\Controller\Request\AbstractRequest,
- Cube\Controller\Front,
- Ppb\Service\Table\Relational\Locations as LocationsService,
- Ppb\Service\Transactions as TransactionsService;
- class PayPal extends AbstractPaymentGateway
- {
- /**
- * payment gateway name
- */
- const NAME = 'PayPal';
- /**
- * required settings
- */
- const BUSINESS = 'business';
- /**
- * form post url
- */
- const POST_URL = 'https://www.paypal.com/cgi-bin/webscr';
- /**
- * paypal description
- */
- protected $_description = 'Click to pay through PayPal.';
- public function __construct($userId = null)
- {
- parent::__construct(self::NAME, $userId);
- }
- /**
- *
- * check if the gateway is enabled
- *
- * @return bool
- */
- public function enabled()
- {
- if (!empty($this->_data[self::BUSINESS])) {
- return true;
- }
- return false;
- }
- /**
- *
- * get paypal setup form elements
- *
- * @return array
- */
- public function getElements()
- {
- $translate = $this->getTranslate();
- return array(
- array(
- 'form_id' => 'PayPal',
- 'id' => self::BUSINESS,
- 'element' => 'text',
- 'label' => $this->_('PayPal Email Address'),
- 'description' => $translate->_('Enter your PayPal registered email address<br>'
- . 'PayPal IPN URL: <br>') . $this->getIpnUrl(),
- 'attributes' => array(
- 'class' => 'form-control input-medium',
- ),
- // 'validators' => array(
- // 'Email'
- // ),
- ),
- );
- }
- public function formElements()
- {
- $elements = array(
- array(
- 'id' => 'cmd',
- 'value' => '_xclick',
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'bn',
- 'value' => 'wa_dw_2.0.4',
- 'element' => 'hidden',
- ),
- array(
- 'id' => self::BUSINESS,
- 'value' => $this->_data[self::BUSINESS],
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'receiver_email',
- 'value' => $this->_data[self::BUSINESS],
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'item_name',
- 'value' => $this->getName(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'amount',
- 'value' => $this->getAmount(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'currency_code',
- 'value' => $this->getCurrency(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'custom',
- 'value' => $this->getTransactionId(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'notify_url',
- 'value' => $this->getIpnUrl(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'return',
- 'value' => $this->getSuccessUrl(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'cancel_return',
- 'value' => $this->getFailureUrl(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'undefined_quantity',
- 'value' => '0',
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'no_note',
- 'value' => '1',
- 'element' => 'hidden',
- ),
- );
- $transactionsService = new TransactionsService();
- $transaction = $transactionsService->findBy('id', $this->getTransactionId());
- $addressId = null;
- if ($transaction->getData('sale_id')) {
- $sale = $transaction->findParentRow('\Ppb\Db\Table\Sales');
- $addressId = $sale->getData('shipping_address_id');
- }
- $user = $this->getUser();
- $shippingAddress = $user->getAddress($addressId);
- if ($shippingAddress !== null) {
- $user->setAddress($shippingAddress);
- $locationsService = new LocationsService();
- $country = $user['country'];
- if (is_numeric($country)) {
- $row = $locationsService->findBy('id', (int)$country);
- if ($row != null) {
- $country = strtoupper($row->getData('iso_code'));
- }
- }
- $elements[] = array(
- 'id' => 'address_override',
- 'value' => '1',
- 'element' => 'hidden',
- );
- $elements[] = array(
- 'id' => 'first_name',
- 'value' => $user['name']['first'],
- 'element' => 'hidden',
- );
- $elements[] = array(
- 'id' => 'last_name',
- 'value' => $user['name']['last'],
- 'element' => 'hidden',
- );
- $elements[] = array(
- 'id' => 'address1',
- 'value' => $shippingAddress['address'],
- 'element' => 'hidden',
- );
- $elements[] = array(
- 'id' => 'city',
- 'value' => $shippingAddress['city'],
- 'element' => 'hidden',
- );
- $elements[] = array(
- 'id' => 'country',
- 'value' => $country,
- 'element' => 'hidden',
- );
- $elements[] = array(
- 'id' => 'zip',
- 'value' => $user['zip_code'],
- 'element' => 'hidden',
- );
- $state = $user['state'];
- if (is_numeric($state)) {
- $row = $locationsService->findBy('id', (int)$state);
- if ($row != null) {
- $state = ($country == 'US') ? strtoupper($row->getData('iso_code')) : $row->getData('name');
- }
- }
- $elements[] = array(
- 'id' => 'state',
- 'value' => $state,
- 'element' => 'hidden',
- );
- $elements[] = array(
- 'id' => 'email',
- 'value' => $user['email'],
- 'element' => 'hidden',
- );
- }
- else {
- $elements[] = array(
- 'id' => 'no_shipping',
- 'value' => '1',
- 'element' => 'hidden',
- );
- }
- return $elements;
- }
- /**
- *
- * get gateway post url
- *
- * @return string
- */
- public function getPostUrl()
- {
- return self::POST_URL;
- }
- /**
- *
- * process ipn
- *
- * @param \Cube\Controller\Request\AbstractRequest $request
- *
- * @return bool return true if ipn returns a valid transaction
- */
- public function processIpn(AbstractRequest $request)
- {
- $errno = null;
- $errstr = null;
- $response = false;
- if ($request->isPost()) {
- $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
- if (!$fp) {
- $this->setGatewayPaymentStatus($errstr . ' (' . $errno . ')');
- }
- else {
- $content = 'cmd=_notify-validate';
- foreach ($request->getParams() as $key => $value) {
- $content .= '&' . $key . '=' . urlencode(stripslashes($value));
- }
- $header = "POST /cgi-bin/webscr HTTP/1.1\r\n"
- . "Content-Type: application/x-www-form-urlencoded\r\n"
- . "Host: www.paypal.com\r\n"
- . "Connection: close\r\n"
- . "Content-Length: " . strlen($content) . "\r\n\r\n";
- fputs($fp, $header . $content);
- $paymentStatus = $_POST['payment_status'];
- $this->setTransactionId($_POST['custom'])
- ->setAmount($_POST['mc_gross'])
- ->setCurrency($_POST['mc_currency'])
- ->setGatewayPaymentStatus($paymentStatus)
- ->setGatewayTransactionCode($_POST['txn_id']);
- while (!feof($fp)) {
- $result = trim(fgets($fp, 1024));
- if (strcmp($result, "VERIFIED") == 0) {
- if ($paymentStatus == "Completed") {
- $response = true;
- }
- }
- else if (strcmp($result, "INVALID") == 0) {
- $this->setGatewayPaymentStatus($result);
- }
- }
- fclose($fp);
- }
- }
- return $response;
- }
- }
|