2checkout api call for create sale - php

I am using 2checkout api for my website product sale. Hence I am calling 2checkout api to create sale where I can call api with total amount with a single product.It's look like flowing:
try {
$charge = Twocheckout_Charge::auth(array(
"merchantOrderId" => "123",
"token" => $_POST['token'],
"currency" => 'USD',
"total" => $first_bill['amount'],
"billingAddr" => array(
"name" => $_POST['b_name'],
"addrLine1" => $_POST['b_address'],
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => $_POST['b_country'],
"email" => $_POST['b_emaill'],
"phoneNumber" => $_POST['b_phone']
),
"shippingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'enter code hereOH',
"zipCode" => '43123',
"country" => 'USA',
"email" => 'example#2co.com',
"phoneNumber" => '555-555-5555'
)
));
if ($charge['response']['responseCode'] == 'APPROVED') {
echo $response;exit;
// echo "Thanks for your Order!";
// echo "<h3>Return Parameters:</h3>";
// echo "<pre>";
// print_r($charge);
// echo "</pre>";
}
else
{
$this->Registrationmodel->delete_account($accounts_id);
echo $charge['response']['responseCode'];
exit;
}
}
catch (Twocheckout_Error $e)
{
echo $e->getMessage();
exit;
}
}
hence when i try to set line items individual like following:
try {
$charge = Twocheckout_Charge::auth(array(
"merchantOrderId" => "123",
"token" => $_POST['token'],
"currency" => 'USD',
//"total" => $first_bill['amount'],
"billingAddr" => array(
"name" => $_POST['b_name'],
"addrLine1" => $_POST['b_address'],
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => $_POST['b_country'],
"email" => $_POST['b_emaill'],
"phoneNumber" => $_POST['b_phone']
),
"LineItem" => array(
"duration" => 'Forever',
"price" => '10',
"productId" =>'1235',
"quantity" => '1',
"recurrence" => '1 Month',
"tangible" => 'N',
"type"=>'product'
),
"shippingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => 'USA',
"email" => 'example#2co.com',
"phoneNumber" => '555-555-5555'
)
));
if ($charge['response']['responseCode'] == 'APPROVED') {
$response = $this->return_information($charge['response']['orderNumber']);
echo $response;exit;
// echo "Thanks for your Order!";
// echo "<h3>Return Parameters:</h3>";
// echo "<pre>";
// print_r($charge);
// echo "</pre>";
}
else
{
$this->Registrationmodel->delete_account($accounts_id);
echo $charge['response']['responseCode'];
exit;
}
}
catch (Twocheckout_Error $e)
{
$this->Registrationmodel->delete_account($accounts_id);
echo $e->getMessage();
exit;
}
}
It is giving me parameter error. Please can anyone help ? how to set line items using api call?

