@ -49,6 +49,12 @@ function fulcrm_shoppingcart_menu() {
'access callback' => 'user_access',
'access arguments' => array('access content'),
);
$items[ 'fulcrm/checkout/coupon/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_coupon',
'page arguments' => array(3,4),
'type' => MENU_CALLBACK,
'access callback' => 'user_access',
'access arguments' => array('access content'),
);
$items[ 'fulcrm/checkout/success/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_checkout_success',
'page arguments' => array(3,4),
'type' => MENU_CALLBACK,
@ -81,7 +87,7 @@ function fulcrm_shoppingcart_theme( $existing, $type, $theme, $path ) {
'fulcrm_shoppingcart_cart_item' => array( 'variables' => array( 'shoppingitem' => null ), // data returned from fulcrm API
'template' => 'fulcrm_shoppingcart_cart_item',
),
'fulcrm_shoppingcart_transaction_checkout' => array( 'variables' => array( 'transaction' => null ), // data returned from fulcrm API
'fulcrm_shoppingcart_transaction_checkout' => array( 'variables' => array( 'transaction' => null, 'use_coupon_url' => null ), // data returned from fulcrm API
'template' => 'fulcrm_shoppingcart_transaction_checkout',
),
'fulcrm_shoppingcart_transaction_items' => array( 'variables' => array( 'items' => null ), // data returned from fulcrm API
@ -90,6 +96,9 @@ function fulcrm_shoppingcart_theme( $existing, $type, $theme, $path ) {
'fulcrm_shoppingcart_transaction_item' => array( 'variables' => array( 'item' => null ), // data returned from fulcrm API
'template' => 'fulcrm_shoppingcart_transaction_item',
),
'fulcrm_shoppingcart_transaction_payment' => array( 'variables' => array( 'item' => null ), // data returned from fulcrm API
'template' => 'fulcrm_shoppingcart_transaction_payment',
),
'fulcrm_shoppingcart_transaction_complete' => array( 'variables' => array( 'transaction' => null ), // data returned from fulcrm API
'template' => 'fulcrm_shoppingcart_transaction_complete',
),
@ -142,6 +151,11 @@ function fulcrm_shoppingcart_get_session_cart( $create = true ) {
}
}
function fulcrm_shoppingcart_coupon_url( $transaction_id ) {
$hmac = fulcrm_crypto_object_id_hmac( 'transaction', $transaction_id );
return url( 'fulcrm/checkout/coupon/' . $transaction_id . '/' . $hmac );
}
function fulcrm_shoppingcart_buy_url( $product_id ) {
$hmac = fulcrm_crypto_object_id_hmac( 'product', $product_id );
return url( 'fulcrm/buy/' . $product_id . '/' . $hmac );
@ -456,6 +470,60 @@ function fulcrm_shoppingcart_cart() {
return theme( 'fulcrm_shoppingcart_cart', array( 'cart' => $cart ) );
}
function fulcrm_shoppingcart_coupon_form_submit( $form, &$form_state ) {
$transaction_id = $form_state[ 'fulcrm_shoppingcart' ][ 'transaction_id' ];
$coupon_code = $form_state[ 'values' ][ 'coupon_code' ];
if ( $coupon_code && $transaction_id ) {
$paymentitem_data = fulcrm_apiv2_POST( 'transaction/' . $transaction_id . '/coupon/', array(), array( 'coupon_code' => $coupon_code,
'expand' => implode( ',', array( 'd' ) ) ) );
if ( $paymentitem_data[ 'success' ] ) {
if ( count( $paymentitem_data[ 'data' ] ) ) {
drupal_set_message( t('Coupon code applied!'), 'info' );
} else {
drupal_set_message( t('That is not a valid coupon code.'), 'warning' );
}
} else {
drupal_set_message( t('There was a problem redeeming your coupon.'), 'error' );
}
} else {
drupal_set_message( t('That is not a valid coupon code.'), 'error' );
}
drupal_goto( 'fulcrm/checkout' );
}
function fulcrm_shoppingcart_coupon_form( $form, &$form_state, $transaction_id ) {
$form_state[ 'fulcrm_shoppingcart' ][ 'transaction_id' ] = $transaction_id;
$form = array();
$form[ 'title_coupon' ] = array( '#type' => 'markup',
'#markup' => '<h1>' . t('Redeem Coupon') . '</h1>',
);
$form[ 'coupon_code' ] = array( '#type' => 'textfield',
'#title' => t('Coupon Code'),
'#description' => t('Please carefully type the coupon code here. Coupons can only be used once, and are non-refundable.'),
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
'#value' => t('Use Coupon'),
'#submit' => array( 'fulcrm_shoppingcart_coupon_form_submit' ),
);
return $form;
}
function fulcrm_shoppingcart_coupon( $transaction_id, $hmac ) {
if ( fulcrm_crypto_check_object_id_hmac( 'transaction', $transaction_id, $hmac ) ) {
return drupal_get_form( 'fulcrm_shoppingcart_coupon_form', $transaction_id );
} else {
drupal_goto( 'fulcrm/checkout' );
}
}
function fulcrm_shoppingcart_checkout() {
$cart = fulcrm_shoppingcart_get_session_cart( false );
if ( $cart && array_key_exists( 'url', $cart ) ) {
@ -472,9 +540,12 @@ function fulcrm_shoppingcart_checkout() {
$transaction_data2 = fulcrm_apiv2_PATCH( $transaction_data[ 'data' ][ 'url' ],
$transaction_push, $query = array( 'expand' => implode( ',', array( 'd',
'lineitems',
'paymentitems',
) ) ) );
if ( $transaction_data2[ 'success' ] ) {
return theme( 'fulcrm_shoppingcart_transaction_checkout', array( 'transaction' => $transaction_data2[ 'data' ] ) );
return theme( 'fulcrm_shoppingcart_transaction_checkout', array( 'transaction' => $transaction_data2[ 'data' ],
'use_coupon_url' => fulcrm_shoppingcart_coupon_url( $transaction_data[ 'data' ][ 'id' ] ),
) );
}
return 'problem 3';
}