MAJ Module Paypal vers 5.1.1 (comme en prod)

This commit is contained in:
2020-02-14 19:42:46 +01:00
parent 188923c03c
commit f00f4207dd
137 changed files with 12999 additions and 5036 deletions

View File

@@ -18,15 +18,41 @@
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @author 2007-2019 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright PayPal
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
use PaypalPPBTlib\AbstractMethod;
abstract class AbstractMethodPaypal extends AbstractMethod
{
public static function load($method = null)
{
if ($method == null) {
$countryDefault = new \Country((int)\Configuration::get('PS_COUNTRY_DEFAULT'));
switch ($countryDefault->iso_code) {
case "DE":
$method = "PPP";
break;
case "BR":
$method = "MB";
break;
case "MX":
$method = "MB";
break;
default:
$method = "EC";
}
}
if (preg_match('/^[a-zA-Z0-9_-]+$/', $method) && file_exists(_PS_MODULE_DIR_.'paypal/classes/Method'.$method.'.php')) {
include_once _PS_MODULE_DIR_.'paypal/classes/Method'.$method.'.php';
$method_class = 'Method'.$method;
return new $method_class();
}
}
}

View File

@@ -20,10 +20,12 @@
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
require_once 'AbstractMethodPaypal.php';
use PayPal\CoreComponentTypes\BasicAmountType;
use PayPal\EBLBaseComponents\DoExpressCheckoutPaymentRequestDetailsType;
use PayPal\EBLBaseComponents\AddressType;
@@ -46,6 +48,8 @@ use PayPal\PayPalAPI\GetExpressCheckoutDetailsReq;
use PayPal\Service\PayPalAPIInterfaceServiceService;
use PaypalAddons\classes\PaypalException;
use PaypalPPBTlib\Extensions\ProcessLogger\ProcessLoggerHandler;
use PayPal\PayPalAPI\GetBalanceReq;
use PayPal\PayPalAPI\GetBalanceRequestType;
/**
* Class MethodEC.
@@ -80,6 +84,13 @@ class MethodEC extends AbstractMethodPaypal
protected $payment_method = 'PayPal';
public $errors = array();
public $advancedFormParametres = array(
'paypal_os_accepted_two',
'paypal_os_waiting_validation'
);
/**
* @param $values array replace for tools::getValues()
*/
@@ -97,266 +108,23 @@ class MethodEC extends AbstractMethodPaypal
*/
public function getConfig(\PayPal $module)
{
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
$params = array('inputs' => array(
array(
'type' => 'select',
'label' => $module->l('Payment action', get_class($this)),
'name' => 'paypal_intent',
'desc' => $module->l('', get_class($this)),
'hint' => $module->l('Sale: the money moves instantly from the buyer\'s account to the seller\'s account at the time of payment. Authorization/capture: The authorized mode is a deferred mode of payment that requires the funds to be collected manually when you want to transfer the money. This mode is used if you want to ensure that you have the merchandise before depositing the money, for example. Be careful, you have 29 days to collect the funds.', get_class($this)),
'options' => array(
'query' => array(
array(
'id' => 'sale',
'name' => $module->l('Sale', get_class($this))
),
array(
'id' => 'authorization',
'name' => $module->l('Authorize', get_class($this))
)
),
'id' => 'id',
'name' => 'name'
),
),
array(
'type' => 'switch',
'label' => $module->l('Show PayPal benefits to your customers', get_class($this)),
'name' => 'paypal_show_advantage',
'is_bool' => true,
'hint' => $module->l('You can increase your conversion rate by presenting PayPal benefits to your customers on payment methods selection page.', get_class($this)),
'values' => array(
array(
'id' => 'paypal_show_advantage_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_show_advantage_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
),
array(
'type' => 'switch',
'label' => $module->l('PayPal In-Context', get_class($this)),
'name' => 'paypal_ec_in_context',
'is_bool' => true,
'hint' => $module->l('PayPal opens in a pop-up window, allowing your buyers to finalize their payment without leaving your website. Optimized, modern and reassuring experience which benefits from the same security standards than during a redirection to the PayPal website.', get_class($this)),
'values' => array(
array(
'id' => 'paypal_ec_in_context_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_ec_in_context_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
),
array(
'type' => 'text',
'label' => $module->l('Brand name', get_class($this)),
'name' => 'config_brand',
'placeholder' => $module->l('Leave it empty to use your Shop name', get_class($this)),
'hint' => $module->l('A label that overrides the business name in the PayPal account on the PayPal pages.', get_class($this)),
),
array(
'type' => 'file',
'label' => $module->l('Shop logo field', get_class($this)),
'name' => 'config_logo',
'display_image' => true,
'image' => file_exists(Configuration::get('PAYPAL_CONFIG_LOGO'))?'<img src="'.Context::getContext()->link->getBaseLink().'modules/paypal/views/img/p_logo_'.Context::getContext()->shop->id.'.png" class="img img-thumbnail" />':'',
'delete_url' => $module->module_link.'&deleteLogoPp=1',
'hint' => $module->l('An image must be stored on a secure (https) server. Use a valid graphics format, such as .gif, .jpg, or .png. Limit the image to 190 pixels wide by 60 pixels high. PayPal crops images that are larger. This logo will replace brand name at the top of the cart review area.', get_class($this)),
),
));
$params['fields_value'] = array(
'paypal_intent' => Configuration::get('PAYPAL_API_INTENT'),
'paypal_show_advantage' => Configuration::get('PAYPAL_API_ADVANTAGES'),
'paypal_ec_in_context' => Configuration::get('PAYPAL_EXPRESS_CHECKOUT_IN_CONTEXT'),
'paypal_ec_merchant_id' => Configuration::get('PAYPAL_MERCHANT_ID_'.$mode),
'config_brand' => Configuration::get('PAYPAL_CONFIG_BRAND'),
'config_logo' => Configuration::get('PAYPAL_CONFIG_LOGO'),
);
}
$country_default = Country::getIsoById(Configuration::get('PS_COUNTRY_DEFAULT'));
if (!in_array($country_default, $module->bt_countries)) {
$params['inputs'][] = array(
'type' => 'switch',
'label' => $module->l('Accept credit and debit card payment', get_class($this)),
'name' => 'paypal_card',
'is_bool' => true,
'hint' => $module->l('Your customers can pay with debit and credit cards as well as local payment systems whether or not they use PayPal', get_class($this)),
'values' => array(
array(
'id' => 'paypal_card_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_card_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
);
$params['fields_value']['paypal_card'] = Configuration::get('PAYPAL_API_CARD');
public function logOut($sandbox = null)
{
if ($sandbox == null) {
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
} else {
$mode = (int)$sandbox ? 'SANDBOX' : 'LIVE';
}
$context = Context::getContext();
$context->smarty->assign(array(
'access_token_sandbox' => Configuration::get('PAYPAL_SANDBOX_ACCESS'),
'access_token_live' => Configuration::get('PAYPAL_LIVE_ACCESS'),
'ec_card_active' => Configuration::get('PAYPAL_API_CARD'),
'ec_paypal_active' => !Configuration::get('PAYPAL_API_CARD') || (Configuration::get('PAYPAL_EXPRESS_CHECKOUT') && Context::getContext()->country->iso_code == 'DE'),
'need_rounding' => ((Configuration::get('PS_ROUND_TYPE') == Order::ROUND_ITEM) && (Configuration::get('PS_PRICE_ROUND_MODE') == PS_ROUND_HALF_UP) ? 0 : 1),
'ec_active' => Configuration::get('PAYPAL_EXPRESS_CHECKOUT'),
));
$context->smarty->assign(array(
'api_username' => Configuration::get('PAYPAL_USERNAME_'.$mode),
'api_password' => Configuration::get('PAYPAL_PSWD_'.$mode),
'api_signature' => Configuration::get('PAYPAL_SIGNATURE_'.$mode),
'merchant_id' => Configuration::get('PAYPAL_MERCHANT_ID_'.$mode),
'mode' => $mode
));
$params['form'] = $this->getApiUserName($module);
$params['short_cut'] = $this->createShortcutForm($module);
return $params;
}
public function getApiUserName($module)
{
$fields_form = array();
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $module->l('Api user name', get_class($this)),
'icon' => 'icon-cogs',
),
);
$apiUserName = (Configuration::get('PAYPAL_SANDBOX')?Configuration::get('PAYPAL_USERNAME_SANDBOX'):Configuration::get('PAYPAL_USERNAME_LIVE'));
$fields_form[0]['form']['input'] = array(
array(
'type' => 'text',
'label' => $module->l('API user name', get_class($this)),
'name'=>'api_user_name',
'disabled'=>'disabled'
)
);
$helper = new HelperForm();
$helper->module = $module;
$helper->name_controller = 'form_api_username';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$module->name;
$helper->title = $module->displayName;
$helper->show_toolbar = false;
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
$helper->tpl_vars = array(
'fields_value' => array('api_user_name'=>$apiUserName),
'id_language' => Context::getContext()->language->id,
'back_url' => $module->module_link.'#paypal_params'
);
return $helper->generateForm($fields_form);
}
public function createShortcutForm($module)
{
$fields_form = array();
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $module->l('PayPal Express Shortcut', get_class($this)),
'icon' => 'icon-cogs',
),
'submit' => array(
'title' => $module->l('Save', get_class($this)),
'class' => 'btn btn-default pull-right button',
),
);
$fields_form[0]['form']['input'] = array(
array(
'type' => 'html',
'name' => 'paypal_desc_shortcut',
'html_content' => $module->l('The PayPal shortcut is displayed directly in the cart or on your product pages, allowing a faster checkout experience for your buyers. It requires fewer pages, clicks and seconds in order to finalize the payment. PayPal provides you with the clients billing and shipping information so that you dont have to collect it yourself.', get_class($this)),
),
array(
'type' => 'switch',
'label' => $module->l('Display the shortcut on product pages', get_class($this)),
'name' => 'paypal_show_shortcut',
'is_bool' => true,
'hint' => $module->l('Recommended for mono-product websites.', get_class($this)),
'values' => array(
array(
'id' => 'paypal_show_shortcut_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_show_shortcut_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
),
array(
'type' => 'switch',
'label' => $module->l('Display shortcut in the cart', get_class($this)),
'name' => 'paypal_show_shortcut_cart',
'is_bool' => true,
'hint' => $module->l('Recommended for multi-products websites.', get_class($this)),
'values' => array(
array(
'id' => 'paypal_show_shortcut_cart_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_show_shortcut_cart_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
),
);
$fields_value = array(
'paypal_show_shortcut' => Configuration::get('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT'),
'paypal_show_shortcut_cart' => Configuration::get('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT_CART'),
);
$helper = new HelperForm();
$helper->module = $module;
$helper->name_controller = 'form_shortcut';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$module->name;
$helper->title = $module->displayName;
$helper->show_toolbar = false;
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->default_form_language = $default_lang;
$helper->submit_action = 'submit_shortcut';
$helper->allow_employee_form_lang = $default_lang;
$helper->tpl_vars = array(
'fields_value' => $fields_value,
'id_language' => Context::getContext()->language->id,
'back_url' => $module->module_link.'#paypal_params'
);
return $helper->generateForm($fields_form);
Configuration::updateValue('PAYPAL_USERNAME_'.$mode, '');
Configuration::updateValue('PAYPAL_PSWD_'.$mode, '');
Configuration::updateValue('PAYPAL_SIGNATURE_'.$mode, '');
Configuration::updateValue('PAYPAL_'.$mode.'_ACCESS', 0);
Configuration::updateValue('PAYPAL_MERCHANT_ID_'.$mode, '');
Configuration::updateValue('PAYPAL_API_CARD', 0);
Configuration::updateValue('PAYPAL_CONNECTION_EC_CONFIGURED', 0);
}
/**
@@ -366,8 +134,11 @@ class MethodEC extends AbstractMethodPaypal
{
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
$paypal = Module::getInstanceByName($this->name);
if (isset($params['api_username']) && isset($params['api_password']) && isset($params['api_signature'])) {
Configuration::updateValue('PAYPAL_METHOD', 'EC');
if (isset($params['api_username']) &&
isset($params['api_password']) &&
isset($params['api_signature']) &&
(isset($params['id_shop']) == false || $params['id_shop'] == Context::getContext()->shop->id)) {
Configuration::updateValue('PAYPAL_EXPRESS_CHECKOUT', 1);
Configuration::updateValue('PAYPAL_USERNAME_'.$mode, $params['api_username']);
Configuration::updateValue('PAYPAL_PSWD_'.$mode, $params['api_password']);
@@ -375,47 +146,14 @@ class MethodEC extends AbstractMethodPaypal
Configuration::updateValue('PAYPAL_'.$mode.'_ACCESS', 1);
Configuration::updateValue('PAYPAL_MERCHANT_ID_'.$mode, $params['merchant_id']);
Configuration::updateValue('PAYPAL_EXPRESS_CHECKOUT_IN_CONTEXT', 1);
Configuration::updateValue('PAYPAL_API_CARD', $params['with_card']);
Tools::redirect($paypal->module_link);
}
if (Tools::isSubmit('submit_shortcut')) {
Configuration::updateValue('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT', $params['paypal_show_shortcut']);
Configuration::updateValue('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT_CART', $params['paypal_show_shortcut_cart']);
}
if (Tools::isSubmit('paypal_config')) {
Configuration::updateValue('PAYPAL_API_INTENT', $params['paypal_intent']);
Configuration::updateValue('PAYPAL_API_ADVANTAGES', $params['paypal_show_advantage']);
Configuration::updateValue('PAYPAL_EXPRESS_CHECKOUT_IN_CONTEXT', $params['paypal_ec_in_context']);
Configuration::updateValue('PAYPAL_CONFIG_BRAND', $params['config_brand']);
if (isset($_FILES['config_logo']['tmp_name']) && $_FILES['config_logo']['tmp_name'] != '') {
if (!in_array($_FILES['config_logo']['type'], array('image/gif', 'image/png', 'image/jpeg'))) {
$paypal->errors .= $paypal->displayError($paypal->l('Use a valid graphics format, such as .gif, .jpg, or .png.', get_class($this)));
return;
}
$size = getimagesize($_FILES['config_logo']['tmp_name']);
if ($size[0] > 190 || $size[1] > 60) {
$paypal->errors .= $paypal->displayError($paypal->l('Limit the image to 190 pixels wide by 60 pixels high.', get_class($this)));
return;
}
if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) ||
!move_uploaded_file($_FILES['config_logo']['tmp_name'], $tmpName)) {
$paypal->errors .= $paypal->displayError($paypal->l('An error occurred while copying the image.', get_class($this)));
}
if (!ImageManager::resize($tmpName, _PS_MODULE_DIR_.'paypal/views/img/p_logo_'.Context::getContext()->shop->id.'.png')) {
$paypal->errors .= $paypal->displayError($paypal->l('An error occurred while copying the image.', get_class($this)));
}
Configuration::updateValue('PAYPAL_CONFIG_LOGO', _PS_MODULE_DIR_.'paypal/views/img/p_logo_'.Context::getContext()->shop->id.'.png');
}
$this->checkCredentials();
return;
}
if (Tools::getValue('deleteLogoPp')) {
unlink(Configuration::get('PAYPAL_CONFIG_LOGO'));
Configuration::updateValue('PAYPAL_CONFIG_LOGO', '');
}
$country_default = Country::getIsoById(Configuration::get('PS_COUNTRY_DEFAULT'));
if (!in_array($country_default, $paypal->bt_countries)) {
if (!in_array($country_default, $paypal->countriesApiCartUnavailable)) {
if (Tools::isSubmit('paypal_config')) {
Configuration::updateValue('PAYPAL_API_CARD', $params['paypal_card']);
}
@@ -429,20 +167,11 @@ class MethodEC extends AbstractMethodPaypal
if (isset($params['method'])) {
Configuration::updateValue('PAYPAL_API_CARD', $params['with_card']);
if ((isset($params['modify']) && $params['modify']) || (Configuration::get('PAYPAL_METHOD') != $params['method'])) {
if ((isset($params['modify']) && $params['modify']) || $this->isConfigured()) {
$response = $paypal->getPartnerInfo();
Tools::redirectLink($response);
}
}
if ($mode == 'SANDBOX' && (!Configuration::get('PAYPAL_USERNAME_'.$mode) || !Configuration::get('PAYPAL_PSWD_'.$mode)
|| !Configuration::get('PAYPAL_SIGNATURE_'.$mode))) {
$paypal->errors .= $paypal->displayError($paypal->l('You are trying to switch to sandbox account. You should use your test credentials. Please go to the "Products" tab and click on "Modify\' for activating the sandbox version of the selected product.', get_class($this)));
}
if ($mode == 'LIVE' && (!Configuration::get('PAYPAL_USERNAME_'.$mode) || !Configuration::get('PAYPAL_PSWD_'.$mode)
|| !Configuration::get('PAYPAL_SIGNATURE_'.$mode))) {
$paypal->errors .= $paypal->displayError($paypal->l('You are trying to switch to production account. You should use your production credentials. Please go to the "Products" tab and click on "Modify\' for activating the production version of the selected product.', get_class($this)));
}
}
/**
@@ -452,6 +181,9 @@ class MethodEC extends AbstractMethodPaypal
public function init()
{
// details about payment
if ($this->isConfigured() == false) {
throw new PaypalException(0, 'Invalid configuration', 'Invalid configuration. Please check your configuration');
}
$this->_paymentDetails = new PaymentDetailsType();
$this->_paymentDetails->ButtonSource = 'PrestaShop_Cart_'.(getenv('PLATEFORM') == 'PSREADY' ? 'Ready_':'').'EC';
@@ -472,6 +204,7 @@ class MethodEC extends AbstractMethodPaypal
$setECReqDetails->AddressOverride = 1;
$setECReqDetails->ReqConfirmShipping = 0;
$setECReqDetails->LandingPage = ($this->credit_card ? 'Billing' : 'Login');
if ($this->short_cut) {
$setECReqDetails->ReturnURL = Context::getContext()->link->getModuleLink($this->name, 'ecScOrder', array(), true);
@@ -579,8 +312,9 @@ class MethodEC extends AbstractMethodPaypal
// It's needed to take a percentage of the order amount, taking into account the others discounts
if ((int)$discount['reduction_percent'] > 0) {
$discount['value_real'] = $order_total_with_reduction * ($discount['value_real'] / $order_total);
$order_total_with_reduction -= $discount['value_real'];
} else {
}
if ((int)$discount['free_shipping'] == false) {
$order_total_with_reduction -= $discount['value_real'];
}
@@ -588,11 +322,9 @@ class MethodEC extends AbstractMethodPaypal
$itemDetails = new PaymentDetailsItemType();
$itemDetails->Name = $discount['name'];
$itemDetails->Amount = new BasicAmountType($currency, $discount['value_real']);
$itemDetails->Tax = new BasicAmountType($currency, $this->formatPrice($discount['tax']));
$itemDetails->Quantity = 1;
$this->_paymentDetails->PaymentDetailsItem[] = $itemDetails;
$this->_itemTotalValue += $discount['value_real'];
$this->_taxTotalValue += $this->formatPrice($discount['tax']);
}
}
}
@@ -689,7 +421,7 @@ class MethodEC extends AbstractMethodPaypal
$url = '/websc&cmd=_express-checkout';
}
if (($method == 'SetExpressCheckout') && ($this->type == 'payment_cart')) {
if (($method == 'SetExpressCheckout') && $this->credit_card) {
$url .= '&useraction=commit';
}
$paypal = Module::getInstanceByName($this->name);
@@ -723,7 +455,6 @@ class MethodEC extends AbstractMethodPaypal
$params['acct1.UserName'] = Configuration::get('PAYPAL_USERNAME_LIVE');
$params['acct1.Password'] = Configuration::get('PAYPAL_PSWD_LIVE');
$params['acct1.Signature'] = Configuration::get('PAYPAL_SIGNATURE_LIVE');
$params['acct1.Signature'] = Configuration::get('PAYPAL_SIGNATURE_LIVE');
$params['mode'] = $mode_order ? 'sandbox' : 'live';
$params['log.LogEnabled'] = false;
break;
@@ -739,15 +470,43 @@ class MethodEC extends AbstractMethodPaypal
return $params;
}
/**
* @param $cart Cart
* @return string additional payment information
*/
public function getCustomFieldInformation(Cart $cart)
{
$module = Module::getInstanceByName($this->name);
$return = $module->l('Cart ID: ', get_class($this)) . $cart->id . '.';
if (Shop::isFeatureActive()) {
$shop = new Shop($cart->id_shop, $cart->id_lang);
if (Validate::isLoadedObject($shop)) {
$return .= $module->l('Shop name: ', get_class($this)) . $shop->name;
}
}
return $return;
}
/**
* @see AbstractMethodPaypal::validation()
*/
public function validation()
{
$context = Context::getContext();
$cart = $context->cart;
$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
throw new Exception('Customer is not loaded object');
}
$this->_paymentDetails = new PaymentDetailsType();
$this->_paymentDetails->ButtonSource = 'PrestaShop_Cart_'.(getenv('PLATEFORM') == 'PSREADY' ? 'Ready_':'').'EC';
$this->_paymentDetails->Custom = $this->getCustomFieldInformation($cart);
if (!Context::getContext()->cart->isVirtualCart()) {
$address = $this->_getShippingAddress();
@@ -770,31 +529,50 @@ class MethodEC extends AbstractMethodPaypal
$DoECReq->DoExpressCheckoutPaymentRequest = $DoECRequest;
$paypalService = new PayPalAPIInterfaceServiceService($this->_getCredentialsInfo());
$exec_payment = $paypalService->DoExpressCheckoutPayment($DoECReq);
if (isset($exec_payment->Errors)) {
if ($exec_payment->Errors[0]->ErrorCode == 10486) {
$this->token = $this->payment_token;
}
throw new PaypalException($exec_payment->Errors[0]->ErrorCode, $exec_payment->Errors[0]->ShortMessage, $exec_payment->Errors[0]->LongMessage);
}
$this->setDetailsTransaction($exec_payment->DoExpressCheckoutPaymentResponseDetails);
$cart = $context->cart;
$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
throw new Exception('Customer is not loaded object');
}
$currency = $context->currency;
$payment_info = $exec_payment->DoExpressCheckoutPaymentResponseDetails->PaymentInfo[0];
$total = $payment_info->GrossAmount->value;
$paypal = Module::getInstanceByName($this->name);
if (Configuration::get('PAYPAL_API_INTENT') == "sale") {
$order_state = Configuration::get('PS_OS_PAYMENT');
} else {
$order_state = Configuration::get('PAYPAL_OS_WAITING');
}
$order_state = $this->getOrderStatus();
$paypal->validateOrder($cart->id, $order_state, $total, $this->getPaymentMethod(), null, $this->getDetailsTransaction(), (int)$currency->id, false, $customer->secure_key);
}
/**
* @return int id of the order status
**/
public function getOrderStatus()
{
if ((int)Configuration::get('PAYPAL_CUSTOMIZE_ORDER_STATUS')) {
if (Configuration::get('PAYPAL_API_INTENT') == "sale") {
$orderStatus = (int)Configuration::get('PAYPAL_OS_WAITING_VALIDATION');
} else {
$orderStatus = (int)Configuration::get('PAYPAL_OS_WAITING');
}
} else {
if (Configuration::get('PAYPAL_API_INTENT') == "sale") {
$orderStatus = (int)Configuration::get('PS_OS_PAYMENT');
} else {
$orderStatus = (int)Configuration::get('PAYPAL_OS_WAITING');
}
}
return $orderStatus;
}
public function setDetailsTransaction($transaction)
{
$payment_info = $transaction->PaymentInfo[0];
@@ -1001,7 +779,7 @@ class MethodEC extends AbstractMethodPaypal
* @param $page_source
* @return mixed
*/
public function renderExpressCheckoutShortCut(&$context, $type, $page_source)
public function renderExpressCheckoutShortCut(Context &$context, $type, $page_source)
{
$lang = $context->language->iso_code;
$environment = (Configuration::get('PAYPAL_SANDBOX')?'sandbox':'live');
@@ -1020,15 +798,13 @@ class MethodEC extends AbstractMethodPaypal
'merchant_id' => Configuration::get('PAYPAL_MERCHANT_ID_'.Tools::strtoupper($environment)),
'environment' => $environment,
));
if ($page_source == 'product') {
$context->smarty->assign(array(
'es_cs_product_attribute' => Tools::getValue('id_product_attribute'),
'es_cs_product_attribute' => Tools::getValue('id_product_attribute')
));
return $context->smarty->fetch('module:paypal/views/templates/hook/EC_shortcut.tpl');
} elseif ($page_source == 'cart') {
return $context->smarty->fetch('module:paypal/views/templates/hook/cart_shortcut.tpl');
}
$context->smarty->assign('source_page', $page_source);
return $context->smarty->fetch('module:paypal/views/templates/hook/shortcut.tpl');
}
/**
@@ -1051,25 +827,115 @@ class MethodEC extends AbstractMethodPaypal
/**
* @see AbstractMethodPaypal::getLinkToTransaction()
*/
public function getLinkToTransaction($id_transaction, $sandbox)
public function getLinkToTransaction($log)
{
if ($sandbox) {
if ($log->sandbox) {
$url = 'https://www.sandbox.paypal.com/activity/payment/';
} else {
$url = 'https://www.paypal.com/activity/payment/';
}
return $url . $id_transaction;
return $url . $log->id_transaction;
}
/**
* @return bool
*/
public function isConfigured()
public function isConfigured($mode = null)
{
if (Configuration::get('PAYPAL_SANDBOX')) {
return (bool)Configuration::get('PAYPAL_MERCHANT_ID_SANDBOX');
return (int)Configuration::get('PAYPAL_CONNECTION_EC_CONFIGURED') === 1;
}
public function checkCredentials($mode = null)
{
$paypalService = new PayPalAPIInterfaceServiceService($this->_getCredentialsInfo($mode));
$getBalanceReq = new GetBalanceReq();
$getBalanceReq->GetBalanceRequest = new GetBalanceRequestType();
$module = Module::getInstanceByName('paypal');
try {
$response = $paypalService->GetBalance($getBalanceReq);
} catch (Exception $e) {
$this->errors[] = $module->l('An error occurred while creating your web experience. Check your credentials.', get_class($this));
Configuration::updateValue('PAYPAL_CONNECTION_EC_CONFIGURED', 0);
return;
}
if ($response->Ack == 'Success') {
Configuration::updateValue('PAYPAL_CONNECTION_EC_CONFIGURED', 1);
} else {
return (bool)Configuration::get('PAYPAL_MERCHANT_ID_LIVE');
$this->errors[] = $module->l('An error occurred while creating your web experience. Check your credentials.', get_class($this));
Configuration::updateValue('PAYPAL_CONNECTION_EC_CONFIGURED', 0);
}
}
public function getTplVars()
{
$urlParameters = array(
'paypal_set_config' => 1,
'method' => 'EC',
'with_card' => 0,
'modify' => 1
);
$context = Context::getContext();
$countryDefault = new \Country((int)\Configuration::get('PS_COUNTRY_DEFAULT'), $context->language->id);
$tpl_vars = array(
'accountConfigured' => $this->isConfigured(),
'urlOnboarding' => $context->link->getAdminLink('AdminPayPalSetup', true, null, $urlParameters),
'country_iso' => $countryDefault->iso_code,
'idShop' => Context::getContext()->shop->id,
);
if ((int)Configuration::get('PAYPAL_SANDBOX')) {
$tpl_vars['paypal_api_user_name'] = Configuration::get('PAYPAL_USERNAME_SANDBOX');
$tpl_vars['paypal_pswd'] = Configuration::get('PAYPAL_PSWD_SANDBOX');
$tpl_vars['paypal_signature'] = Configuration::get('PAYPAL_SIGNATURE_SANDBOX');
$tpl_vars['paypal_merchant_id'] = Configuration::get('PAYPAL_MERCHANT_ID_SANDBOX');
$tpl_vars['mode'] = 'SANDBOX';
} else {
$tpl_vars['paypal_api_user_name'] = Configuration::get('PAYPAL_USERNAME_LIVE');
$tpl_vars['paypal_pswd'] = Configuration::get('PAYPAL_PSWD_LIVE');
$tpl_vars['paypal_signature'] = Configuration::get('PAYPAL_SIGNATURE_LIVE');
$tpl_vars['paypal_merchant_id'] = Configuration::get('PAYPAL_MERCHANT_ID_LIVE');
$tpl_vars['mode'] = 'LIVE';
}
return $tpl_vars;
}
public function getAdvancedFormInputs()
{
$inputs = array();
$module = Module::getInstanceByName($this->name);
$orderStatuses = $module->getOrderStatuses();
if (Configuration::get('PAYPAL_API_INTENT') == 'authorization') {
$inputs[] = array(
'type' => 'select',
'label' => $module->l('Payment authorized and waiting for validation by admin', get_class($this)),
'name' => 'paypal_os_waiting_validation',
'hint' => $module->l('You are currently using the Authorize mode. It means that you separate the payment authorization from the capture of the authorized payment. By default the orders will be created in the "Waiting for PayPal payment" but you can customize it if needed.', get_class($this)),
'desc' => $module->l('Default status : Waiting for PayPal payment', get_class($this)),
'options' => array(
'query' => $orderStatuses,
'id' => 'id',
'name' => 'name'
)
);
} else {
$inputs[] = array(
'type' => 'select',
'label' => $module->l('Payment accepted and transaction completed', get_class($this)),
'name' => 'paypal_os_accepted_two',
'hint' => $module->l('You are currently using the Sale mode (the authorization and capture occur at the same time as the sale). So the payement is accepted instantly and the new order is created in the "Payment accepted" status. You can customize the status for orders with completed transactions. Ex : you can create an additional status "Payment accepted via PayPal" and set it as the default status.', get_class($this)),
'desc' => $module->l('Default status : Payment accepted', get_class($this)),
'options' => array(
'query' => $orderStatuses,
'id' => 'id',
'name' => 'name'
)
);
}
return $inputs;
}
}

View File

@@ -18,12 +18,15 @@
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @author 2007-2019 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright PayPal
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
require_once 'AbstractMethodPaypal.php';
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Api\Amount;
@@ -59,6 +62,8 @@ class MethodPPP extends AbstractMethodPaypal
private $_amount;
public $errors = array();
/** @var boolean shortcut payment from product or cart page*/
public $short_cut;
@@ -70,6 +75,10 @@ class MethodPPP extends AbstractMethodPaypal
protected $payment_method = 'PayPal';
public $advancedFormParametres = array(
'paypal_os_accepted_two'
);
/**
* @param $values array replace for tools::getValues()
*/
@@ -82,241 +91,27 @@ class MethodPPP extends AbstractMethodPaypal
}
}
public function logOut($sandbox = null)
{
if ($sandbox == null) {
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
} else {
$mode = (int)$sandbox ? 'SANDBOX' : 'LIVE';
}
Configuration::updateValue('PAYPAL_' . $mode . '_CLIENTID', '');
Configuration::updateValue('PAYPAL_' . $mode . '_SECRET', '');
}
/**
* @see AbstractMethodPaypal::setConfig()
*/
public function setConfig($params)
{
$paypal = Module::getInstanceByName($this->name);
if (Tools::isSubmit('paypal_config')) {
Configuration::updateValue('PAYPAL_API_ADVANTAGES', $params['paypal_show_advantage']);
Configuration::updateValue('PAYPAL_PPP_CONFIG_TITLE', $params['ppp_config_title']);
Configuration::updateValue('PAYPAL_PPP_CONFIG_BRAND', $params['ppp_config_brand']);
if (isset($_FILES['ppp_config_logo']['tmp_name']) && $_FILES['ppp_config_logo']['tmp_name'] != '') {
if (!in_array($_FILES['ppp_config_logo']['type'], array('image/gif', 'image/png', 'image/jpeg'))) {
$paypal->errors .= $paypal->displayError($paypal->l('Use a valid graphics format, such as .gif, .jpg, or .png.', get_class($this)));
return;
}
$size = getimagesize($_FILES['ppp_config_logo']['tmp_name']);
if ($size[0] > 190 || $size[1] > 60) {
$paypal->errors .= $paypal->displayError($paypal->l('Limit the image to 190 pixels wide by 60 pixels high.', get_class($this)));
return;
}
if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) ||
!move_uploaded_file($_FILES['ppp_config_logo']['tmp_name'], $tmpName)) {
$paypal->errors .= $paypal->displayError($paypal->l('An error occurred while copying the image.', get_class($this)));
}
if (!ImageManager::resize($tmpName, _PS_MODULE_DIR_.'paypal/views/img/ppp_logo'.Context::getContext()->shop->id.'.png')) {
$paypal->errors .= $paypal->displayError($paypal->l('An error occurred while copying the image.', get_class($this)));
}
Configuration::updateValue('PAYPAL_PPP_CONFIG_LOGO', _PS_MODULE_DIR_.'paypal/views/img/ppp_logo'.Context::getContext()->shop->id.'.png');
}
if ((Configuration::get('PAYPAL_SANDBOX') && Configuration::get('PAYPAL_SANDBOX_CLIENTID') && Configuration::get('PAYPAL_SANDBOX_SECRET'))
|| (!Configuration::get('PAYPAL_SANDBOX') && Configuration::get('PAYPAL_LIVE_CLIENTID') && Configuration::get('PAYPAL_LIVE_SECRET'))) {
$experience_web = $this->createWebExperience();
if ($experience_web) {
Configuration::updateValue('PAYPAL_PLUS_EXPERIENCE', $experience_web->id);
} else {
$paypal->errors .= $paypal->displayError($paypal->l('An error occurred while creating your web experience. Check your credentials.', get_class($this)));
}
}
}
if (Tools::isSubmit('submit_shortcut')) {
Configuration::updateValue('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT', $params['paypal_show_shortcut']);
Configuration::updateValue('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT_CART', $params['paypal_show_shortcut_cart']);
}
if (Tools::getValue('deleteLogoPp')) {
unlink(Configuration::get('PAYPAL_PPP_CONFIG_LOGO'));
Configuration::updateValue('PAYPAL_PPP_CONFIG_LOGO', '');
}
if (Tools::isSubmit('save_credentials')) {
$sandbox = Tools::getValue('sandbox');
$live = Tools::getValue('live');
if ($sandbox['client_id'] && $sandbox['secret'] && (!$live['client_id'] || !$live['secret'])) {
Configuration::updateValue('PAYPAL_SANDBOX', 1);
}
Configuration::updateValue('PAYPAL_SANDBOX_CLIENTID', $sandbox['client_id']);
Configuration::updateValue('PAYPAL_SANDBOX_SECRET', $sandbox['secret']);
Configuration::updateValue('PAYPAL_LIVE_CLIENTID', $live['client_id']);
Configuration::updateValue('PAYPAL_LIVE_SECRET', $live['secret']);
Configuration::updateValue('PAYPAL_METHOD', 'PPP');
Configuration::updateValue('PAYPAL_PLUS_ENABLED', 1);
if ((Configuration::get('PAYPAL_SANDBOX') && $sandbox['client_id'] && $sandbox['secret'])
|| (!Configuration::get('PAYPAL_SANDBOX') && $live['client_id'] && $live['secret'])) {
$experience_web = $this->createWebExperience();
if ($experience_web) {
Configuration::updateValue('PAYPAL_PLUS_EXPERIENCE', $experience_web->id);
} else {
$paypal->errors .= $paypal->displayError($paypal->l('An error occurred while creating your web experience. Check your credentials.', get_class($this)));
}
}
}
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
if ($mode == 'SANDBOX' && (!Configuration::get('PAYPAL_SANDBOX_CLIENTID') || !Configuration::get('PAYPAL_SANDBOX_SECRET'))) {
$paypal->errors .= $paypal->displayError($paypal->l('You are trying to switch to sandbox account. You should use your test credentials. Please go to the "Products" tab and click on "Modify\' for activating the sandbox version of the selected product.', get_class($this)));
}
if ($mode == 'LIVE' && (!Configuration::get('PAYPAL_LIVE_CLIENTID') || !Configuration::get('PAYPAL_LIVE_SECRET'))) {
$paypal->errors .= $paypal->displayError($paypal->l('You are trying to switch to production account. You should use your production credentials. Please go to the "Products" tab and click on "Modify\' for activating the production version of the selected product.', get_class($this)));
}
}
/**
* @see AbstractMethodPaypal::getConfig()
*/
public function getConfig(Paypal $module)
public function getConfig(Paypal $paypal)
{
/*$module->l('Test t');*/
$params = array('inputs' => array(
array(
'type' => 'text',
'label' => $module->l('Title', get_class($this)),
'name' => 'ppp_config_title',
'placeholder' => $module->l('Leave it empty to use default PayPal payment method title', get_class($this)),
),
array(
'type' => 'text',
'label' => $module->l('Brand name', get_class($this)),
'name' => 'ppp_config_brand',
'placeholder' => $module->l('Leave it empty to use your Shop name', get_class($this)),
'hint' => $module->l('A label that overrides the business name in the PayPal account on the PayPal pages.', get_class($this)),
),
array(
'type' => 'file',
'label' => $module->l('Shop logo field', get_class($this)),
'name' => 'ppp_config_logo',
'display_image' => true,
'delete_url' => $module->module_link.'&deleteLogoPp=1',
'hint' => $module->l('An image must be stored on a secure (https) server. Use a valid graphics format, such as .gif, .jpg, or .png. Limit the image to 190 pixels wide by 60 pixels high. PayPal crops images that are larger. This logo will replace brand name at the top of the cart review area.', get_class($this)),
'image' => file_exists(_PS_MODULE_DIR_.'paypal/views/img/ppp_logo'.Context::getContext()->shop->id.'.png')?'<img src="'.Context::getContext()->link->getBaseLink().'modules/paypal/views/img/ppp_logo'.Context::getContext()->shop->id.'.png" class="img img-thumbnail" />':''
),
array(
'type' => 'switch',
'label' => $module->l('Show PayPal benefits to your customers', get_class($this)),
'name' => 'paypal_show_advantage',
'desc' => $module->l('', get_class($this)),
'is_bool' => true,
'hint' => $module->l('You can increase your conversion rate by presenting PayPal benefits to your customers on payment methods selection page.', get_class($this)),
'values' => array(
array(
'id' => 'paypal_show_advantage_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_show_advantage_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
),
));
$params['fields_value'] = array(
'ppp_config_title' => Configuration::get('PAYPAL_PPP_CONFIG_TITLE'),
'ppp_config_brand' => Configuration::get('PAYPAL_PPP_CONFIG_BRAND'),
'ppp_config_logo' => Configuration::get('PAYPAL_PPP_CONFIG_LOGO'),
'paypal_show_advantage' => Configuration::get('PAYPAL_API_ADVANTAGES'),
);
$params['short_cut'] = $this->createShortcutForm($module);
$context = Context::getContext();
$context->smarty->assign(array(
'need_rounding' => ((Configuration::get('PS_ROUND_TYPE') == Order::ROUND_ITEM) && (Configuration::get('PS_PRICE_ROUND_MODE') == PS_ROUND_HALF_DOWN) ? 0 : 1),
'ppp_active' => Configuration::get('PAYPAL_PLUS_ENABLED'),
));
return $params;
}
public function createShortcutForm($module)
{
$fields_form = array();
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $module->l('PayPal Express Shortcut', get_class($this)),
'icon' => 'icon-cogs',
),
'submit' => array(
'title' => $module->l('Save', get_class($this)),
'class' => 'btn btn-default pull-right button',
),
);
$fields_form[0]['form']['input'] = array(
array(
'type' => 'html',
'name' => 'paypal_desc_shortcut',
'html_content' => $module->l('The PayPal shortcut is displayed directly in the cart or on your product pages, allowing a faster checkout experience for your buyers. It requires fewer pages, clicks and seconds in order to finalize the payment. PayPal provides you with the clients billing and shipping information so that you dont have to collect it yourself.', get_class($this)),
),
array(
'type' => 'switch',
'label' => $module->l('Display the shortcut on product pages', get_class($this)),
'name' => 'paypal_show_shortcut',
'is_bool' => true,
'hint' => $module->l('Recommended for mono-product websites.', get_class($this)),
'values' => array(
array(
'id' => 'paypal_show_shortcut_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_show_shortcut_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
),
array(
'type' => 'switch',
'label' => $module->l('Display shortcut in the cart', get_class($this)),
'name' => 'paypal_show_shortcut_cart',
'is_bool' => true,
'hint' => $module->l('Recommended for multi-products websites.', get_class($this)),
'values' => array(
array(
'id' => 'paypal_show_shortcut_cart_on',
'value' => 1,
'label' => $module->l('Enabled', get_class($this)),
),
array(
'id' => 'paypal_show_shortcut_cart_off',
'value' => 0,
'label' => $module->l('Disabled', get_class($this)),
)
),
),
);
$fields_value = array(
'paypal_show_shortcut' => Configuration::get('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT'),
'paypal_show_shortcut_cart' => Configuration::get('PAYPAL_EXPRESS_CHECKOUT_SHORTCUT_CART'),
);
$helper = new HelperForm();
$helper->module = $module;
$helper->name_controller = 'form_shortcut';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$module->name;
$helper->title = $module->displayName;
$helper->show_toolbar = false;
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->default_form_language = $default_lang;
$helper->submit_action = 'submit_shortcut';
$helper->allow_employee_form_lang = $default_lang;
$helper->tpl_vars = array(
'fields_value' => $fields_value,
'id_language' => Context::getContext()->language->id,
'back_url' => $module->module_link.'#paypal_params'
);
return $helper->generateForm($fields_form);
}
/**
@@ -405,7 +200,9 @@ class MethodPPP extends AbstractMethodPaypal
try {
// Use this call to create a profile.
$createProfileResponse = $webProfile->create($this->_getCredentialsInfo());
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
} catch (Exception $ex) {
$module = Module::getInstanceByName('paypal');
$this->errors[] = $module->l('An error occurred while creating your web experience. Check your credentials.', get_class($this));
return false;
}
@@ -417,6 +214,9 @@ class MethodPPP extends AbstractMethodPaypal
*/
public function init()
{
if ($this->isConfigured() == false) {
return '';
}
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// ### Itemized information
@@ -469,7 +269,23 @@ class MethodPPP extends AbstractMethodPaypal
// The return object contains the state and the
// url to which the buyer must be redirected to
// for payment approval
$payment->create($this->_getCredentialsInfo());
try {
$payment->create($this->_getCredentialsInfo());
} catch (Exception $e) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1)[0];
$message = 'Error in ' . $backtrace['file'];
$message .= ' (line ' . $backtrace['line'] . '); ';
$message .= 'Message: ' . $e->getMessage() . ';';
$message .= 'File: ' . $e->getFile() . '; ';
$message .= 'Line: ' . $e->getLine();
ProcessLoggerHandler::openLogger();
ProcessLoggerHandler::logError($message);
ProcessLoggerHandler::closeLogger();
return '';
}
// ### Get redirect url
// The API response provides the url that you must redirect
@@ -581,7 +397,7 @@ class MethodPPP extends AbstractMethodPaypal
* Update payment requestbefore redirection.
* Add reductions.
*/
public function doPatch()
public function doPatch($paymentId = null)
{
$discounts = Context::getContext()->cart->getCartRules();
$total_discount = 0;
@@ -593,7 +409,11 @@ class MethodPPP extends AbstractMethodPaypal
// Retrieve the payment object by calling the tatic `get` method
// on the Payment class by passing a valid Payment ID
$payment = Payment::get(Context::getContext()->cookie->paypal_plus_payment, $this->_getCredentialsInfo());
if ($paymentId === null) {
$paymentId = Context::getContext()->cookie->paypal_plus_payment;
}
$payment = Payment::get($paymentId, $this->_getCredentialsInfo());
$cart = new Cart(Context::getContext()->cart->id);
$address_delivery = new Address($cart->id_address_delivery);
@@ -636,39 +456,21 @@ class MethodPPP extends AbstractMethodPaypal
{
$context = Context::getContext();
$cart = $context->cart;
$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
throw new Exception('Customer is not loaded object');
}
// Get the payment Object by passing paymentId
// payment id was previously stored in session in
// CreatePaymentUsingPayPal.php
$paymentId = $this->short_cut ? $context->cookie->paypal_pSc : $this->paymentId;
$payment = Payment::get($paymentId, $this->_getCredentialsInfo());
if ($this->short_cut) {
$discounts = Context::getContext()->cart->getCartRules();
if (count($discounts) > 0) {
Context::getContext()->cookie->__unset('paypal_pSc');
Context::getContext()->cookie->__unset('paypal_pSc_payerid');
throw new Exception('The total of the order do not match amount paid.');
}
$address_delivery = new Address($cart->id_address_delivery);
$state = '';
if ($address_delivery->id_state) {
$state = new State((int) $address_delivery->id_state);
}
$state_name = $state ? $state->iso_code : '';
$patchAdd = new Patch();
$patchAdd->setOp('replace')
->setPath('/transactions/0/item_list/shipping_address')
->setValue(json_decode('{
"recipient_name": "'.$address_delivery->firstname.' '.$address_delivery->lastname.'",
"line1": "'.$address_delivery->address1.'",
"city": "'.$address_delivery->city.'",
"state": "'.$state_name.'",
"postal_code": "'.$address_delivery->postcode.'",
"country_code": "'.Country::getIsoById($address_delivery->id_country).'"
}'));
$patchRequest = new PatchRequest();
$patchRequest->setPatches(array($patchAdd));
$payment->update($patchRequest, $this->_getCredentialsInfo());
// The total of the order or address can be changed, so need to update transaction info
if ($this->short_cut) {
$this->doPatch($paymentId);
}
// ### Payment Execute
@@ -684,17 +486,24 @@ class MethodPPP extends AbstractMethodPaypal
// do that by passing the transaction object with just `amount` field in it.
$exec_payment = $payment->execute($execution, $this->_getCredentialsInfo());
$this->setDetailsTransaction($exec_payment);
$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
throw new Exception('Customer is not loaded object');
}
$currency = $context->currency;
$total = (float)$exec_payment->transactions[0]->amount->total;
$paypal = Module::getInstanceByName($this->name);
$order_state = Configuration::get('PS_OS_PAYMENT');
$order_state = $this->getOrderStatus();
$paypal->validateOrder($cart->id, $order_state, $total, $this->getPaymentMethod(), null, $this->getDetailsTransaction(), (int)$currency->id, false, $customer->secure_key);
}
public function getOrderStatus()
{
if ((int)Configuration::get('PAYPAL_CUSTOMIZE_ORDER_STATUS')) {
$orderStatus = (int)Configuration::get('PAYPAL_OS_ACCEPTED_TWO');
} else {
$orderStatus = (int)Configuration::get('PS_OS_PAYMENT');
}
return $orderStatus;
}
public function setDetailsTransaction($transaction)
{
$payment_info = $transaction->transactions[0];
@@ -803,11 +612,30 @@ class MethodPPP extends AbstractMethodPaypal
/**
* Get payment details
* @param $id_payment
* @return mixed
* @return bool|mixed
*/
public function getInstructionInfo($id_payment)
{
$sale = Payment::get($id_payment, $this->_getCredentialsInfo());
if ($this->isConfigured() == false) {
return false;
}
try {
$sale = Payment::get($id_payment, $this->_getCredentialsInfo());
} catch (Exception $e) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1)[0];
$message = 'Error in ' . $backtrace['file'];
$message .= ' (line ' . $backtrace['line'] . '); ';
$message .= 'Message: ' . $e->getMessage() . ';';
$message .= 'File: ' . $e->getFile() . '; ';
$message .= 'Line: ' . $e->getLine();
ProcessLoggerHandler::openLogger();
ProcessLoggerHandler::logError($message);
ProcessLoggerHandler::closeLogger();
return false;
}
return $sale->payment_instruction;
}
@@ -828,39 +656,134 @@ class MethodPPP extends AbstractMethodPaypal
'action_url' => $context->link->getModuleLink($this->name, 'ScInit', array(), true),
'environment' => $environment,
));
if ($page_source == 'product') {
$context->smarty->assign(array(
'es_cs_product_attribute' => Tools::getValue('id_product_attribute'),
'es_cs_product_attribute' => Tools::getValue('id_product_attribute')
));
return $context->smarty->fetch('module:paypal/views/templates/hook/PPP_shortcut.tpl');
} elseif ($page_source == 'cart') {
return $context->smarty->fetch('module:paypal/views/templates/hook/cart_shortcut.tpl');
}
$context->smarty->assign('source_page', $page_source);
return $context->smarty->fetch('module:paypal/views/templates/hook/shortcut.tpl');
}
/**
* @see AbstractMethodPaypal::getLinkToTransaction()
*/
public function getLinkToTransaction($id_transaction, $sandbox)
public function getLinkToTransaction($log)
{
if ($sandbox) {
if ($log->sandbox) {
$url = 'https://www.sandbox.paypal.com/activity/payment/';
} else {
$url = 'https://www.paypal.com/activity/payment/';
}
return $url . $id_transaction;
return $url . $log->id_transaction;
}
/**
* @return bool
*/
public function isConfigured()
public function isConfigured($mode = null)
{
if (Configuration::get('PAYPAL_SANDBOX')) {
return $this->credentialsSetted($mode) && Configuration::get('PAYPAL_PLUS_EXPERIENCE');
}
/**
* @return bool
*/
public function credentialsSetted($mode = null)
{
if ($mode == null) {
$mode = (int) Configuration::get('PAYPAL_SANDBOX');
}
if ($mode) {
return (bool)Configuration::get('PAYPAL_SANDBOX_CLIENTID') && (bool)Configuration::get('PAYPAL_SANDBOX_SECRET');
} else {
return (bool)Configuration::get('PAYPAL_LIVE_CLIENTID') && (bool)Configuration::get('PAYPAL_LIVE_SECRET');
}
}
/**
* Assign form data for Paypal Plus payment option
* @return boolean
*/
public function assignJSvarsPaypalPlus()
{
$context = Context::getContext();
try {
$approval_url = $this->init();
$context->cookie->__set('paypal_plus_payment', $this->paymentId);
} catch (Exception $e) {
return false;
}
$paypal = Module::getInstanceByName('paypal');
$address_invoice = new Address($context->cart->id_address_invoice);
$country_invoice = new Country($address_invoice->id_country);
Media::addJsDef(array(
'approvalUrlPPP' => $approval_url,
'modePPP' => Configuration::get('PAYPAL_SANDBOX') ? 'sandbox' : 'live',
'languageIsoCodePPP' => $context->language->iso_code,
'countryIsoCodePPP' => $country_invoice->iso_code,
'ajaxPatchUrl' => $context->link->getModuleLink('paypal', 'pppPatch', array(), true),
));
Media::addJsDefL('waitingRedirectionMsg', $paypal->l('In few seconds, you will be redirected to PayPal. Please wait.', get_class($this)));
return true;
}
public function getTplVars()
{
$sandboxMode = (int)Configuration::get('PAYPAL_SANDBOX');
if ($sandboxMode) {
$tpl_vars = array(
'paypal_sandbox_clientid' => Configuration::get('PAYPAL_SANDBOX_CLIENTID'),
'paypal_sandbox_secret' => Configuration::get('PAYPAL_SANDBOX_SECRET'),
);
} else {
$tpl_vars = array(
'paypal_live_secret' => Configuration::get('PAYPAL_LIVE_SECRET'),
'paypal_live_clientid' => Configuration::get('PAYPAL_LIVE_CLIENTID')
);
}
$tpl_vars['accountConfigured'] = $this->isConfigured();
$tpl_vars['sandboxMode'] = $sandboxMode;
return $tpl_vars;
}
public function checkCredentials()
{
$experience_web = $this->createWebExperience();
if ($experience_web) {
Configuration::updateValue('PAYPAL_PLUS_EXPERIENCE', $experience_web->id);
} else {
Configuration::updateValue('PAYPAL_PLUS_EXPERIENCE', '');
}
}
public function getAdvancedFormInputs()
{
$inputs = array();
$module = Module::getInstanceByName($this->name);
$orderStatuses = $module->getOrderStatuses();
$inputs[] = array(
'type' => 'select',
'label' => $module->l('Payment accepted and transaction completed', get_class($this)),
'name' => 'paypal_os_accepted_two',
'hint' => $module->l('You are currently using the Sale mode (the authorization and capture occur at the same time as the sale). So the payement is accepted instantly and the new order is created in the "Payment accepted" status. You can customize the status for orders with completed transactions. Ex : you can create an additional status "Payment accepted via PayPal" and set it as the default status.', get_class($this)),
'desc' => $module->l('Default status : Payment accepted', get_class($this)),
'options' => array(
'query' => $orderStatuses,
'id' => 'id',
'name' => 'name'
)
);
return $inputs;
}
}

View File

@@ -18,10 +18,11 @@
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @author 2007-2019 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright PayPal
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
/**
@@ -92,7 +93,13 @@ class PaypalCapture extends ObjectModel
$sql->from('paypal_order', 'po');
$sql->innerJoin('paypal_capture', 'pc', 'po.`id_paypal_order` = pc.`id_paypal_order`');
$sql->where('po.id_order = '.(int)$id_order);
return Db::getInstance()->getRow($sql);
$row = Db::getInstance()->getRow($sql);
if (is_array($row)) {
return $row;
} else {
return array();
}
}
/**

View File

@@ -18,10 +18,11 @@
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @author 2007-2019 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright PayPal
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
namespace PaypalAddons\classes;

View File

@@ -18,10 +18,11 @@
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @author 2007-2019 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright PayPal
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
use PaypalPPBTlib\Extensions\ProcessLogger\Classes\ProcessLoggerObjectModel;

View File

@@ -18,10 +18,11 @@
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @author 2007-2019 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright PayPal
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
/**
@@ -120,10 +121,16 @@ class PaypalOrder extends ObjectModel
*/
public static function getOrderById($id_order)
{
return Db::getInstance()->getRow(
'SELECT * FROM `'._DB_PREFIX_.'paypal_order`
WHERE `id_order` = '.(int) $id_order
);
$query = new DBQuery();
$query->from('paypal_order');
$query->where('id_order = ' . (int) $id_order);
$rowOrder = Db::getInstance()->getRow($query);
if (is_array($rowOrder)) {
return $rowOrder;
} else {
return array();
}
}
/**

View File

@@ -20,29 +20,23 @@
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
/**
* Class PaypalVaulting.
* Class PaypalOrder.
*/
class PaypalVaulting extends ObjectModel
{
/** @var string Token received from BT */
public $token;
/** @var integer id of the Prestashop Customer object*/
public $id_customer;
/** @var int BT Customer ID */
public $id_paypal_customer;
/** @var string hash of the remembered card ids*/
public $rememberedCards;
/** @var string client can set card name in prestashop account */
public $name;
/** @var string Card or paypal account short info like last 4 numbers/exp. date */
public $info;
/** @var string card ou paypal, etc... */
public $payment_tool;
/** @var bool mode of payment (sandbox or live) */
public $sandbox;
/** @var string Object creation date */
public $date_add;
@@ -50,6 +44,9 @@ class PaypalVaulting extends ObjectModel
/** @var string Object last modification date */
public $date_upd;
/** @var string */
public $profile_key;
/**
* @see ObjectModel::$definition
*/
@@ -58,70 +55,14 @@ class PaypalVaulting extends ObjectModel
'primary' => 'id_paypal_vaulting',
'multilang' => false,
'fields' => array(
'token' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'id_paypal_customer' => array('type' => self::TYPE_INT),
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'info' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'payment_tool' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'rememberedCards' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'profile_key' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'sandbox' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
)
);
/**
* Checking if vault was created already for this card/pp account
* @return boolean
*/
public static function vaultingExist($token, $customer)
{
$db = Db::getInstance();
$query = new DbQuery();
$query->select('id_paypal_vaulting');
$query->from('paypal_vaulting');
$query->where('token = "'.pSQL($token).'" AND id_paypal_customer = '.(int)$customer);
$result = $db->getValue($query);
return $result ? true : false;
}
/**
* Get all vaulted methods (cards, accounts) for this customer
* @param integer $customer PrestaShop Customer ID
* @param string $method payment tool (card or paypal account)
* @return array PaypalVaulting
*/
public static function getCustomerMethods($customer, $method)
{
$db = Db::getInstance();
$query = new DbQuery();
$query->select('*');
$query->from('paypal_vaulting', 'pv');
$query->leftJoin('paypal_customer', 'pc', 'pv.id_paypal_customer = pc.id_paypal_customer');
$query->where('pc.id_customer = '.(int)$customer);
$query->where('pv.payment_tool = "'.pSQL($method).'"');
$query->where('pc.sandbox = ' . (int)Configuration::get('PAYPAL_SANDBOX'));
$result = $db->executeS($query);
return $result;
}
/**
* Get vaulted methods grouped by tools (card or paypal account)
* @param integer $customer PrestaShop Customer ID
* @return array PaypalVaulting
*/
public static function getCustomerGroupedMethods($customer)
{
$db = Db::getInstance();
$methods = array();
$query = new DbQuery();
$query->select('*');
$query->from('paypal_vaulting', 'pv');
$query->leftJoin('paypal_customer', 'pc', 'pv.id_paypal_customer = pc.id_paypal_customer');
$query->where('pc.id_customer = '.(int)$customer);
$query->where('pc.sandbox = ' . (int)Configuration::get('PAYPAL_SANDBOX'));
$results = $db->query($query);
while ($result = $db->nextRow($results)) {
$methods[$result['payment_tool']][] = $result;
}
return $methods;
}
}

View File

@@ -18,10 +18,11 @@
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* @author 2007-2019 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @copyright PayPal
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");