Your lineitem array is missing the 'name' param, which is a required parameter. A full list of required params can be located here .
Also your shipping array is unnecessary as your product is intangible, therefore you could remove the shipping array.
Here's a working example I use:
try {
$charge = Twocheckout_Charge::auth(array(
"sellerId" => "xxxxxxxxx",
"li_0_merchant_order_id" => "1",
"type" => "product",
"token" => $_POST['token'],
"currency" => "USD",
// "total" => "1.00", //Only use when lineitems are not passed in
"lineItems" => array(
"0" => array(
"name" => $_POST['name'],
"price" => $_POST['price'],
"quantity" => $_POST['quantity'],
"startupFee" => $_POST['startupFee'],
"tangible" => $_POST['tangible']
)
),
"billingAddr" => array(
"name" => $_POST['customer'],
"addrLine1" => $_POST['addrLine1'],
"city" => $_POST['city'],
"state" => $_POST['state'],
"zipCode" => $_POST['zipCode'],
"country" => $_POST['country'],
"email" => $_POST['email'],
"phoneNumber" => ''
)
));
if ($charge['response']['responseCode'] == 'APPROVED') {
To better fit your app, add the "duration" & "recurrence" keys & their respective values to the lineItem array in the code I provided above. The example I provided contains all required parameters for a non-tangible sale.

Related

2CheckOut - Error : Bad Request - Parameter error

Request Data to TwoCheckOut :
Response from TwoCheckOut.
{
"error": "Bad request - parameter error"
}
Here is the Code
$charge = Twocheckout_Charge::auth(array(
"sellerId" => '*HIDDEN*',
"merchantOrderId" => $orderID,
"token" => $token,
"currency" => $currency,
"total" => $itemPrice,
"billingAddr" => array(
"name" => $name,
"addrLine1" => $addrLine1,
"city" => $city,
"state" => $state,
"zipCode" => $zipCode,
"country" => $country,
"email" => $email,
"phoneNumber" => $phoneNumber
),
"shippingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => 'USA',
"email" => 'testingtester#2co.com',
"phoneNumber" => '555-555-5555'
),
"demo" => true
));
Althought i am getting unauthenticated if i send wrong 'token' key.
EDIT :
MY VIEW JS CODE
Feel free to drop your suggestions
"demo" => 'Y'
Demo key should be 'Y' not true was the issue here
TCO.loadPubKey('production');
loadPubKey should be 'production' it should not be empty

Payment api multidimensional array issue in php

My payment provider issued the following example code for their API:
$order = $mollie->orders->create([
"amount" => [
"value" => "1027.99",
"currency" => "EUR"
],
"billingAddress" => [
"streetAndNumber" => "Keizersgracht 313",
"postalCode" => "1016 EE",
"city" => "Amsterdam",
"country" => "nl",
"givenName" => "Luke",
"familyName" => "Skywalker",
"email" => "luke#skywalker.com",
],
"shippingAddress" => [
"streetAndNumber" => "Keizersgracht 313",
"postalCode" => "1016 EE",
"city" => "Amsterdam",
"country" => "nl",
"givenName" => "Luke",
"familyName" => "Skywalker",
"email" => "luke#skywalker.com",
],
"metadata" => [
"order_id" => $orderId
],
"consumerDateOfBirth" => "1958-01-31",
"locale" => "en_US",
"orderNumber" => strval($orderId),
"redirectUrl" => "{$protocol}://{$hostname}{$path}/orders/return.php?order_id={$orderId}",
"webhookUrl" => "{$protocol}://{$hostname}{$path}/orders/webhook.php",
"method" => "ideal",
"lines" => [
[
"sku" => "5702016116977",
"name" => "LEGO 42083 Bugatti Chiron",
"productUrl" => "https://shop.lego.com/nl-NL/Bugatti-Chiron-42083",
"imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image//LEGO/42083_alt1?$main$',
"quantity" => 2,
"vatRate" => "21.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "399.00"
],
"totalAmount" => [
"currency" => "EUR",
"value" => "698.00"
],
"discountAmount" => [
"currency" => "EUR",
"value" => "100.00"
],
"vatAmount" => [
"currency" => "EUR",
"value" => "121.14"
]
],
[
"type" => "digital",
"sku" => "5702015594028",
"name" => "LEGO 42056 Porsche 911 GT3 RS",
"productUrl" => "https://shop.lego.com/nl-NL/Porsche-911-GT3-RS-42056",
"imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image/LEGO/42056?$PDPDefault$',
"quantity" => 1,
"vatRate" => "21.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "329.99"
],
"totalAmount" => [
"currency" => "EUR",
"value" => "329.99"
],
"vatAmount" => [
"currency" => "EUR",
"value" => "57.27"
]
]
]
]);
What I'd like to do is to replace the hard coded second dimension "lines" with data from these session vars.
foreach ($_SESSION['cart_products'] as $cart_itm) {
$name = $cart_itm['cat_item_titel_' . $lang . ''];
$unitPrice = $cart_itm['cat_item_prijs'];
$sku = $cart_itm['cat_item_code'];
}
And this is the solution I came up with but this just doesn't work. The reason, I assume, is that the data in the $orders array is echoed as a string and not functional code as i should be. But I haven't got a clue how to aproach this issue.
$i = 0;
$lngth = count($_SESSION['cart_products']);
foreach ($_SESSION['cart_products'] as $cart_itm) {
$orders[] = '[';
$orders[] .= '"sku" => ' . $cart_itm['cat_item_code'] . ',';
$orders[] .= '"name" => ' . $cart_itm['cat_item_titel_en'] . ',';
$orders[] .= '"quantity" => 1,';
$orders[] .= '"vatRate" => "0.00",';
$orders[] .= '"unitPrice" => [
"currency" => "EUR",
"value" => "15.50"],';
$orders[] .= '"totalAmount" => [
"currency" => "EUR",
"value" => "15.50"],';
$orders[] .= '"vatAmount" => [
"currency" => "EUR",
"value" => "0.00"]';
if ($i == $lngth - 1) {
$orders[] .= ']';
}
else {
$orders[] .= '],';
}
$i++;
}
$echoOrders = implode($orders);
$order = $mollie->orders->create([
"amount" => [
"value" => "15.50",
"currency" => "EUR"
],
"billingAddress" => [
"streetAndNumber" => $address,
"postalCode" => $postalcode,
"city" => $city,
"country" => $country,
"givenName" => $firstname,
"familyName" => $name,
"email" => $email,
],
"shippingAddress" => [
"streetAndNumber" => $addressa,
"postalCode" => $postalcodea,
"city" => $citya,
"country" => $countrya,
"givenName" => $firstnamea,
"familyName" => $namea,
"email" => $email,
],
"metadata" => [
"order_id" => $orderId
],
"locale" => "nl_BE",
"orderNumber" => strval($orderId),
"redirectUrl" => "{$protocol}://{$hostname}{$path}/thankyou.php?order_id={$orderId}",
"webhookUrl" => "https://www.cluster-park.com/dev/includes/webhook.php",
"lines" => [ $echoOrders ]
]);
All suggestions are more than welcome.
Thanks!
You need to build an associative array, at the moment you are building a string. This should give you a starting point...
$lines = [];
foreach ($_SESSION['cart_products'] as $cart_itm) {
$lines[] = [ "sku" => $cart_itm['cat_item_code'],
"name" => $cart_itm['cat_item_titel_en'],
"quantity" => 1,
"vatRate" => "0.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "15.50"]
],
// Add all of the other data you have
];
}
then later in your code, you add them in using...
"lines" => $lines

