PHP and PayPal IPN array - php

I am getting PayPal's IPN in array form like this:
$ipn = array(
'mc_gross' => '15.50',
'protection_eligibility' => 'Eligible',
'address_status' => 'confirmed',
'item_number1' => '1',
'tax' => '0.00',
'item_number2' => '2',
'payer_id' => 'XXXXXXXXXXXXX',
'address_street' => '1 Main St',
'payment_date' => '02:41:18 Nov 23, 2011 PST',
'payment_status' => 'Completed',
'charset' => 'windows-1252',
'address_zip' => '95131',
'mc_shipping' => '0.00',
'mc_handling' => '0.00',
'first_name' => 'Test',
'mc_fee' => '0.75',
'address_country_code' => 'US',
'address_name' => 'Test User',
'custom' => '',
'payer_status' => 'verified',
'business' => 'receiver#example.com',
'address_country' => 'United States',
'num_cart_items' => '2',
'mc_handling1' => '0.00',
'mc_handling2' => '0.00',
'address_city' => 'San Jose',
'payer_email' => 'payer#example.com',
'mc_shipping1' => '0.00',
'mc_shipping2' => '0.00',
'txn_id' => '8RE82733S8684874F',
'payment_type' => 'instant',
'last_name' => 'User',
'address_state' => 'CA',
'item_name1' => 'trilogy2',
'receiver_email' => 'receiver#example.com',
'item_name2' => 'trilogy1',
'payment_fee' => '0.75',
'quantity1' => '1',
'quantity2' => '1',
'receiver_id' => 'XXXXXXXXXXXXX',
'txn_type' => 'cart',
'mc_gross_1' => '5.55',
'mc_currency' => 'USD',
'mc_gross_2' => '9.95',
'residence_country' => 'US',
'transaction_subject' => 'Shopping Carttrilogy2trilogy1',
'payment_gross' => '15.50',
'' => ''
);
In this case customer has ordered only 2 items: 'item_name1' and 'item_name2'
Obviously, a customer can order X number of products and PayPal will include an 'item_nameX' in the notification data. Is there a way to go through the array and extract all names and assign them to variables so I will have something like:
$ordered_item1 = $ipn['item_name1'];
$ordered_item2 = $ipn['item_name2'];
...
$ordered_itemXYZ = $ipn['item_nameXYZ'];
Thanks for any help.

If you have data named with a prefix followed by a count, you should be using an array, not separate variables. To fill an array with the item information:
foreach ($ipn as $key => $value) {
if (preg_match('/(?P<field>[a-z]+)_?(?P<i>\d+)/', $key, $parts)) {
$ordered_items[(int) $parts['i']][$parts['field']] = $value;
}
}

Related

Get specific data from the array

