Guest User

Untitled

a guest
Mar 1st, 2018
2,724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.66 KB | None | 0 0
  1. <?php
  2. /**
  3. * Sign Up Controller
  4. *
  5. * Handles sign ups with or without Paypal
  6. *
  7. * @author   Tyler King <tyler.n.king@gmail.com>
  8. * @license  Private for John Dixon
  9. */
  10. namespace Application\Controller;
  11. use Slim\Application;
  12. use Slim\CSRF;
  13. use Application\Vendor\phpPaypal;
  14. use Application\Vendor\PasswordHash;
  15. use PDO;
  16.  
  17. session_start( );
  18.  
  19. class SignUp {
  20.     /**
  21.     * @protected $slim object Slim Application instance.
  22.     * @protected $db object PDO instance.
  23.     * @protected $route string URL route for the app.
  24.     * @protected $csrf CRSF object.
  25.     */
  26.     protected   $slim,
  27.                 $db,
  28.                 $route,
  29.                 $csrf;
  30.  
  31.     /**
  32.     * @private $paypal object Paypal class object.
  33.     */
  34.     private     $paypal;
  35.  
  36.     /**
  37.     * Construction, does nothing really...
  38.     *
  39.     * @param $slim object Slim Application instance.
  40.     * @param $db object PDO instance.
  41.     * @param $route string URL route for the app.
  42.     * @param $crsf object CRSF object.
  43.     * @return null
  44.     */
  45.     public function __construct( Application $slim, PDO $db, $route, CSRF $csrf ) {
  46.         $this->slim     = $slim;
  47.         $this->db       = $db;
  48.         $this->route    = $route;
  49.         $this->csrf     = $csrf;
  50.  
  51.         // Create instance of the phpPayPal class.
  52.         $this->paypal   = new phpPayPal( array (
  53.             'api_username' => $this->slim->config->attr( 'paypalAPIUsername' ),
  54.             'api_password' => $this->slim->config->attr( 'paypalAPIPassword' ),
  55.             'api_signature' => $this->slim->config->attr( 'paypalAPISignature' )
  56.         ), true );
  57.     }
  58.  
  59.     /**
  60.     * Shows the index page, the FIRST page.
  61.     *
  62.     * @return $view string rendered view.
  63.     */
  64.     public function index( ) {
  65.         $view   = $this->slim->view->getView( 'Application/View/SignUp_index' );
  66.         $view->setLayout( 'Application/View/Layout', 'body' );
  67.  
  68.         $view->set( array(
  69.             'route' => $this->route )
  70.         );
  71.  
  72.         return  $view->render( );
  73.     }
  74.  
  75.     /**
  76.     * Account typing paying with Paypal
  77.     *
  78.     * @return mixed null or view depending upon result.
  79.     */
  80.     public function pay( $type = '' ) {
  81.         $post               = $this->slim->request->post( );
  82.         $_SESSION[ 'form' ] = serialize( $post );
  83.  
  84.         /* Get the account type. */
  85.         $sql    = $this->db->prepare( "SELECT * FROM account WHERE id = :id" );
  86.         $sql->bindParam( ':id', $post[ 'account_id' ], PDO::PARAM_INT );
  87.         $sql->execute( );
  88.         $row    = $sql->fetch( PDO::FETCH_OBJ );
  89.  
  90.         /* Currency type. */
  91.         $this->paypal->currency_code    = 'USD';
  92.  
  93.         /* Set the total amount for this order. */
  94.         $this->paypal->amount_total = (int) $row->price;
  95.  
  96.         /* Set the paths. */
  97.         $this->paypal->return_url   = $this->slim->urlFor( 'pay-success' );
  98.         $this->paypal->cancel_url   = $this->slim->urlFor( 'pay-failed' );
  99.  
  100.         /* $paypal->add_item('ItemName', 'ItemNumber', 'ItemQuantity', 'ItemTaxAmount', 'ItemPrice'); */
  101.         $this->paypal->add_item( $row->name, $row->id, 1, 0, (int) $row->price );
  102.  
  103.         /* Process it. */
  104.         $this->paypal->set_express_checkout( );
  105.  
  106.         /* If successful, we need to store the token, and then redirect the user to PayPal. */
  107.         if( !$this->paypal->_error ) {
  108.             /* Token sotre. */
  109.             $_SESSION[ 'token' ]    = $this->paypal->token;
  110.            
  111.             /* Now, go to Paypal! */
  112.             $this->paypal->set_express_checkout_successful_redirect( );
  113.            
  114.         } else {
  115.             /* WTH? No money or what b'y?! */
  116.             return  $this->error( );
  117.         }
  118.     }
  119.  
  120.     /**
  121.     * Error somewhere!
  122.     *
  123.     * @return $view rendered view.
  124.     */
  125.     private function error( ) {
  126.         $view   = $this->slim->view->getView( 'Application/View/SignUp_error' );
  127.         $view->setLayout( 'Application/View/Layout', 'body' );
  128.  
  129.         $view->set( array(
  130.             'errorNum' => $this->paypal->_error_code,
  131.             'errorMessage' => $this->paypal->_error_long_message,
  132.             'route' => $this->route )
  133.         );
  134.  
  135.         return  $view->render( );
  136.     }
  137.  
  138.     /**
  139.     * Successfull pay payment.
  140.     *
  141.     * @return $view rendered view.
  142.     */
  143.     public function success( ) {
  144.         $get    = $this->slim->request->get( );
  145.  
  146.         /* Store the callbakcs from Paypal. */
  147.          $this->paypal->token       = $get[ 'token' ];
  148.          $this->paypal->payer_id    = $get[ 'PayerID' ];
  149.          
  150.         /* Do the actual payment. */
  151.         $this->paypal->do_express_checkout_payment( );
  152.  
  153.         if( !$this->paypal->_error && $this->paypal->get_transaction_details( ) ) {
  154.             if( 'COMPLETED' == $this->paypal->payment_status && ( 'SUCCESS' == $this->paypal->ack || 'SUCCESSWITHWARNING' == $this->paypal->ack ) ) {
  155.                 $view   = $this->slim->view->getView( 'Application/View/SignUp_success' );
  156.                 $view->setLayout( 'Application/View/Layout', 'body' );
  157.  
  158.                 /* Register the user. */
  159.                 $post       = unserialize( $_SESSION[ 'form' ] );
  160.                 $user_id    = $this->register( $post );
  161.  
  162.                 /* Set their next payment date. */
  163.                 $sql    = $this->db->prepare( "UPDATE user SET next_paypal_date = :next_paypal_date WHERE id = :id" );
  164.                 $sql->bindParam( ':next_paypal_date', strtotime( '+1 month' ), PDO::PARAM_INT );
  165.                 $sql->bindParam( ':id', $user_id, PDO::PARAM_INT );
  166.                 $sql->execute( );
  167.  
  168.                 return  $view->render( );
  169.             }
  170.         } else {
  171.             /* WTH? No money or what b'y?! */
  172.             return  $this->error( );
  173.         }
  174.     }
  175.  
  176.     /**
  177.     * Free process.
  178.     *
  179.     * @return $view rendered view.
  180.     */
  181.     public function free( ) {
  182.         $post   = $this->slim->request->post( );
  183.  
  184.         $view   = $this->slim->view->getView( 'Application/View/SignUp_free' );
  185.         $view->setLayout( 'Application/View/Layout', 'body' );
  186.  
  187.         /* Register the user. */
  188.         $this->register( $post );
  189.  
  190.         return  $view->render( );
  191.     }
  192.  
  193.     /**
  194.     * Registers the user.
  195.     *
  196.     * @param $data array post data from the signup form.
  197.     * @return int the new user ID.
  198.     */
  199.     private function register( $data ) {
  200.         /* Now, lets use PhPass lib to hash the password. */
  201.         $hasher     = new PasswordHash( 9, false );
  202.         $password   = $hasher->HashPassword( $data[ 'password' ] );
  203.  
  204.         $sql    = $this->db->prepare( "
  205.             INSERT INTO user SET
  206.                 username = :username,
  207.                 password = :password,
  208.                 name = :name,
  209.                 email = :email,
  210.                 registered = :registered,
  211.                 street = :street,
  212.                 state = :state,
  213.                 country = :country,
  214.                 zip = :zip,
  215.                 account_id = :account_id
  216.         " );
  217.         $sql->bindParam( ':username', clean( $data[ 'username' ], true ), PDO::PARAM_STR );
  218.         $sql->bindParam( ':password', $password, PDO::PARAM_STR );
  219.         $sql->bindParam( ':name', clean( $data[ 'name' ], true ), PDO::PARAM_STR );
  220.         $sql->bindParam( ':email', clean( $data[ 'email' ], true ), PDO::PARAM_STR );
  221.         $sql->bindParam( ':registered', time( ), PDO::PARAM_INT );
  222.         $sql->bindParam( ':street', clean( $data[ 'street' ], true ), PDO::PARAM_STR );
  223.         $sql->bindParam( ':state', clean( $data[ 'state' ], true ), PDO::PARAM_STR );
  224.         $sql->bindParam( ':country', clean( $data[ 'country' ], true ), PDO::PARAM_STR );
  225.         $sql->bindParam( ':zip', clean( $data[ 'zip' ], true ), PDO::PARAM_STR );
  226.         $sql->bindParam( ':account_id', clean( $data[ 'account_id' ], true ), PDO::PARAM_INT );
  227.         $sql->execute( );
  228.  
  229.         return  $this->db->lastInsertId( );
  230.     }
  231. }
  232. ?>
Add Comment
Please, Sign In to add comment