Laravel/Omnipay paypal, pass custom shipping amount - php

I'm trying pass a custom shipping price to paypal express checkout.
I'm using omnipay and laravel 4.
I gather this is possible as referenced in the paypal express docs but cant get it to work with omnipay Ive tried the following:
/*set api details*/
$request = $gateway->purchase([
'amount'=> $cart['total'],
'currency' => 'GBP',
'XXX' => 100
'transactionId' => $order->reference,
'returnUrl' => $returnURL . '/checkout/success',
'cancelUrl' => $returnURL . '/checkout/cancel'
])->setItems($cart['items'])->send();
/*handleResponse*/
where XXX is shipping, shippingAmt, shipping_amount, I've tried adding shipping value to each item in the cart. I've tried modifying abstractrequest.php class to add on a shipping value manually like so:
protected function getItemData()
{
$data = array();
$items = $this->getItems();
if ($items) {
foreach ($items as $n => $item) {
$data["L_PAYMENTREQUEST_0_NAME$n"] = $item->getName();
$data["L_PAYMENTREQUEST_0_DESC$n"] = $item->getDescription();
$data["L_PAYMENTREQUEST_0_QTY$n"] = $item->getQuantity();
$data["L_PAYMENTREQUEST_0_AMT$n"] = $this->formatCurrency($item->getPrice());
}
}
$data["L_PAYMENTREQUEST_0_SHIPPINGAMT"] = "3.00";
return $data;
}

I found the answer here
I was using an older version of package, so just needed to run composer update and could use the function setShippingAmount();

Related

Exact API - SalesOrderLines (Item Mandatory: Unit) even when the unit is there

We import orders into Exact API which works for UK, NL but now we are trying to import DE orders too. We have everything setted up, but when we are trying to import orders we get an error which looks like this:
Item Mandatory: Unit
Even when we try to import the unit in different ways it shows this
Invalid Reference: Unit
And we can not figure it out why it doesn't work but on UK an NL it works like a charm.
There is a piece of code for DE order import.
/** #var \Picqer\Financials\Exact\Item $item */
$item = ExactItem::getItemByEan($connection, $i->product_ean);
if (isset($item)) {
$unitPrice = round(doubleval($i->product_item_price_no_vat), 4);
$unitPriceVat = round(doubleval($i->product_item_price), 4);
$unitCode = $item->Unit;
if ($client == ExactController::CLIENT_NL) {
$item = [
'Item' => $item->ID,
'Quantity' => $i->product_quantity,
'ItemCode' => $i->product_ean,
'ItemDescription' => $i->product_name
];
} else {
$item = [
'Item' => $item->ID,
'Quantity' => $i->product_quantity,
'ItemCode' => $i->product_ean,
'ItemDescription' => $i->product_name
];
}
if ($client == ExactController::CLIENT_DE) {
$item["UnitCode"] = $unitCode;
}
if ($client == ExactController::CLIENT_NL || $client == ExactController::CLIENT_DE) {
//$item['NetPrice'] = $unitPrice;
$item['UnitPrice'] = $unitPriceVat;
} else {
$item['UnitPrice'] = $unitPrice;
}
}
It would definitely help when you had this problem and you know how to fix.
PS: I have tried to set UnitCode to ID, Code and it didn't help.
We have found a solution!
The problem was when we tried to import shipping as an item and the shipping wasn't defined in Exact DE but at UK and NL was.
Even now we don't know why it was saying that there was a problem with Units.

How to initialize a POST and a PUT method from inside a Wordpress custom endpoint