Since I'm really unexperienced with PHP, I'm having trouble retrieving specific data from the following array that I receive via API:
I need the values of
'id'
'amount'
'statsTags'
Unfortunatly the following code retrieves an undefined index error:
echo $commissions['TPerformant\API\Model\Commission::__set_state']['id'];
This is the content of the var_dump
var_dump($commissions):
array (
0 =>
TPerformant\API\Model\Commission::__set_state(array(
'id' => 457171,
'userId' => 8917,
'actionid' => 456657,
'actiontype' => 'sale',
'amount' => '0.31',
'status' => 'pending',
'affrequestId' => NULL,
'description' => 'cosmetics|Mascara Extra Super Lash Curved Brush 8 mlx1 (7% from 6.66), cosmetics|Sampon uscat Blush Dry 50 mlx1 (7% from 8.33), cosmetics|Crema de maini si corp cu Lavandax1 (7% from 4.99)',
'createdAt' => '2016-12-24T16:48:07Z',
'updatedAt' => '2016-12-24T16:48:07Z',
'reason' => NULL,
'statsTags' => ',undefinedDesktopnulla60949bd-0b06-4fd6-a324-0d65351c10e9,',
'history' => NULL,
'currency' => 'EUR',
'workingCurrencyCode' => 'RON',
'programId' => 882,
'amountInWorkingCurrency' => '1.4',
'program' =>
TPerformant\API\Model\AffiliateProgram::__set_state(array(
'affrequest' => NULL,
'id' => NULL,
'slug' => 'elefant-ro',
'name' => 'elefant.ro ',
'mainUrl' => NULL,
'baseUrl' => NULL,
'description' => NULL,
'activatedAt' => NULL,
'userId' => NULL,
'uniqueCode' => NULL,
'status' => NULL,
'cookieLife' => NULL,
'tos' => NULL,
'productFeedsCount' => NULL,
'productsCount' => NULL,
'bannersCount' => NULL,
'approvalTime' => NULL,
'currency' => NULL,
'enableLeads' => NULL,
'enableSales' => NULL,
'defaultLeadCommissionRate' => NULL,
'defaultLeadCommissionType' => NULL,
'defaultSaleCommissionRate' => NULL,
'defaultSaleCommissionType' => NULL,
'approvedCommissionCountRate' => NULL,
'approvedCommissionAmountRate' => NULL,
'paymentType' => 'postpaid',
'balanceIndicator' => NULL,
'downtime' => NULL,
'averagePaymentTime' => NULL,
'logoId' => NULL,
'logoPath' => NULL,
'userLogin' => 'elefant',
'category' => NULL,
'countries' => NULL,
'ignoreIPs' => NULL,
'requester' =>
TPerformant\API\HTTP\Affiliate::__set_state(array(
'accessToken' =>
array (
0 => 'notrelevant',
),
'clientToken' =>
array (
0 => 'notrelevant',
),
'uid' =>
array (
0 => 'notrelevant',
),
'userData' =>
TPerformant\API\Model\Affiliate::__set_state(array(
'id' => 8917,
'email' => 'notrelevant',
'login' => 'notrelevant',
'name' => 'notrelevant',
'role' => 'affiliate',
'uniqueCode' => 'notrelevant',
'createdAt' => '2012-04-17T10:25:11Z',
'avatarUrl' => 'https://secure.gravatar.com/avatar/2aa7cbeda20ce7dee64a2764004ef7b5?d=https://network.2performant.com%2Fimg%2Favatar-default.jpg',
'newsletterSubscription' => true,
'userInfo' =>
stdClass::__set_state(array(
'address' => 'Bucuresti',
'city' => 'Bucuresti',
'country' => 'Romania',
'display_name' => 'regular',
'firstname' => 'Andrei',
'lastname' => 'Ioniță',
'organization' => '',
'phone' => '',
'state' => 'Bucuresti',
'typeofbusiness' => '',
)),
'requester' => NULL,
)),
)),
)),
'publicActionData' =>
stdClass::__set_state(array(
'created_at' => '2016-12-24T16:48:07Z',
'updated_at' => '2016-12-24T16:48:07Z',
'rate' => '7.0',
'amount' => '4.41',
'ad_type' => 'quicklink',
'ad_id' => '882',
'source_ip' => '109.99.30.73',
'description' => 'cosmetics|Mascara Extra Super Lash Curved Brush 8 mlx1 (7% from 6.66), cosmetics|Sampon uscat Blush Dry 50 mlx1 (7% from 8.33), cosmetics|Crema de maini si corp cu Lavandax1 (7% from 4.99)',
'working_currency_code' => 'RON',
'amount_in_working_currency' => '19.98',
)),
'publicClickData' =>
stdClass::__set_state(array(
'created_at' => '2016-12-24T16:13:20Z',
'source_ip' => '109.99.30.73',
'url' => 'https://www.maller.ro/voucher-elefant-ro-40-reducere/',
'redirect_to' => 'http://www.elefant.ro/oferte-speciale#utm_source=2parale&utm_medium=Campaign&utm_campaign=2c42f3c4e',
'stats_tags' => ',undefinedDesktopnulla60949bd-0b06-4fd6-a324-0d65351c10e9,',
)),
'requester' =>
TPerformant\API\HTTP\Affiliate::__set_state(array(
'accessToken' =>
array (
0 => 'notrelevant',
),
'clientToken' =>
array (
0 => 'notrelevant',
),
'uid' =>
array (
0 => 'notrelevant',
),
'userData' =>
TPerformant\API\Model\Affiliate::__set_state(array(
'id' => 8917,
'email' => 'notrelevant',
'login' => 'notrelevant',
'name' => 'Andrei Ioniță',
'role' => 'affiliate',
'uniqueCode' => 'notrelevant',
'createdAt' => '2012-04-17T10:25:11Z',
'avatarUrl' => 'https://secure.gravatar.com/avatar/2aa7cbeda20ce7dee64a2764004ef7b5?d=https://network.2performant.com%2Fimg%2Favatar-default.jpg',
'newsletterSubscription' => true,
'userInfo' =>
stdClass::__set_state(array(
'address' => 'Bucuresti',
'city' => 'Bucuresti',
'country' => 'Romania',
'display_name' => 'regular',
'firstname' => 'Andrei',
'lastname' => 'Ioniță',
'organization' => '',
'phone' => '',
'state' => 'Bucuresti',
'typeofbusiness' => '',
)),
'requester' => NULL,
)),
)),
)),

How to get unique values from an array?

I have address array. I want to get unique address records from below array. I have used lots of logic but all fails to extract unique address. If we get any differences between keys like address_one, address_two etc. with another one, then I will consider as unique, but here some array values same with others.
Array
(
0 => Array
(
'address_one' => 'qqqqqqqqqq',
'address_two' => 'wwwwww',
'zipcode' => '212121',
'country_id' => '1',
'country_name' => 'United States',
'state_id' => '5',
'state_name' => 'AP',
'city_id' => '3',
'city_name' => 'Bhopal'
),
1 => Array
(
'address_one' => 'lkl',
'address_two' => 'ik2',
'zipcode' => '564564',
'country_id' => '1',
'country_name' => 'United States',
'state_id' => '1',
'state_name' => 'Madhya Pradesh',
'city_id' => '1',
'city_name' => 'Indore'
),
2 => Array
(
'address_one' => 'ace1',
'address_two' => 'caldrys1',
'zipcode' => '564561',
'country_id' => '91',
'country_name' => 'Guinea',
'state_id' => '3',
'state_name' => 'AL',
'city_id' => '3',
'city_name' => 'Bhopal'
),
3 => Array
(
'address_one' => '',
'address_two' => '',
'zipcode' => '',
'country_id' => '',
'country_name' => '',
'state_id' => '',
'state_name' => '',
'city_id' => '',
'city_name' => ''
),
4 => Array
(
'address_one' => '',
'address_two' => '',
'zipcode' => '',
'country_id' => '',
'country_name' => '',
'state_id' => '',
'state_name' => '',
'city_id' => '',
'city_name' => ''
),
5 => Array
(
'address_one' => 'lkl',
'address_two' => 'ik2',
'zipcode' => '564564',
'country_id' => '1',
'country_name' => 'United States',
'state_id' => '1',
'state_name' => 'Madhya Pradesh',
'city_id' => '1',
'city_name' => 'Indore'
),
6 => Array
(
'address_one' => 'Ace',
'address_two' => 'Matru Line',
'zipcode' => '483504',
'country_id' => '100',
'country_name' => 'India',
'state_id' => '1',
'state_name' => 'Madhya Pradesh',
'city_id' => '2',
'city_name' => 'Katni'
),
7 => Array
(
'address_one' => 'lkl',
'address_two' => 'ik2',
'zipcode' => '564564',
'country_id' => '1',
'country_name' => 'United States',
'state_id' => '1',
'state_name' => 'Madhya Pradesh',
'city_id' => '1',
'city_name' => 'Indore'
),
8 => Array
(
'address_one' => 'ace1',
'address_two' => 'caldrys1',
'zipcode' => '564561',
'country_id' => '91',
'country_name' => 'Guinea',
'state_id' => '3',
'state_name' => 'AL',
'city_id' => '3',
'city_name' => 'Bhopal'
)
);
To get unique multidimensional array you can use this :
$output_array = array_map(
"unserialize",
array_unique(array_map("serialize", $input_array))
);
Please use this to get unique array.
$output_array = array_unique($input_array, SORT_REGULAR);

cakephp manually building arrays

I have a relationship where a quote habtm applicants. I am trying to get a quote to save with multiple applicants at once. I already have an array of the applicants I need but I don't know how to format that array to get it to save when I insert it into the quote array.
The applicant array looks like this:
array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
)
I need one that looks like this:
array(
'Applicants' => array(
'id' => 435,
'id' => 436,
'id' => 66
)
)
How might I go about doing this?
Or is there a better way?
When I save a quote the array looks like this:
array(
'QuoteButton' => 'Submit',
'Quote' => array(
'date' => array(
'day' => '13',
'month' => '10',
'year' => '2013'
),
'description' => '',
'quote_accepted' => '0',
'research_accepted' => '0',
'cc_accepted' => '0',
'pesel_accepted' => '0',
'setfees_accepted' => '0',
'total' => '0'
),
'Applicant' => array(
'id' => '66'
),
How do I insert more than one applicant into the array?
An array can't have the same id, but can crate another array like this:
$datas = array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
);
$ids = array();
foreach($datas as $data => $applicants) {
$ids[] = $applicants['Applicant']['id'];
}
print_r($ids);
Output:
Array ( [0] => 436 [1] => 435 [2] => 66 )
How to use the ids? Like this:
foreach($ids as $key => $id) {
// do whatever you want with the applicant id
}
like tttony pointed out, array indexes need to be unique so i would:
$ids = array();
foreach($array as $applicant)
$ids[$applicant['Applicant']['id']] = null;
which will output:
Array
(
[436] =>
[435] =>
[66] =>
)
now id is key to $ids array...you could add something else as value rather than null