Magento SOAP order failes with message "birthday is required"

I use this code to create an order through the magento (1.9) soap api v2.
The problem is, that I have to do this for a site, where the dob (day of birth) is a required field. Therefor I do get an error message - on that site - "birthday is required".
Do anybody know how to solve that problem (without disabling the dob as required field)?
$storeId = '1';
$quoteId = $client->shoppingCartCreate($session,$storeId);
echo "<br>Quote ID : " . $quoteId;
/* Set cart customer */
$guest = true;
if ($guest)
{
$customerData = array(
"firstname" => "testFirstname",
"lastname" => "testLastName",
"email" => "testEmail#mail.com",
"mode" => "guest",
"website_id" => "1"
);
}
else
{
$customer = array(
"customer_id" => '69301',
"website_id" => "1",
"group_id" => "1",
"store_id" => "1",
"mode" => "customer",
);
}
//Set cart customer (assign customer to quote)
$resultCustomerSet = $client->shoppingCartCustomerSet($session, $quoteId, $customerData,$storeId);
echo "<br>Set Customer: ";
var_dump($resultCustomerSet); //Set customer
/* Set customer addresses Shipping and Billing */
$addresses = array(
array(
"mode" => "shipping",
"firstname" => "Ahsan",
"lastname" => "testLastname",
"company" => "testCompany",
"street" => "testStreet",
"city" => "Karachi",
"region" => "Sindh",
"postcode" => "7502",
"country_id" => "PK",
"telephone" => "0123456789",
"fax" => "0123456789",
"is_default_shipping" => 0,
"is_default_billing" => 0
),
array(
"mode" => "billing",
"firstname" => "Ahsan",
"lastname" => "testLastname",
"company" => "testCompany",
"street" => "testStreet",
"city" => "Karachi",
"region" => "Sindh",
"postcode" => "7502",
"country_id" => "PK",
"telephone" => "0123456789",
"fax" => "0123456789",
"is_default_shipping" => 0,
"is_default_billing" => 0
)
);
//Set cart customer address
$resultCustomerAddress = $client->shoppingCartCustomerAddresses($session, $quoteId, $addresses,$storeId);
echo "<br>Set Customer Address: ";
var_dump($resultCustomerAddress); //Set customer addresses
/* Add products to cart */
$product = array(
'product_id' => '905',
// 'sku' => 'simple_product',
'qty' => '1',
'options' => null,
'bundle_option' => null,
'bundle_option_qty' => null,
'links' => null);
$product2 = array(
'product_id' => '904',
// 'sku' => 'simple_product',
'qty' => '2',
'options' => null,
'bundle_option' => null,
'bundle_option_qty' => null,
'links' => null);
$addToCart = $client->shoppingCartProductAdd($session, $quoteId, array($product,$product2),$storeId);
echo "<br>Add to Cart: ";
var_dump($addToCart);
/* Set payment method */
$responsePayment = $client->shoppingCartPaymentMethod($session, $quoteId, array(
'method' => 'cashondelivery',
),$storeId);
echo "<br>Set Payment method: ";
var_dump($responsePayment);
/* Set shipping method */
$setShipping = $client->shoppingCartShippingMethod($session, $quoteId, 'flatrate_flatrate',$storeId);
echo "<br>Set Shipping method: ";
var_dump($setShipping);
/* Set Discount Code */
try
{
$result = $client->shoppingCartCouponAdd($session, $quoteId, 'test123',$storeId);
echo "<br>Apply discount code: ";
var_dump($result);
}
catch(Exception $ex)
{
echo "<br>Discount code Failed: " . $ex->getMessage();
die();
}
$orderId = $client->shoppingCartOrder($session, $quoteId, $storeId, null);
echo "<br>Placed Order: ";
var_dump($orderId);

