how to access items within the basket

1 week 18 hours ago #315525 by activha
Hello

I am writing a plugin to sync invoices with an external service and cannot find how to access payment items in the basket

Within this example of code I cannot get
$paymentItems->plan_id
	public function onCPayAfterPaymentStatusUpdateEvent( &$user, &$paymentBasket, &$subscriptions, $unifiedStatus,$previousUnifiedStatus, $eventType ) {
		global $_CB_framework;

............
		$paymentItems = $paymentBasket->loadPaymentItems();
		
		$items[] = array(
							"id" => $paymentBasket->id,
							"sku" => $paymentItems->plan_id,
							"name" => strip_tags($paymentItem->alias),
							"desc" => strip_tags($paymentBasket->item_name),
							"value" => $paymentItems->rate,
							"amount" => $paymentBasket->quantity,
							"tax" => $thetax ,
							"tax_id" => $paramsiv->get('tax_id', 0),
							"external_type" => $component,
							"product_id" => $paymentItems->artnum,
							);
..............

Thanks in advance
1 week 17 hours ago #315526 by krileon
loadPaymentItems returns an array of payment rows. Review the iDevAffiliate, Piwik, or GoogleAnalytics integrations for examples of its usage.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Templates - CBSubs - Hosting - Forge - Incubator - GroupJive
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM EST to 4:00 PM EST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
1 week 15 hours ago #315528 by activha
Thanks a lot :-)

How can I also retrieve the paiement data associated with the payment basket within the same trigger ?

I would like to retrieve the last paiement associated
1 week 15 hours ago #315529 by krileon
That'll already be the case with loadPaymentItems. That function specifically loads the payment rows for a basket. Review the listed integrations carefully as they're using it basically exactly how you're wanting to use it except they're using it for analytic tracking of purchases.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Templates - CBSubs - Hosting - Forge - Incubator - GroupJive
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM EST to 4:00 PM EST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
1 week 14 hours ago #315534 by activha
Well I have reviewed the plugin and wrote this one which objective is to sync baskets/payments with Joomla That Invoice Manager.
This would be a perfect fit for our customers and a nice addition to our use of CBsubs.

However I struggle to deal with payments when they are reversed or paid partially.
Would you mind to review the payment part and suggest some enhancements ?
Hope it could help someone when people need a component for invoicing (this one is pretty complementary to a lot of components including hikashop)
<?php

use CB\Database\Table\UserTable;

if ( ! ( defined( '_VALID_CB' ) || defined( '_JEXEC' ) || defined( '_VALID_MOS' ) ) ) { die( 'Direct Access to this location is not allowed.' ); }

global $_PLUGINS;
$_PLUGINS->registerFunction( 'onCPayAfterPaymentStatusUpdateEvent', 'onCPayAfterPaymentStatusUpdateEvent', 'getahainvoicemanagerTab' );