Magento: create bundle order programmatically

I have this code: http://pastebin.com/iFwyKM7G
Inside an event-observer class which executes after the customer registered. It creates an order automatically and it works for simple products. However I cannot figure out for the life of me how to make it work with Bundled product!
How can I make this work with a bundled product?
I DID IT!
I changed my code to the following:
$customer = Mage::getSingleton('customer/customer')->load($observer->getCustomer()->getId());
$session = Mage::getSingleton('adminhtml/session_quote');
$order_create_model = Mage::getSingleton('adminhtml/sales_order_create');
Mage::log($customer->debug(), null, 'toszodj_meg.log');
//$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
Mage::log($customer->getDefaultBillingAddress()->debug(), null, 'toszodj_meg.log');
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
$session->setCustomerId((int) $customer->getId());
$session->setStoreId((int) $storeId);
$orderData = array(
'session' => array(
'customer_id' => $customer->getId(),
'store_id' => $storeId,
),
'payment' => array(
'method' => 'banktransfer',
'po_number' => (string) '-',
),
// 123456 denotes the product's ID value
'add_products' =>array(
'2' => array(
'qty' => 1,
'bundle_option' => array(
2 => 2,
1 => 1,
),
'bundle_option_qty' => array(
2 => 1,
1 => 1,
),
),
),
'order' => array(
'currency' => 'EUR',
'account' => array(
'group_id' => $customer->getGroupId(),
'email' => (string) $customer->getEmail(),
),
'comment' => array('customer_note' => 'API ORDER'),
'send_confirmation' => 1,
'shipping_method' => 'flatrate_flatrate',
'billing_address' => array(
'customer_address_id' => $customer->getDefaultBillingAddress()->getEntityId(),
'prefix' => $customer->getDefaultBillingAddress()->getPrefix(),
'firstname' => $customer->getDefaultBillingAddress()->getFirstname(),
'middlename' => $customer->getDefaultBillingAddress()->getMiddlename(),
'lastname' => $customer->getDefaultBillingAddress()->getLastname(),
'suffix' => $customer->getDefaultBillingAddress()->getSuffix(),
'company' => $customer->getDefaultBillingAddress()->getCompany(),
'street' => $customer->getDefaultBillingAddress()->getStreet(),
'city' => $customer->getDefaultBillingAddress()->getCity(),
'country_id' => $customer->getDefaultBillingAddress()->getCountryId(),
'region' => $customer->getDefaultBillingAddress()->getRegion(),
'region_id' => $customer->getDefaultBillingAddress()->getRegionId(),
'postcode' => $customer->getDefaultBillingAddress()->getPostcode(),
'telephone' => $customer->getDefaultBillingAddress()->getTelephone(),
'fax' => $customer->getDefaultBillingAddress()->getFax(),
),
'shipping_address' => array(
'customer_address_id' => $customer->getDefaultShippingAddress()->getEntityId(),
'prefix' => $customer->getDefaultShippingAddress()->getPrefix(),
'firstname' => $customer->getDefaultShippingAddress()->getFirstname(),
'middlename' => $customer->getDefaultShippingAddress()->getMiddlename(),
'lastname' => $customer->getDefaultShippingAddress()->getLastname(),
'suffix' => $customer->getDefaultShippingAddress()->getSuffix(),
'company' => $customer->getDefaultShippingAddress()->getCompany(),
'street' => $customer->getDefaultShippingAddress()->getStreet(),
'city' => $customer->getDefaultShippingAddress()->getCity(),
'country_id' => $customer->getDefaultShippingAddress()->getCountryId(),
'region' => $customer->getDefaultShippingAddress()->getRegion(),
'region_id' => $customer->getDefaultShippingAddress()->getRegionId(),
'postcode' => $customer->getDefaultShippingAddress()->getPostcode(),
'telephone' => $customer->getDefaultShippingAddress()->getTelephone(),
'fax' => $customer->getDefaultShippingAddress()->getFax(),
),
),
);
$order_create_model->importPostData($orderData['order']);
$order_create_model->getBillingAddress();
$order_create_model->setShippingAsBilling(true);
$order_create_model->addProducts($orderData['add_products']);
$order_create_model->collectShippingRates();
$order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
$order_create_model
->initRuleData()
->saveQuote();
$order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
$order_create_model->setPaymentData($orderData['payment']);
$order_create_model
->importPostData($orderData['order'])
->createOrder();
$session->clear();
Mage::unregister('rule_data');
Mage::log('Order Successfull', null, 'siker_bammer.log');
And it works! thought the customer doesn't get notified. which iam trying to figure out now.