Cannot create order Woocommerce rest Api php

I tried the following code and tried to post values from android to place an order but every time i test it on chrome it give http 500 error, and Android gives volly server error. Dont know where i am messing up, serious help would be appriciated. Thank You
Ask me if you need to take a look at the android Code, but i think that doesnot matter here because i am giving hard coded values in $data.
<?php
require_once( 'lib/woocommerce-api.php' );
$pageNum=1;
$pageNum=$_GET['page_num'];
$options = array(
'debug' => true,
'return_as_array' => false,
'validate_url' => false,
'timeout' => 30,
'ssl_verify' => false,
);
try {
$client = new WC_API_Client( 'https://www.move2mart.com/', 'ck_0afa3a49305683160fe34189553a660053bd4e6239', 'cs_7dc38a7b52c3fdd34qw61e34090be636ae3364b196', $options );
//$data=array();
$data=[
'payment_method' => 'cod',
'payment_method_title' => 'Cash on Delivery',
'set_paid' => false,
'billing' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'city' => 'Karachi',
'email' => 'princeali#testing.com',
'phone' => '03123121995'
], 'line_items' => [
[
'product_id' => 779,
'quantity' => 1
]
]
];
print_r($client->post('orders', $data));
if($_POST !=null) {
}
else{
echo "Null POST Request";
}
} catch ( WC_API_Client_Exception $e ) {
echo $e->getMessage() . PHP_EOL;
echo $e->getCode() . PHP_EOL;
if ( $e instanceof WC_API_Client_HTTP_Exception ) {
print_r( $e->get_request() );
print_r( $e->get_response() );
}
}
$orderData = array(
"order" => array(
"billing_address" => array(
array(
"first_name" => "",
"last_name" => "",
"company" => "",
"address_1" => "",
"address_2" => "",
"city" => "",
"state" => "",
"postcode" => "",
"country" => "",
"email" => "",
"phone" => "",
)
),
"shipping_address" => array(
array(
"first_name" => "",
"last_name" => "",
"company" => "",
"address_1" => "",
"address_2" => "",
"city" => "",
"state" => "",
"postcode" => "",
"country" => "",
)
),
"customer_id" => 1,
"line_items" => array(
array(
"product_id" => 1,
"quantity" => 1
)
)
)
);
$client->orders->create($orderData);
Would you please try above code?
Finally, I figured it out after some research. Below is working code of woocommerce checkout webservice that may Help you -
/*** Just Copy & Paste and change your varriables **/
//do your initial stuff
header('Content-type: application/json');
$json_file=file_get_contents('php://input');
$jsonvalue= json_decode($json_file,true);
$user_id = $jsonvalue['user_id'];
$product_id = $jsonvalue['product_id'];
$quantity = $jsonvalue['quantity'];
//start order data
$orderData = array(
"order" => array(
'payment_method' => 'paypal',
'payment_method_title' => 'Paypal',
'set_paid' => true,
"billing_address" => array(
"first_name" => "bfname",
"last_name" => "blname",
"company" => "testcompanybilling",
"address_1" => "sec8",
"address_2" => "e32",
"city" => "noida",
"state" => "Noida",
"postcode" => "99999",
"country" => "IN",
"email" => "test#gmail.com",
"phone" => "888899999999"
),
"shipping_address" => array(
"first_name" => "sfname",
"last_name" => "slname",
"company" => "testcompanyshipping",
"address_1" => "shakkarpur",
"address_2" => "laxminigar",
"city" => "New Delhi",
"state" => "Delhi",
"postcode" => "110092",
"country" => "IN",
"email" => "testsh#gmail.com",
"phone" => "11009999"
),
"customer_id" => $user_id,
"line_items" => array(
array(
"product_id" => $product_id,
"quantity" => $quantity
)
),
'shipping_lines' => array(
array(
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => 10
)
)
)
);
//Create order usind order data
$data = $client->orders->create($orderData);
//echo '<pre>';
//print_r($data);
$result['success']='true';
$result['error']="0";
$result['msg']='Your order has been successfully placed.';
$result['data']=$data;
echo json_encode($result); `
cheers!!!

Push Array into another Array PHP

I would like to push this:
"serialNumber" => $serial,
Inside product array of $postArray and
"note" => array(
"text" => $note
),
before reservationDate Inside $postArray.
$postArray = array(
"reservationDate" => $timeData,
"emailLanguageCode" => "it_it",
"shipToCode" => $_POST['ship'],
"customer" => array(
"firstName" => $_POST['firstName'],
"lastName" => $_POST['lastName'],
"emailId" => $_POST['emailId'],
"phoneNumber" => $_POST['phoneNumber'],
"address" => array(
"addressLine1" => $_POST['addressLine1'],
"city" => $_POST['city'],
"state" => $_POST['state'],
"country" => $_POST['country'],
"postalCode" => $_POST['postalCode']
)
),
"product" => array(
"productCode" => $_POST['productCode'],
"issueReported" => $_POST['issueReported']
)
);
And result that I would like:
$postArray = array(
"note" => array(
"text" => $note
),
"reservationDate" => $timeData,
"emailLanguageCode" => "it_it",
"shipToCode" => $_POST['ship'],
"customer" => array(
"firstName" => $_POST['firstName'],
"lastName" => $_POST['lastName'],
"emailId" => $_POST['emailId'],
"phoneNumber" => $_POST['phoneNumber'],
"address" => array(
"addressLine1" => $_POST['addressLine1'],
"city" => $_POST['city'],
"state" => $_POST['state'],
"country" => $_POST['country'],
"postalCode" => $_POST['postalCode']
)
),
"product" => array(
"serialNumber" => $serial,
"productCode" => $_POST['productCode'],
"issueReported" => $_POST['issueReported']
)
);
I tried this:
array_push($postArray["product"][],$numeroSerie);
Please help.
Just simply do this
$postArray['product']['serialNumber'] = $serial;
Make First Array that which array you want in product array after use array_merge
$postArray1 = array(
"note" => array(
"text" =>15
));
$postArray = array(
"reservationDate" =>1,
"emailLanguageCode" => 2,
"shipToCode" => 3,
"customer" => array(
"firstName" => 4,
"lastName" => 5,
"emailId" => 6,
"phoneNumber" => 7,
"address" => array(
"addressLine1" => 8,
"city" =>9,
"state" => 10,
"country" => 11,
"postalCode" => 12
)
),
"product" => array(
"productCode" => 13,
"issueReported" => 14
)
);
array_merge($postArray1,$postArray);
very simple, add this two line code :
// insert "serialNumber" into "product" array
$postArray['product']['serialNumber'] = $serial;
// insert "note" into begining array element
$postArray = array('note'=>array("text" => $note))+$postArray;
These two lines,
$postArray['product'] = $serial + $postArray['product'];
$postArray = $numeroSerie + $postArray ;
And that's it,
+ operator gives union of both array and offcourse will satisfy your requirement.
Give it a try, this will work.
You will Try this i think its working fine.
$data=array("note" => array("text" => $note));
$postArray= array_merge($data,$postArray);
$postArray["product"]=array_merge(array("serialNumber"=> $serial),$postArray["product"]);

Categories