class getahainvoicemanagerTab extends cbTabHandler {
	public function onCPayAfterPaymentStatusUpdateEvent( &$user, &$paymentBasket, /** @noinspection PhpUnusedParameterInspection */ &$subscriptions, $unifiedStatus, /** @noinspection PhpUnusedParameterInspection */ $previousUnifiedStatus, /** @noinspection PhpUnusedParameterInspection */ $eventType ) {
		global $_CB_framework;

		// if ( ( ! ( ( $unifiedStatus == 'Completed' ) || ( $unifiedStatus == 'Processed' ) ) ) || ( ! is_object( $user ) ) ) {
// 			return;
// 		}

		$params							=	cbpaidApp::settingsParams();
		$invFile						=	$_CB_framework->getCfg( 'absolute_path' ) . '/components/com_invoices/helpers/helpers.php';
		if ( ! file_exists( $invFile ) ) {
			return;
		}
		$paramsiv = JComponentHelper::getParams( 'com_invoices' );
		$component = "com_comprofiler" ;
		include_once($invFile);

		$db = JFactory::getDBO();

		$is_new = false;
		$must_update = false ;
		$invoice_id = 0 ;

		//we check if this particular order had already been tracked
		$query = " SELECT id FROM #__invoices_invoices WHERE external_type = '".$component."' AND external_id = '". $paymentBasket->id."'";
		$db->setQuery($query);
		$exists = $db->loadResult();

		if(!$exists){
			$is_new = true ;
		}
		else{
			$must_update = true ;
			$invoice_id = $exists;
		}

		if($is_new || $must_update){

			$query = ' SELECT i.real_invoice_num FROM #__invoices_invoices as i WHERE i.type = 1 '.' ORDER BY i.id DESC ';
			$db->setQuery( $query );
			$real_invoice_num = $db->loadResult() + 1;
			$invoice_num = InvoicesHelper::getInvoiceNum($real_invoice_num) ;

			$taxes = array();

			$invoice_date = date('Y-m-d H:i:s');

			$app = JFactory::getApplication();
			if($app->isSite()) $language = "fr-FR"; 
			else $language = $paramsiv->get('default_language', 'en-GB');

			$link_contact = 2;
			$contact_id = 0;

			$query = " SELECT id FROM #__invoices_contacts WHERE email = '". $paymentBasket->payer_email ."'";
			$db->setQuery($query);
			$contact_id = (int)$db->loadResult();

			if($contact_id) $link_contact = 1;

			if(!$is_new){ // we are updating. These fields should NOT be updated.
				$real_invoice_num = null;
				$invoice_num = null;
				$invoice_date = null;
				$language = null;
				$link_contact = 1;
				$contact_id = null;
			}

			$invoice_data = array(
							"id" => $invoice_id ,
							"to_name" => $paymentBasket->first_name . " " .$paymentBasket->last_name,
							"to_company" => $paymentBasket->payer_business_name,
							"to_address" => $paymentBasket->address_street,
							"to_email" => $paymentBasket->payer_email,
							"to_zipcode" => $paymentBasket->address_zip,
							"to_city" => $paymentBasket->address_city,
							"to_state" => $paymentBasket->address_state,
							"to_country" => $paymentBasket->address_country,
							"to_phone" => $paymentBasket->contact_phone,
							"to_vatid" => $paymentBasket->vat_number,
							"vincular_cliente_checkbox" => $link_contact,
							"user_id" => $contact_id,
							"from_name" => $paramsiv->get('from_name'),
							"from_address" => $paramsiv->get('from_address'),
							"from_num" => $paramsiv->get('from_num'),
							"from_url" => $paramsiv->get('from_url'),
							"from_email" => $paramsiv->get('from_email'),
							"from_phone" => $paramsiv->get('from_phone'),
							"invoice_num" => $invoice_num,
							"real_invoice_num" => $real_invoice_num,
							"invoice_date" => $invoice_date,
							"template_id" => $paramsiv->get('invoice_template'),
							"currency_id" => $paramsiv->get('currency_id'),
							"language" => $language,
// 							"publish" => $paramsiv->get('activation'),
							"publish" => 1,
// 							"is_recurrent" => $paymentBasket->recurring,
							"start_publish" => null,
							"end_publish" => null,
							//to do invoice date plus 15j
							"invoice_duedate" => date('Y-m-d H:i:s', strtotime(' + 15 days')),
							"joomla_user_id" => $paymentBasket->user_id,
							"taxes" => $taxes,
							"status" => null,
							"external_type" => $component,
							"external_id" => $paymentBasket->id,
							"external_ref" => $paymentBasket->invoice,
							//"discount" => $order_discount_price,
							"sendemail" => $paramsiv->get('sendemail', 0)
							) ;


		$items = array();

		$value = ($paymentBasket->mc_gross-$paymentBasket->tax);
		$diff = ($paymentBasket->mc_gross-$value);
		
		if($paymentBasket->tax > 0) $thetax = round(( $diff / $value ) * 100, $paramsiv->get('roundtax', 2)) ;
		else $thetax = 0 ;
		

		$products = $paymentBasket->loadPaymentItems();

		foreach($products as $product){ 
				$name = $product->alias;
				$sku = $product->plan_id;
			
		
		$items[] = array(
							"id" => $paymentBasket->id,
							"sku" => $sku,
							"name" => strip_tags($name),
							"desc" => strip_tags($paymentBasket->item_name),
							"value" => $value,
							"amount" => $paymentBasket->quantity,
							"tax" => $thetax  ,
							"tax_id" => $paramsiv->get('tax_id', 0),
							"external_type" => $component,
							"product_id" => $products->artnum,
							);
		}
		
		//Shipping
		if($paymentBasket->mc_shipping > 0){

			$shipping_name = "";
			$shipping_name = $paymentBasket->shipping;

			$shipping_price = $paymentBasket->mc_shipping;

			$shipping_item_exists = 0;

			if($must_update){
				$query = " SELECT id FROM #__invoices_items WHERE invoice_id = '".$invoice_id."' AND external_type = '".$component."_shipping' ";
				$db->setQuery($query);
				$shipping_item_exists = $db->loadResult();
			}

			$shipping_item_id = $shipping_item_exists ;

			$items[] = array(
								"id" => $shipping_item_id,
								"sku" => "",
								"name" => JText::_("Shipping"),
								"desc" => $shipping_name,
								"value" => $shipping_price,
								"amount" => 1,
								"tax" => ( $theorder->tax / $shipping_price ) * 100,
								"tax_id" => $paramsiv->get('tax_id', 0),
								"external_type" => $component."_shipping",
								"product_id" => 0,
								);
		}

		//Payment
		if($paymentBasket->mc_gross > 0){

			$payment_name = "";
			$payment_name = $paymentBasket->payment_type;

			$payment_item_exists = 0 ;

			if($must_update){
				$query = " SELECT id FROM #__invoices_items WHERE invoice_id = '".$invoice_id."' AND external_type = '".$component."_payment'";
				$db->setQuery($query);
				$payment_item_exists = $db->loadResult();
			}

			$payment_item_id = $payment_item_exists ;

			$items[] = array(
								"id" => $payment_item_id,
								"sku" => $sku,
								"name" => strip_tags($name),
								"desc" => strip_tags($paymentBasket->item_name),
								"value" => $value,
								"amount" => $paymentBasket->quantity,
								"tax" => $thetax ,
								"tax_id" => $paramsiv->get('tax_id', 0),
								"external_type" => $component."_payment",
								"product_id" => $products->artnum

								);
		}
		
		$payments = array();

		if($paymentBasket->payment_status == "Completed") $payment_status = 1 ;
		else $payment_status = 0 ;

		$payment_exists = 0 ;

		//if(!$paymentBasket->payment_gross) $paymentBasket = $paymentBasket->old;

		if($must_update){
			$query = " SELECT id FROM #__invoices_payments WHERE invoice_id = '".$invoice_id."' AND payment_type = 'payment_".$paymentBasket->payment_method . "' ";
			$db->setQuery($query);
			$payment_exists = $db->loadResult();
		}

		$payment_id = $payment_exists;

		$payments[0] = array(
							"id" => $payment_id,
							"payment_details" => $paymentBasket->txn_id,
							"payment_datetime" => $paymentBasket->time_completed_date,
							"payment_amount" => $paymentBasket->mc_gross,
							"payment_type" => "payment_".$paymentBasket->payment_method,
							"payment_description" => $paymentBasket->payment_method . " - ".$paymentBasket->payment_type . " <br /> id : " . $paymentBasket->txn_id,
							"payment_id" => $paymentBasket->id,
							"payment_status" => $payment_status
							);
		if($must_update){
			$query = " SELECT id FROM #__cbsubs_payments WHERE payment_basket_id = '".$paymentBasket->id."' ORDER BY time_paid DESC LIMIT 1";
			$db->setQuery($query);
			$last_payment = $db->loadResult();
			
			$payments[0] = array(
							"id" => 0,
							"payment_details" => $paymentBasket->txn_id,
							"payment_datetime" => $last_payment->time_paid,
							"payment_amount" => $last_payment->mc_gross,
							"payment_type" => "payment_".$paymentBasket->payment_method,
							"payment_description" => $paymentBasket->payment_method . " - ".$paymentBasket->payment_type . " <br /> id : " . $paymentBasket->txn_id,
							"payment_id" => $last_payment->id,
							"payment_status" => $payment_status
							);
		
		}						

		InvoicesHelper::create_invoice($invoice_data, $items, $payments);
		}
	}	
}
1 week 13 hours ago #315537 by krileon
onCPayAfterPaymentStatusUpdateEvent trigger will certainly fire for reversals. $unifiedStatus for it should be Reversed when that happens.

However I struggle to deal with payments when they are reversed or paid partially.

Can you be more specific? What exactly are you struggling with?


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Templates - CBSubs - Hosting - Forge - Incubator - GroupJive
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM EST to 4:00 PM EST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
Moderators: beatnantkrileon
Time to create page: 0.422 seconds
Facebook Twitter Google LinkedIn