I am creating a custom endpoint in wordpress, which should do the following:
1. Create the path (done)
2. call a function, which:
2.1. Gets all the products from WooCommerce and stores them in an array (done)
2.2. Compares the products on WooCommerce with products from an external database and if there is any difference in data, get updated (PUT) by the data from the external DB
2.3. If some products exist on the external DB but do not exist on WooCommerce, create them on WooCommerce (POST)
I have managed to get all the data from WooCommerce via wc_get_products ($args) but I cannot find how to write the PUT and the POST method to update or create products.
If I use Automatic/WooCommerce to push or put products from a standalone file (not from the custom endpoint) works, but if I keep the Automatic/WooCommerce on the custom endpoint it tells me the following:
Failed to connect to localhost port 8000: Connection refused in /var/www/html/wp-content/plugins/wl-api/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 417
which makes total sense because from the custom endpoint I am already connected to WooCommerce and the Automatic/WooCommerce would attempt to connect again. So are there any methods like wc_get_products but for PUT and PUSH?
//CUSTOM ENDPOINT PHP FILE
add_action('rest_api_init', 'customEndpoint');
function customEndpoint() {
register_rest_route('endpoint', '/transfer', array(
'methods' => 'GET',
'callback' => 'get_data',
));
}
function get_data() {
include "updateOrCreate.php";
}
//updateOrCreate.php
require 'vendor/autoload.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'vendor/guzzlehttp/guzzle/src/Client.php';
use Automattic\WooCommerce;
//-----------Connect to the External DB----------
$baseUrl=...;
$externalClient = new GuzzleHttp\Client();
$res = $externalClient->request('GET', $baseUrl, [
"query" => ['fields' => $fields, 'aktiv' => true],
'auth' => ['...', '...']
]);
$articles = json_decode($res->getBody());
//-----------Connect to WooCommerce DB
$wooKey= "...";
$wooSecret= "...";
$wooCommerceBaseUrl = "http://localhost:8000";
$wooCommerceClient = new WooCommerce\Client($wooCommerceBaseUrl,
$wooKey, $wooSecret,
[
'wp_api' => true,
'version' => 'wc/v3',
]
);
//GET all products from WooCommerce
$args = array(
'status' => 'publish',
'limit' => -1,
);
$wooExistingProducts = wc_get_products($args);
$productsForWoo= [];
//check if there are no products on woocommerce:
if(empty($wooExistingProducts)){
foreach ($articles as &$article){
//Create the necessary template to initialize the connections between external products and woocommerce product template
$wooTemplate = [
...
];
array_push($productsForWoo, $wooTemplate);
//CREATE BATCHES OF 10
if(sizeof($productsForWoo) == 10){
$data = [
'create' => $productsForWoo,
];
$wooCommerceClient->post('products/batch', $data);
$productsForWoo = [];
}
}
//As there is a big chance that the last batch will have less than 10 products, push them as well to wooCommerce as a last batch
$data = [
'create' => $productsForWoo,
];
$wooCommerceClient->post('products/batch', $data);
}
// if there are existing products on woocommerce
else{
//Loop through all existing products on the external DB
foreach ($articles as &$article){
$did_match = false;
$wooTemplate = [
...
];
//loop through all products on WooCommerce
foreach ($wooExistingProducts as &$product){
//if the product id from the external db matches the id from an existing product on woocommerce
if(...){
//update that product
$wooCommerceClient->put('products/'.urlencode($product->id), $wooTemplate);
$did_match = true;
break;
}
}
//otherwise, if the product from the external db is not found on the woocommerce database, create it
if(!$did_match){
array_push($productsForWoo, $wooTemplate);
if(sizeof($productsForWoo) == 10){
$data = [
'create' => $productsForWoo,
];
$wooCommerceClient->post('products/batch', $data);
$productsForWoo = [];
}
}
}
//As there is a big chance that the last batch will have less than 10 products, push them as well to wooCommerce as a last batch
$data = [
'create' => $productsForWoo,
];
$wooCommerceClient->post('products/batch', $data);
}
I will deeply appreciate any input related to my issue. Thank you!
To get POST and PUT, you'll need to implement the methods (verbs).
You can use:
$verb = $_SERVER['REQUEST_METHOD'];
echo $verb;
... to test what HTTP request method (PUT vs GET) you're using.
Here's a SO article regarding PHP methods which I have bookmarked and often use when troubleshooting this. The code provided there is pretty clean and a great place to start.

Pull Payments details from Stripe API

