I am trying to test Paypals batch payout:
My code:
// ...
public function sendBatchPayment(array $payoutItems, $senderBatchId, $type= 'Email', $currency = 'CAD', $note = '') {
$payouts = new \PayPal\Api\Payout();
$senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();
$senderBatchHeader->setSenderBatchId($senderBatchId)
->setEmailSubject("You have a Payout!");
foreach ($payoutItems as $payoutItem) {
$emailExists = (isset($payoutItem['email']) && !empty($payoutItem['email']));
$amountExists = (isset($payoutItem['amount']) && !empty($payoutItem['amount']));
$senderItemIdExists = (isset($payoutItem['sender_item_id']) && !empty($payoutItem['sender_item_id']));
if(!$emailExists || !$amountExists || !$senderItemIdExists) continue;
$receiver = $payoutItem['email'];
$item_id = $payoutItem['sender_item_id'];
$value = (float) $payoutItem['amount'];
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Your Payment')
->setReceiver($receiver)
->setSenderItemId($item_id)
->setAmount(new \PayPal\Api\Currency('{
"value":$value,
"currency":"CAD"
}'));
$payouts->setSenderBatchHeader($senderBatchHeader)
->addItem($senderItem);
} // EO foreach
$request = clone $payouts;
try {
$output = $payouts->create(null, $this->apiContext);
} catch (Exception $e) {
return $e->getMessage;
}
return $output;
}
But I keep getting a 400.
I suspect its due to this being inside the loop:
$payouts->setSenderBatchHeader($senderBatchHeader)
->addItem($senderItem);
when Paypals examples are like this:
$payouts->setSenderBatchHeader($senderBatchHeader)
->addItem($senderItem1)
->addItem($senderItem2);
;
How do I add items inside the loop?
EDIT:
Error:
PayPalConnectionException in PayPalHttpConnection.php line 180:
Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payouts?.
Thankyou.
I managed to fix the issue by changing the $senderBatchId value to just:
uniqid()
instead of:
uniqid() . microtime(true)
Paypal REST API does not like it that way for some reason. If a better programmer than I can explain this I will mark his/her answer are correct.
Related
I am wiring up the Authorize.net API for a web site and I have created a cart system where all is well so far. My issue is with the way I am using a parameter in one of the class function calls. It will only work if I use single quotes surrounding my string. I cannot use a variable at all it throws an error which is the reason I am here asking this question. For example:
$lineItem->setDescription('SOME TEXT GOES HERE'); // works perfectly
$foo = 'SOME TEXT GOES HERE';
$lineItem->setDescription($foo); // fails
$lineItem->setDescription("$foo"); // fails
// I tried all kinds of ways like:
$foo = "SOME TEXT GOES HERE"; // double quotes
$lineItem->setDescription($foo); // fails
// I tried this too and this works but In my case I am within a loop
define("FOO", $foo);
$lineItem->setDescription(FOO); // works but I need to loop all the items
// I tried this
$foo = json_encode($foo);
$lineItem->setDescription($foo); // fails?
// It seems like this would work but I know this is not realistic.
$lineItem->setDescription('$foo'); // this is just for illustrative purposes
So what am I doing wrong? If I could print a variable in single quotes it would seem to work but PHP doesn't work like that. Can anyone give me another way to get this to work?
Thanks in advance.
Let me show the full function so everyone can see whats happening...
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
function AuthorizeNet(){
require 'assets/authorizenet/vendor/autoload.php';
define("AUTHORIZENET_LOG_FILE", "phplog");
// Common setup for API credentials
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($GLOBALS['authorizenet_api']);
$merchantAuthentication->setTransactionKey($GLOBALS['authorizenet_tx']);
// Create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4111111111111111");
$creditCard->setExpirationDate("2038-12");
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);
// Create the Bill To info
$billto = new AnetAPI\CustomerAddressType();
$billto->setFirstName("Ellen");
$billto->setLastName("Johnson");
$billto->setCompany("Souveniropolis");
$billto->setAddress("14 Main Street");
$billto->setCity("Pecan Springs");
$billto->setState("TX");
$billto->setZip("44628");
$billto->setCountry("USA");
$billto->setPhoneNumber("310-867-5309");
$total = 0;
$tax=0;
$shipping=0;
$items_subtotal = 0;
$discount_total = 0;
$lineItem_Array = array();
$i=0;
// Create the items
foreach($_SESSION['ms_cart'] as $cart_item){
$desc = array();
if(!empty($cart_item["size"])){
$desc[] = "Size: " . $cart_item["size"];
}
if(!empty($cart_item["color"])){
$desc[] = "Color: " . $cart_item["color"];
}
$description = implode(", ", $desc);
if(!empty($cart_item["discount_total"])){
$discount_total = $discount_total+round($cart_item["discount_total"],2);
}
$name = $cart_item["name"];
$desc = $description;
$quantity = $cart_item["qty"];
$price = $cart_item["price"];
$sku = $cart_item["uiid"];
$items_subtotal = ($price*$quantity)+$items_subtotal;
$lineItem = new AnetAPI\LineItemType();
$lineItem->setItemId($sku);
$lineItem->setName($name); // <-- this is my issue
$lineItem->setDescription($desc); // <-- also here
$lineItem->setQuantity(1);
$lineItem->setUnitPrice(1);
$lineItem->setTaxable(0); // 1 Yes 0 for no
$lineItem_Array[$i] = $lineItem;
$i++;
}
$subtotal = round($items_subtotal, 2);
if(!empty($_SESSION['ms_cart_tax'])){
$tax = number_format(round($_SESSION['ms_cart_tax'], 2), 2, '.', '');
}
if(!empty($_SESSION['ms_ship_rate'])){
$shipping = round($_SESSION['ms_ship_rate'], 2);
}
$total = ($subtotal+$tax+$shipping)-$discount_total;
$total = round($total,2);
// Create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType( "authCaptureTransaction");
$transactionRequestType->setBillTo($billto);
$transactionRequestType->setLineItems($lineItem_Array);
$transactionRequestType->setPayment($paymentOne);
$total=4; // there are 4 items in my cart loop for now I have hard coded the 4
$transactionRequestType->setAmount($total);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setTransactionRequest($transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
$response = $controller->executeWithApiResponse($GLOBALS['authorizenet_env']);
if ($response != null)
{
$tresponse = $response->getTransactionResponse();
if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )
{
echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
echo "Charge Credit Card TRANS ID : " . $tresponse->getTransId() . "\n";
}
else
{
echo "Charge Credit Card ERROR : Invalid response\n";
}
}
else
{
echo "Charge Credit card Null response returned";
}
}
This was not a good solution for me. I ended up removing the php-sdk and creating my own function to call Authorize.net's API. With some simple PHP JSON and cURL. I was up and running in an hour. Sometimes fussing around with API's can make you spend more time than just writing from scratch. I would like to thank everyone that took the time to take look and provide feedback.
Basically, im trying to make a finance section on magento, I have looked into how to place an order as part of the finance submission, everywhere I look uses the code below (Not exact):
$order = Mage::getModel('sales/order');
$store = Mage::app()->getStore();
$website_id = Mage::app()->getWebsite()->getStoreId();
$quote = Mage::getModel('sales/order')->setStoreId($store->getId());
$customer = Mage::getSingleton('customer/session')->getCustomer();
$quote->assignCustomer($customer);
$quote->setSendCconfirmation(1);
$product_ids = array();
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach($cart->getAllVisibleItems() as $item) {
$quote->addProduct($item->getProduct() , $item->getQty());
}
$shipping = $quote->getShippingAddress();
$shipping->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('flatrate_flatrate')->setPaymentMethod(array(
'method' => 'checkmo'
));
try {
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$increment_id = $service->getOrder()->getRealOrderId();
}
catch(Exception $e) {
die($e->getMessage());
}
catch(Mage_Core_Exception $d) {
die($d->getMessage());
}
And for some reason i keep getting this error:
I was loading the customer from session, and not supplying the correct model as an argument , for anyone also looking for this answer;
$customer = Mage::getModel('customer/customer')->loadByEmail(Mage::getSingleton('customer/session')->getCustomer()->getEmail());
I used paypal php sdk with this :
https://github.com/paypal/merchant-sdk-php/blob/master/samples/RecurringPayments/CreateRecurringPaymentsProfile.php
Express checkout is works well, but use recurring payments have problem : token is invalid.
Line 152 in the sdk, it's said
A timestamped token, the value of which was returned in the response
to the first call to SetExpressCheckout. Call
CreateRecurringPaymentsProfile once for each billing
agreement included in SetExpressCheckout request and use the same
token for each call. Each CreateRecurringPaymentsProfile request
creates a single recurring payments profile.
But i don't understand how to "Call CreateRecurringPaymentsProfile once in SetExpressCheckout", there is my code:
public function createPayToken($returnUrl, $cancelUrl, $payModeData) {
$itemName = $payModeData['name'];
$order = $payModeData['fee'];
// $category = 'Digital';
$category = 'Physical';
$currencyCode = "USD";
$paymentDetails = new PaymentDetailsType();
$itemAmount = new BasicAmountType($currencyCode, $order);
$itemDetails = new PaymentDetailsItemType();
$itemDetails->Name = $itemName;
$itemDetails->Amount = $itemAmount;
$itemDetails->Quantity = 1;
$itemDetails->ItemCategory = $category;
$paymentDetails->OrderTotal = new BasicAmountType($currencyCode, $order);
$paymentDetails->PaymentAction = 'Sale';
$paymentDetails->PaymentDetailsItem[] = $itemDetails;
$setECReqDetails = new SetExpressCheckoutRequestDetailsType();
$setECReqDetails->PaymentDetails[] = $paymentDetails;
$setECReqDetails->ReqConfirmShipping = 0;
$setECReqDetails->NoShipping = 1;
$setECReqDetails->AddressOverride = 0;
$setECReqDetails->CancelURL = $cancelUrl;
$setECReqDetails->ReturnURL = $returnUrl;
$setECReqType = new SetExpressCheckoutRequestType();
$setECReqType->SetExpressCheckoutRequestDetails = $setECReqDetails;
$setECReq = new SetExpressCheckoutReq();
$setECReq->SetExpressCheckoutRequest = $setECReqType;
$paypalService = new PayPalAPIInterfaceServiceService();
try {
$setECResponse = $paypalService->SetExpressCheckout($setECReq);
exit;
} catch (Exception $ex) {
echo $ex;
exit;
}
if(isset($setECResponse)) {
if($setECResponse->Ack =='Success') {
$token = $setECResponse->Token;
return $token;
}
var_dump($setECResponse);
exit;
}
return false;
}
Thank you.
You just need to make sure you're including the billing agreement information in your SetExpressCheckout request. Take a look at this sample of set of API calls to complete a recurring payments profile using Express Checkout.
You'll notice the SEC request includes parameters for L_BILLINGTYPE0 AND L_BILLINGAGREEMENTDESCRIPTION0. You need to make sure you include those, otherwise the token that you get back won't be valid for CreateRecurringPaymentsProfile.
I have been trying to export all of our invoices in a specific format for importing into Sage accounting. I have been unable to export via Dataflow as I need to export the customer ID (which strangely is unavailable) and also a couple of static fields to denote tax codes etc…
This has left me with the option of using the API to export the data and write it to a CSV. I have taken an example script I found (sorry can’t remember where in order to credit it...) and made some amendments and have come up with the following:
<?php
$website = 'www.example.com';
$api_login = 'user';
$api_key ='password';
function magento_soap_array($website,$api_login,$api_key,$list_type,$extra_info){
$proxy = new SoapClient('http://'.$website.'/api/soap/?wsdl');
$sessionId = $proxy->login($api_login, $api_key);
$results = $proxy->call($sessionId,$list_type,1);
if($list_type == 'order_invoice.list'){
/*** INVOICES CSV EXPORT START ***/
$filename = "invoices.csv";
$data = "Type,Account Reference,Nominal A/C Ref,Date,Invoice No,Net Amount,Tax Code,Tax Amount\n";
foreach($results as $invoice){
foreach($invoice as $entry => $value){
if ($entry == "order_id"){
$orders = $proxy->call($sessionId,'sales_order.list',$value);
}
}
$type = "SI";
$nominal = "4600";
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, $invoice['created_at']);
$invoicedOn = $date->format('d/m/Y');
$invoiceNo = $invoice['increment_id'];
$subtotal = $invoice['base_subtotal'];
$shipping = $invoice['base_shipping_amount'];
$net = $subtotal+$shipping;
$taxCode = "T1";
$taxAmount = $invoice['tax_amount'];
$orderNumber = $invoice['order_id'];
foreach($orders as $order){
if ($order['order_id'] == $orderNumber){
$accRef = $order['customer_id'];
}
}
$data .= "$type,$accRef,$nominal,$invoicedOn,$invoiceNo,$net,$taxCode,$taxAmount\n";
}
file_put_contents($_SERVER['DOCUMENT_ROOT']."/var/export/" . $filename, "$header\n$data");
/*** INVOICES CSV EXPORT END ***/
}else{
echo "nothing to see here";
}/*** GENERIC PAGES END ***/
}/*** END function magento_soap_array ***/
if($_GET['p']=="1")
{
magento_soap_array($website,$api_login,$api_key,'customer.list','Customer List');
}
else if($_GET['p']=="2")
{
magento_soap_array($website,$api_login,$api_key,'order_creditmemo.list','Credit Note List');
}
else if($_GET['p']=="3")
{
magento_soap_array($website,$api_login,$api_key,'sales_order.list','Orders List');
}
else if($_GET['p']=="4")
{
magento_soap_array($website,$api_login,$api_key,'order_invoice.list','Invoice List');
}
?>
This seems to be working fine, however it is VERY slow and I can’t help but think there must be a better, more efficient way of doing it…
Has anybody got any ideas?
Thanks
Marc
i think on put break; would be okey. because only one key with order_id, no need to looping after found order_id key.
if ($entry == "order_id"){
$orders = $proxy->call($sessionId,'sales_order.list',$value);
break;
}
and you can gather all call(s) and call it with multicall as example:
$client = new SoapClient('http://magentohost/soap/api/?wsdl');
// If somestuff requires api authentification,
// then get a session token
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'somestuff.method');
$result = $client->call($session, 'somestuff.method', 'arg1');
$result = $client->call($session, 'somestuff.method', array('arg1', 'arg2', 'arg3'));
$result = $client->multiCall($session, array(
array('somestuff.method'),
array('somestuff.method', 'arg1'),
array('somestuff.method', array('arg1', 'arg2'))
));
// If you don't need the session anymore
$client->endSession($session);
source
I've been working with the PHP api for google Calendar, and have been getting quite frustrated.
I downloaded the zend package with all the libraries from google's page, and have been working off the provided code to make sure it can meet my requirements.
The issue I'm running into involves getting an event back from the system. The provided code includes a demo with function getEvent($clientId, $eventId), which based on the documentation and reading the code, i would expect to return an event that is in my calendar that matches the provided Id.
So in my tests, I create a new event, and then I try to retrieve it. However, whenever I retrieve the event, Zend_data_Gdata_App_HttpException is:
function processPageLoad()
{
global $_SESSION, $_GET;
if (!isset($_SESSION['sessionToken']) && !isset($_GET['token'])) {
requestUserLogin('Please login to your Google Account.');
} else {
$client = getAuthSubHttpClient();
$id = createEvent ($client, 'Tennis with Beth',
'Meet for a quick lesson', 'On the courts',
'2010-10-20', '10:00',
'2010-10-20', '11:00', '-08');
$newEvent = getEvent($client, $id);
}
}
the code for createEvent( ) is :
function createEvent ($client, $title = 'Tennis with Beth',
$desc='Meet for a quick lesson', $where = 'On the courts',
$startDate = '2008-01-20', $startTime = '10:00',
$endDate = '2008-01-20', $endTime = '11:00', $tzOffset = '-08')
{
$gc = new Zend_Gdata_Calendar($client);
$newEntry = $gc->newEventEntry();
$newEntry->title = $gc->newTitle(trim($title));
$newEntry->where = array($gc->newWhere($where));
$newEntry->content = $gc->newContent($desc);
$newEntry->content->type = 'text';
$when = $gc->newWhen();
$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
$newEntry->when = array($when);
$createdEntry = $gc->insertEvent($newEntry);
return $createdEntry->id->text;
}
And finally the code for getEvent() is:
function getEvent($client, $eventId)
{
$gdataCal = new Zend_Gdata_Calendar($client);
$query = $gdataCal->newEventQuery();
$query->setUser('default');
$query->setVisibility('private');
$query->setProjection('full');
$query->setEvent($eventId);
try {
$eventEntry = $gdataCal->getCalendarEventEntry($query);
return $eventEntry;
} catch (Zend_Gdata_App_Exception $e) {
var_dump($e);
return null;
}
}
we have the same problem, just share my solution. since you assign $id = createEvent()
$id will have a URL values. then you use $id getEvent($client, $id);
the solution is that getCalendarEventEntry() can have a $query or just a URL.
function getEvent($client, $eventId)
{
$gdataCal = new Zend_Gdata_Calendar($client);
try {
$eventEntry = $gdataCal->getCalendarEventEntry($eventId);
return $eventEntry;
} catch (Zend_Gdata_App_Exception $e) {
var_dump($e);
return null;
}
Hope it helps!!