Trouble verifying paypal notify data with PHP

I'm trying to check the data that paypal sends to my notify_url after a transaction has been paid (in this case using the sandbox).
I've tried the sample code from paypal with fsockopen but I just couldn't get it to work properly. I also tried with cURL by manually giving a certificate, but it didn't turn out well either.
However after some changes, I managed to find a solution :
<?php
$_POST = array('mc_gross' => '9.90',
'protection_eligibility' => 'Eligible',
'address_status' => 'confirmed',
'payer_id' => '5PK5H93BE5RAQ',
'tax' => '0.00',
'address_street' => '1 Main St',
'payment_date' => '12:13:29 Jan 12, 2012 PST',
'payment_status' => 'Pending',
'charset' => 'windows-1252',
'address_zip' => '95131',
'first_name' => 'Test',
'address_country_code' => 'US',
'address_name' => 'Test User',
'notify_version' => '3.4',
'custom' => '523368657',
'payer_status' => 'verified',
'business' => 'seller_1326059109_biz#yahoo.fr',
'address_country' => 'United States',
'address_city' => 'San Jose',
'quantity' => '1',
'verify_sign' => 'AGilT0jx3itxd3XAujXRvUPipg93ATqSM88ML-PAwAoa7xGkRSWXhVR8',
'payer_email' => 'buyer_1326398444_per#yahoo.fr',
'txn_id' => '1C254380JG793833D',
'payment_type' => 'instant',
'last_name' => 'User',
'address_state' => 'CA',
'receiver_email' => 'seller_1326059109_biz#yahoo.fr',
'receiver_id' => 'FKBDH7FDLECEQ',
'pending_reason' => 'multi_currency',
'txn_type' => 'web_accept',
'item_name' => 'Compte premium winker',
'mc_currency' => 'EUR',
'item_number' => '',
'residence_country' => 'US',
'test_ipn' => '1',
'handling_amount' => '0.00',
'transaction_subject' => '523368657',
'payment_gross' => '',
'shipping' => '0.00',
'ipn_track_id' => '1v3OWX6MBMqE6Oe6-BSmCA');
$req = '?cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$url = 'https://sandbox.paypal.com/cgi-bin/webscr'.$req;
echo '<p>browse link</p>';
$content = file_get_contents($url);
var_dump($content);
I do get a result but it is "INVALID", whereas if I open the link on my browser (with the exact same informations), it is "VERIFIED" !
Any ideas ?
Thanks in advance for your replies

Categories