I am creating a WordPress plugin, and trying to pull payments details from stripe API done via third party. I have tried everything mention in this code but I am not able to pull data of Payments from stripe.
I need to fetch payment details from stripe API
https://stripe.com/docs/api/transfers?lang=php
I have tried payouts \Stripe\Payout::all() but getting empty object
Here is my code:
namespace wpmember;
class WPMember
{
public function __construct()
{
add_action( 'admin_menu', array($this, 'wpdocs_register_my_custom_menu_page') );
}
public function wpdocs_register_my_custom_menu_page()
{
add_menu_page(
'Custom Menu Title',
'custom menu',
'manage_options',
'custom-menu',
$this->connect_stripe()
);
}
public function connect_stripe()
{
require ( PLUGIN_DIR . 'vendor/autoload.php');
//echo PLUGIN_DIR . 'vendor/autoload.php';
\Stripe\Stripe::setApiKey("xxxx");
\Stripe\Stripe::setApiKey("xxxx");
$customers = \Stripe\Customer::all(["limit" => 3]);
$products = \Stripe\Product::all(["limit" => 3]);
$subscriptions = \Stripe\Subscription::all(['limit'=>3]);
$orders = \Stripe\Order::all(["limit" => 3]);
$allpayouts = \Stripe\Payout::all(["limit" => 3]);
$paymentIntent = \Stripe\PaymentIntent::all(["limit" => 3]);
//$payout = \Stripe\Payout::retrieve($allpayouts->data[0]->id);
$invoice = \Stripe\Invoice::all(["limit" => 3]);
echo "<pre>";
print_r($subscriptions);
echo "</pre>";
}
}
You seem to be fetching Payouts which are according to the Stripe API reference are the transfers from your Stripe account to your bank account. Which do not seem to be Charges object you showed on your screenshot or Transfer objects that could be fetched with \Stripe\Transfer::all().
Here you can get the payment details:
require ( PLUGIN_DIR . 'vendor/autoload.php');
\Stripe\Stripe::setApiKey("pk_test_VNbKcUGTqFIlyfIwFgizNx8h");
\Stripe\Stripe::setApiKey("sk_test_7VbuCbiZsDZjDHHlOtHeCqo7");
$charges = \Stripe\Charge::all(["limit" => 3]);
print_r($charges);

Prestashop module for product features

I'm working on a prestashop custom module.
This module will have to show some product features, let the admin modify its values and then save them in a custom tab.
So, showing features in a custom tab haven't been so difficult:
public function hookDisplayAdminProductsExtra($params) {
$id_lang = $this->context->language->id;
$features = FeatureCore::getFeatures($id_lang);
$values = [];
foreach ($features as $feature) {
array_push($values, $feature['value']);
}
$this->context->smarty->assign(array(
'features' => $features,
'values' => $values
));
if(!empty($sampleObj) && isset($sampleObj->id)){
$this->context->smarty->assign(array(
'custom_text_area' => $sampleObj->textarea
));
}
return $this->display(__FILE__, 'views/admin/sample.tpl');
}
The only thing which I can't get is features' default values in order to put them into a select.
After that what I want to do is to be able to save every value on product save.
So I wrote this hook:
public function hookActionProductUpdate($params) {
$id_product = $params['id_product'];
$product = new Product($id_product);
var_dump($product);
$all_tpl_vars = $smarty->getTemplateVars();
print_r($all_tpl_vars);
die("hello");
}
but when I press the save button nothing happens, and nothing will be shown on screen.
This is my first prestashop module, all the hooks has been registered in the module constructor.
Thanks to all.

Search Shipping Item in Netsuite API

I have this existing code snippet that searches list of records specified by its RecordType (e.g. InventoryItem, SalesOrder).
$request = new GetRequest();
$request->baseRef = new RecordRef();
$request->baseRef->type = $type; //Record Type
$request->baseRef->internalId = $internalId; //Internal ID of record
$getResponse = $service->get($request);
if ( ! $getResponse->readResponse->status->isSuccess) {
return 'ERROR';
} else {
return $getResponse->readResponse->record;
}
However, it seems that there's no Shipping Item in the list in RecordType although I can pass an internal ID. My goal here was to get the shipping item details to be used in my computation for creating a sales order (needs to be displayed before submitting).
Will there be a different approach in getting the shipping item record? How?
Shipping Item record is not yet supported in Suitetalk. As an alternate solution you can create a RESTlet instead to get the Shipping Item.
I can now successfully retrieve Shipping items via RESTlets. I uploaded this first as new in the File Cabinet, then added it as a new script. NetSuite does not allow direct upload of script file when creating a new script.
// get_record.js
function get_record(datain)
{
var record = nlapiLoadRecord(datain.recordType, datain.id);
return record;
}
Then used guzzle http library to call the RESTlet.
$url = "https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl";
$client = new GuzzleHttp\Client();
$authorization = [
'NLAuth nlauth_account='.getenv('NETSUITE_ACCOUNT'),
'nlauth_email='.getenv('NETSUITE_EMAIL'),
'nlauth_signature='.getenv('NETSUITE_PASSWORD'),
'nlauth_role='.getenv('NETSUITE_ROLE')
];
$response = $client->request('GET', $url, [
'headers' => [
'Authorization' => implode(',', $authorization),
'Content-Type' => 'application/json'
],
'query' => [
'script' => '343', //script id
'deploy' => '1',
'recordType' => 'ShipItem',
'id' => '5905' // example of internal id of desired shipping item
]
]);
return json_decode($response->getBody());

Categories