| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 | <?php/** * * PHP Pro Bid $Id$ mv8+4GZyaXK9rXNy6v7/aUJqMS5QvAJJ6z0rheUNJNQ= * * @link        http://www.phpprobid.com * @copyright   Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL * @license     http://www.phpprobid.com/license Commercial License * * @version     7.2 *//** * paypal sandbox payment gateway model class */namespace Ppb\Model\PaymentGateway;use Cube\Controller\Request\AbstractRequest;class PayPalSandbox extends AbstractPaymentGateway{    /**     * payment gateway name     */    const NAME = 'PayPalSandbox';    /**     * required settings     */    const BUSINESS = 'sandbox_business';    /**     * form post url (sandbox)     */    const POST_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';    /**     * paypal description     */    protected $_description = 'Click to pay through PayPal (sandbox mode).';    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'     => 'PayPalSandbox',                'id'          => self::BUSINESS,                'element'     => 'text',                'label'       => $this->_('PayPal Sandbox Email Address'),                'description' => $translate->_('Enter your PayPal Sandbox registered email address<br>'                        . 'PayPal IPN URL: <br>') . $this->getIpnUrl(),                'attributes'  => array(                    'class' => 'form-control input-medium',                ),            ),        );    }    public function formElements()    {        return array(            array(                'id'      => 'cmd',                'value'   => '_xclick',                'element' => 'hidden',            ),            array(                'id'      => 'bn',                'value'   => 'wa_dw_2.0.4',                'element' => 'hidden',            ),            array(                'id'      => '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_shipping',                'value'   => '1',                'element' => 'hidden',            ),            array(                'id'      => 'no_note',                'value'   => '1',                'element' => 'hidden',            ),        );    }    /**     *     * 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.sandbox.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;    }}
 |