partial #2242 - separate user and payment type table

This commit is contained in:
Daniel Camargo 2013-12-17 14:49:28 -02:00
parent 3ec5e9c404
commit f5b1040fdb
6 changed files with 137 additions and 22 deletions

View File

@ -0,0 +1,16 @@
CREATE TABLE `user_payment_type` (
`id_user_payment_type` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_user` int(11) unsigned NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`stripe_id` varchar(255) DEFAULT NULL,
`card` varchar(16) DEFAULT NULL,
`card_type` enum('visa','mastercard','amex','discover') DEFAULT NULL,
`balanced_id` varchar(255) DEFAULT NULL,
`card_exp_year` int(4) DEFAULT NULL,
`card_exp_month` int(2) DEFAULT NULL,
`date` DATETIME NOT NULL,
PRIMARY KEY (`id_user_payment_type`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
ALTER TABLE `user_payment_type` ADD CONSTRAINT `user_payment_type_ibfk1` FOREIGN KEY(`id_user`) REFERENCES `user`(`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -274,19 +274,6 @@ class Crunchbutton_Order extends Cana_Table {
$this->txn = $this->transaction();
}
if ($this->_customer->id) {
switch (c::config()->processor) {
case 'stripe':
default:
$user->stripe_id = $this->_customer->id;
break;
case 'balanced':
$user->balanced_id = $this->_customer->id;
break;
}
}
$user->location_lat = $params['lat'];
$user->location_lon = $params['lon'];
@ -297,12 +284,6 @@ class Crunchbutton_Order extends Cana_Table {
$user->address = $this->address;
}
if ($this->pay_type == 'card' && $params['card']['id']) {
$user->card = '************'.$params['card']['lastfour'];
$user->card_exp_year = $params['card']['year'];
$user->card_exp_month = $params['card']['month'];
}
$user->pay_type = $this->pay_type;
$user->delivery_type = $this->delivery_type;
$user->tip = $this->tip;
@ -313,9 +294,56 @@ class Crunchbutton_Order extends Cana_Table {
$user->saving_from = $user->saving_from.'Order->process 2 - ';
$user->save();
$user = new User($user->id_user);
$user = new User( $user->id_user );
$this->_user = $user;
// If the pay_type is card
if ($this->pay_type == 'card' ) {
// Verify if the user already has a payment type
$payment_type = $user->payment_type();
if( !$payment_type ){
// Copy the last user's payment
$payment_type = Crunchbutton_User_Payment_Type::copyPaymentFromUserTable( $user->id_user );
}
$saveThisPayment = false;
// The user hasnt any payment type, so lets create one
if( $payment_type ){
// Compare this payment with the last one
if( $params['card']['id'] && $params['card']['year'] && $params['card']['lastfour'] && $params['card']['month'] && (
$user_payment_type->card != '************'.$params['card']['lastfour'] ||
$user_payment_type->card_exp_year != $params['card']['year'] ||
$user_payment_type->card_exp_month != $params['card']['month'] ) ){
$saveThisPayment = true;
}
} else {
$saveThisPayment = true;
}
if( $saveThisPayment ){
$user_payment_type = new Crunchbutton_User_Payment_Type();
$user_payment_type->id_user = $user->id_user;
$user_payment_type->active = 1;
if ($this->_customer->id) {
switch (c::config()->processor) {
case 'stripe':
default:
$user_payment_type->stripe_id = $this->_customer->id;
break;
case 'balanced':
$user_payment_type->balanced_id = $this->_customer->id;
break;
}
}
$user_payment_type->card = '************'.$params['card']['lastfour'];
$user_payment_type->card_type = $params['card']['card_type'];
$user_payment_type->card_exp_year = $params['card']['year'];
$user_payment_type->card_exp_month = $params['card']['month'];
$user_payment_type->date = date('Y-m-d H:i:s');
$user_payment_type->save();
// Desactive others payments
$user_payment_type->desactiveOlderPaymentsType( $user->id_user, $user_payment_type->id_user_payment_type );
}
}
// If the user typed a password it will create a new user auth
if( $params['password'] != '' ){
$params_auth = array();
@ -545,14 +573,20 @@ class Crunchbutton_Order extends Cana_Table {
switch (c::config()->processor) {
case 'stripe':
default:
if( $user && $user->payment_type() ){
$stripe_id = $user->payment_type()->stripe_id;
}
$charge = new Charge_Stripe([
'stripe_id' => $user->stripe_id
'stripe_id' => $stripe_id
]);
break;
case 'balanced':
if( $user && $user->payment_type() ){
$balanced_id = $user->payment_type()->balanced_id;
}
$charge = new Charge_Balanced([
'balanced_id' => $user->balanced_id
'balanced_id' => $balanced_id
]);
break;
}

View File

@ -222,6 +222,15 @@ class Crunchbutton_User extends Cana_Table {
$out['ip'] = $_SERVER['REMOTE_ADDR'];
$out['email'] = $this->email ? $this->email : $this->email();
// Get user payment type
$payment_type = $this->payment_type();
if( $payment_type ){
$out[ 'card' ] = $payment_type->card;
$out[ 'card_type' ] = $payment_type->card_type;
$out[ 'card_exp_year' ] = $payment_type->card_exp_year;
$out[ 'card_exp_month' ] = $payment_type->card_exp_month;
}
if (c::env() == 'beta' || c::env() == 'local') {
$out['debug'] = true;
}
@ -234,6 +243,10 @@ class Crunchbutton_User extends Cana_Table {
return $out;
}
public function payment_type(){
return Crunchbutton_User_Payment_Type::getUserPaymentType( $this->id_user );
}
public function getLastNote(){
$lastOrderNotes = $this->lastOrder();
if( $lastOrderNotes->notes ){

View File

@ -0,0 +1,50 @@
<?php
class Crunchbutton_User_Payment_Type extends Cana_Table {
public function getUserPaymentType( $id_user = null ){
$id_user = ( $id_user ) ? $id_user : c::user()->id_user;
if( $id_user ){
$payment = Crunchbutton_User_Payment_Type::q( 'SELECT * FROM user_payment_type WHERE id_user = "' . $id_user . '" AND active = 1 ORDER BY id_user_payment_type DESC LIMIT 1' );
if( $payment->id_user_payment_type ){
return $payment;
}
}
return false;
}
public function desactiveOlderPaymentsType( $id_user, $id_user_payment_type ){
$query = 'UPDATE user_payment_type SET active = 0 WHERE id_user = ' . $id_user . ' AND id_user_payment_type != ' . $id_user_payment_type;
c::db()->query( $query );
}
public function copyPaymentFromUserTable( $id_user = null ){
$id_user = ( $id_user ) ? $id_user : c::user()->id_user;
if( $id_user ){
$user = Crunchbutton_User::o( $id_user );
if( $user->card && $user->card_exp_year && $user->card_exp_month ){
$user_payment_type = new Crunchbutton_User_Payment_Type();
$user_payment_type->id_user = $user->id_user;
$user_payment_type->active = 1;
$user_payment_type->stripe_id = $user->stripe_id;
$user_payment_type->balanced_id = $user->balanced_id;
$user_payment_type->card = $user->card;
$user_payment_type->card_type = $user->card_type;
$user_payment_type->card_exp_year = $user->card_exp_year;
$user_payment_type->card_exp_month = $user->card_exp_month;
$user_payment_type->date = date('Y-m-d H:i:s');
$user_payment_type->save();
return Crunchbutton_User_Payment_Type::o( $user_payment_type->id_user_payment_type );
}
}
return false;
}
public function __construct($id = null) {
parent::__construct();
$this
->table('user_payment_type')
->idVar('id_user_payment_type')
->load($id);
}
}

View File

@ -931,6 +931,7 @@ App.tokenizeCard = function(card, complete) {
res.status = true;
res.id = response.data.id;
res.uri = response.data.uri;
res.card_type = response.data.card_type;
res.lastfour = response.data.last_four;
res.month = card.expiration_month;
res.year = card.expiration_year;

View File

@ -597,6 +597,7 @@ NGApp.factory( 'OrderService', function ($http, $location, $rootScope, $filter,
id: card.id,
uri: card.uri,
lastfour: card.lastfour,
card_type: card.card_type,
month: card.month,
year: card.year
};