Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Sign Up Controller
- *
- * Handles sign ups with or without Paypal
- *
- * @author Tyler King <tyler.n.king@gmail.com>
- * @license Private for John Dixon
- */
- namespace Application\Controller;
- use Slim\Application;
- use Slim\CSRF;
- use Application\Vendor\phpPaypal;
- use Application\Vendor\PasswordHash;
- use PDO;
- session_start( );
- class SignUp {
- /**
- * @protected $slim object Slim Application instance.
- * @protected $db object PDO instance.
- * @protected $route string URL route for the app.
- * @protected $csrf CRSF object.
- */
- protected $slim,
- $db,
- $route,
- $csrf;
- /**
- * @private $paypal object Paypal class object.
- */
- private $paypal;
- /**
- * Construction, does nothing really...
- *
- * @param $slim object Slim Application instance.
- * @param $db object PDO instance.
- * @param $route string URL route for the app.
- * @param $crsf object CRSF object.
- * @return null
- */
- public function __construct( Application $slim, PDO $db, $route, CSRF $csrf ) {
- $this->slim = $slim;
- $this->db = $db;
- $this->route = $route;
- $this->csrf = $csrf;
- // Create instance of the phpPayPal class.
- $this->paypal = new phpPayPal( array (
- 'api_username' => $this->slim->config->attr( 'paypalAPIUsername' ),
- 'api_password' => $this->slim->config->attr( 'paypalAPIPassword' ),
- 'api_signature' => $this->slim->config->attr( 'paypalAPISignature' )
- ), true );
- }
- /**
- * Shows the index page, the FIRST page.
- *
- * @return $view string rendered view.
- */
- public function index( ) {
- $view = $this->slim->view->getView( 'Application/View/SignUp_index' );
- $view->setLayout( 'Application/View/Layout', 'body' );
- $view->set( array(
- 'route' => $this->route )
- );
- return $view->render( );
- }
- /**
- * Account typing paying with Paypal
- *
- * @return mixed null or view depending upon result.
- */
- public function pay( $type = '' ) {
- $post = $this->slim->request->post( );
- $_SESSION[ 'form' ] = serialize( $post );
- /* Get the account type. */
- $sql = $this->db->prepare( "SELECT * FROM account WHERE id = :id" );
- $sql->bindParam( ':id', $post[ 'account_id' ], PDO::PARAM_INT );
- $sql->execute( );
- $row = $sql->fetch( PDO::FETCH_OBJ );
- /* Currency type. */
- $this->paypal->currency_code = 'USD';
- /* Set the total amount for this order. */
- $this->paypal->amount_total = (int) $row->price;
- /* Set the paths. */
- $this->paypal->return_url = $this->slim->urlFor( 'pay-success' );
- $this->paypal->cancel_url = $this->slim->urlFor( 'pay-failed' );
- /* $paypal->add_item('ItemName', 'ItemNumber', 'ItemQuantity', 'ItemTaxAmount', 'ItemPrice'); */
- $this->paypal->add_item( $row->name, $row->id, 1, 0, (int) $row->price );
- /* Process it. */
- $this->paypal->set_express_checkout( );
- /* If successful, we need to store the token, and then redirect the user to PayPal. */
- if( !$this->paypal->_error ) {
- /* Token sotre. */
- $_SESSION[ 'token' ] = $this->paypal->token;
- /* Now, go to Paypal! */
- $this->paypal->set_express_checkout_successful_redirect( );
- } else {
- /* WTH? No money or what b'y?! */
- return $this->error( );
- }
- }
- /**
- * Error somewhere!
- *
- * @return $view rendered view.
- */
- private function error( ) {
- $view = $this->slim->view->getView( 'Application/View/SignUp_error' );
- $view->setLayout( 'Application/View/Layout', 'body' );
- $view->set( array(
- 'errorNum' => $this->paypal->_error_code,
- 'errorMessage' => $this->paypal->_error_long_message,
- 'route' => $this->route )
- );
- return $view->render( );
- }
- /**
- * Successfull pay payment.
- *
- * @return $view rendered view.
- */
- public function success( ) {
- $get = $this->slim->request->get( );
- /* Store the callbakcs from Paypal. */
- $this->paypal->token = $get[ 'token' ];
- $this->paypal->payer_id = $get[ 'PayerID' ];
- /* Do the actual payment. */
- $this->paypal->do_express_checkout_payment( );
- if( !$this->paypal->_error && $this->paypal->get_transaction_details( ) ) {
- if( 'COMPLETED' == $this->paypal->payment_status && ( 'SUCCESS' == $this->paypal->ack || 'SUCCESSWITHWARNING' == $this->paypal->ack ) ) {
- $view = $this->slim->view->getView( 'Application/View/SignUp_success' );
- $view->setLayout( 'Application/View/Layout', 'body' );
- /* Register the user. */
- $post = unserialize( $_SESSION[ 'form' ] );
- $user_id = $this->register( $post );
- /* Set their next payment date. */
- $sql = $this->db->prepare( "UPDATE user SET next_paypal_date = :next_paypal_date WHERE id = :id" );
- $sql->bindParam( ':next_paypal_date', strtotime( '+1 month' ), PDO::PARAM_INT );
- $sql->bindParam( ':id', $user_id, PDO::PARAM_INT );
- $sql->execute( );
- return $view->render( );
- }
- } else {
- /* WTH? No money or what b'y?! */
- return $this->error( );
- }
- }
- /**
- * Free process.
- *
- * @return $view rendered view.
- */
- public function free( ) {
- $post = $this->slim->request->post( );
- $view = $this->slim->view->getView( 'Application/View/SignUp_free' );
- $view->setLayout( 'Application/View/Layout', 'body' );
- /* Register the user. */
- $this->register( $post );
- return $view->render( );
- }
- /**
- * Registers the user.
- *
- * @param $data array post data from the signup form.
- * @return int the new user ID.
- */
- private function register( $data ) {
- /* Now, lets use PhPass lib to hash the password. */
- $hasher = new PasswordHash( 9, false );
- $password = $hasher->HashPassword( $data[ 'password' ] );
- $sql = $this->db->prepare( "
- INSERT INTO user SET
- username = :username,
- password = :password,
- name = :name,
- email = :email,
- registered = :registered,
- street = :street,
- state = :state,
- country = :country,
- zip = :zip,
- account_id = :account_id
- " );
- $sql->bindParam( ':username', clean( $data[ 'username' ], true ), PDO::PARAM_STR );
- $sql->bindParam( ':password', $password, PDO::PARAM_STR );
- $sql->bindParam( ':name', clean( $data[ 'name' ], true ), PDO::PARAM_STR );
- $sql->bindParam( ':email', clean( $data[ 'email' ], true ), PDO::PARAM_STR );
- $sql->bindParam( ':registered', time( ), PDO::PARAM_INT );
- $sql->bindParam( ':street', clean( $data[ 'street' ], true ), PDO::PARAM_STR );
- $sql->bindParam( ':state', clean( $data[ 'state' ], true ), PDO::PARAM_STR );
- $sql->bindParam( ':country', clean( $data[ 'country' ], true ), PDO::PARAM_STR );
- $sql->bindParam( ':zip', clean( $data[ 'zip' ], true ), PDO::PARAM_STR );
- $sql->bindParam( ':account_id', clean( $data[ 'account_id' ], true ), PDO::PARAM_INT );
- $sql->execute( );
- return $this->db->lastInsertId( );
- }
- }
- ?>
Add Comment
Please, Sign In to add comment