Skip to content

Commit

Permalink
Merge branch 'gitlab-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason authored and Mason committed Apr 24, 2019
2 parents b7e7c7d + c9d7c84 commit 040cbd3
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 153 deletions.
2 changes: 1 addition & 1 deletion composer.json
@@ -1,7 +1,7 @@
{
"name": "paymentwall/paymentwall-php",
"description": "Paymentwall PHP Library. Paymentwall is the leading digital payments platform for globally monetizing digital goods and services.",
"version": "2.2.2",
"version": "2.2.3",
"type": "library",
"homepage": "https://www.paymentwall.com/?source=gh",
"keywords": [
Expand Down
307 changes: 155 additions & 152 deletions lib/Paymentwall/Widget.php
Expand Up @@ -2,156 +2,159 @@

class Paymentwall_Widget extends Paymentwall_Instance
{
const CONTROLLER_PAYMENT_VIRTUAL_CURRENCY = 'ps';
const CONTROLLER_PAYMENT_DIGITAL_GOODS = 'subscription';
const CONTROLLER_PAYMENT_CART = 'cart';

protected $userId;
protected $widgetCode;
protected $products;
protected $extraParams;

public function __construct($userId, $widgetCode = '', $products = array(), $extraParams = array()) {
$this->userId = $userId;
$this->widgetCode = $widgetCode;
$this->products = $products;
$this->extraParams = $extraParams;
}

public function getUrl()
{
$params = array(
'key' => $this->getPublicKey(),
'uid' => $this->userId,
'widget' => $this->widgetCode
);

$productsNumber = count($this->products);

if ($this->getApiType() == Paymentwall_Config::API_GOODS) {

if (!empty($this->products)) {

if ($productsNumber == 1) {

$product = current($this->products);

if ($product->getTrialProduct() instanceof Paymentwall_Product) {
$postTrialProduct = $product;
$product = $product->getTrialProduct();
}

$params['amount'] = $product->getAmount();
$params['currencyCode'] = $product->getCurrencyCode();
$params['ag_name'] = $product->getName();
$params['ag_external_id'] = $product->getId();
$params['ag_type'] = $product->getType();

if ($product->getType() == Paymentwall_Product::TYPE_SUBSCRIPTION) {
$params['ag_period_length'] = $product->getPeriodLength();
$params['ag_period_type'] = $product->getPeriodType();
if ($product->isRecurring()) {

$params['ag_recurring'] = intval($product->isRecurring());

if (isset($postTrialProduct)) {
$params['ag_trial'] = 1;
$params['ag_post_trial_external_id'] = $postTrialProduct->getId();
$params['ag_post_trial_period_length'] = $postTrialProduct->getPeriodLength();
$params['ag_post_trial_period_type'] = $postTrialProduct->getPeriodType();
$params['ag_post_trial_name'] = $postTrialProduct->getName();
$params['post_trial_amount'] = $postTrialProduct->getAmount();
$params['post_trial_currencyCode'] = $postTrialProduct->getCurrencyCode();
}

}
}

} else {
//TODO: $this->appendToErrors('Only 1 product is allowed in flexible widget call');
}

}

} else if ($this->getApiType() == Paymentwall_Config::API_CART) {

$index = 0;
foreach ($this->products as $product) {
$params['external_ids[' . $index . ']'] = $product->getId();

if (isset($product->amount)) {
$params['prices[' . $index . ']'] = $product->getAmount();
}
if (isset($product->currencyCode)) {
$params['currencies[' . $index . ']'] = $product->getCurrencyCode();
}
if (isset($product->name)) {
$params['names[' . $index . ']'] = $product->getName();
}

$index++;
}
unset($index);
}

$params['sign_version'] = $signatureVersion = $this->getDefaultSignatureVersion();

if (!empty($this->extraParams['sign_version'])) {
$signatureVersion = $params['sign_version'] = $this->extraParams['sign_version'];
}

$params = array_merge($params, $this->extraParams);

$widgetSignatureModel = new Paymentwall_Signature_Widget();
$params['sign'] = $widgetSignatureModel->calculate(
$params,
$signatureVersion
);

return $this->getApiBaseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params);
}

public function getHtmlCode($attributes = array())
{
$defaultAttributes = array(
'frameborder' => '0',
'width' => '750',
'height' => '800'
);

$attributes = array_merge($defaultAttributes, $attributes);

$attributesQuery = '';
foreach ($attributes as $attr => $value) {
$attributesQuery .= ' ' . $attr . '="' . $value . '"';
}

return '<iframe src="' . $this->getUrl() . '" ' . $attributesQuery . '></iframe>';

}

protected function getDefaultSignatureVersion() {
return $this->getApiType() != Paymentwall_Config::API_CART ? Paymentwall_Signature_Abstract::DEFAULT_VERSION : Paymentwall_Signature_Abstract::VERSION_TWO;
}

protected function buildController($widget = '')
{
$controller = null;
$isPaymentWidget = !preg_match('/^w|s|mw/', $widget);

if ($this->getApiType()== Paymentwall_Config::API_VC) {
if ($isPaymentWidget) {
$controller = self::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY;
}
} else if ($this->getApiType() == Paymentwall_Config::API_GOODS) {
/**
* @todo cover case with offer widget for digital goods for non-flexible widget call
*/
$controller = self::CONTROLLER_PAYMENT_DIGITAL_GOODS;
} else {
$controller = self::CONTROLLER_PAYMENT_CART;
}

return $controller;
}
const CONTROLLER_PAYMENT_VIRTUAL_CURRENCY = 'ps';
const CONTROLLER_PAYMENT_DIGITAL_GOODS = 'subscription';
const CONTROLLER_PAYMENT_CART = 'cart';

protected $userId;
protected $widgetCode;
protected $products;
protected $extraParams;

public function __construct($userId, $widgetCode = '', $products = array(), $extraParams = array()) {
$this->userId = $userId;
$this->widgetCode = $widgetCode;
$this->products = $products;
$this->extraParams = $extraParams;
}

public function getUrl()
{
$params = array(
'key' => $this->getPublicKey(),
'uid' => $this->userId,
'widget' => $this->widgetCode
);

$productsNumber = count($this->products);

if ($this->getApiType() == Paymentwall_Config::API_GOODS) {

if (!empty($this->products)) {

if ($productsNumber == 1) {

$product = current($this->products);

if ($product->getTrialProduct() instanceof Paymentwall_Product) {
$postTrialProduct = $product;
$product = $product->getTrialProduct();
}

$params['amount'] = $product->getAmount();
$params['currencyCode'] = $product->getCurrencyCode();
$params['ag_name'] = $product->getName();
$params['ag_external_id'] = $product->getId();
$params['ag_type'] = $product->getType();

if ($product->getType() == Paymentwall_Product::TYPE_SUBSCRIPTION) {
$params['ag_period_length'] = $product->getPeriodLength();
$params['ag_period_type'] = $product->getPeriodType();
if ($product->isRecurring()) {

$params['ag_recurring'] = intval($product->isRecurring());

if (isset($postTrialProduct)) {
$params['ag_trial'] = 1;
$params['ag_post_trial_external_id'] = $postTrialProduct->getId();
$params['ag_post_trial_period_length'] = $postTrialProduct->getPeriodLength();
$params['ag_post_trial_period_type'] = $postTrialProduct->getPeriodType();
$params['ag_post_trial_name'] = $postTrialProduct->getName();
$params['post_trial_amount'] = $postTrialProduct->getAmount();
$params['post_trial_currencyCode'] = $postTrialProduct->getCurrencyCode();
}

}
}

} else {
//TODO: $this->appendToErrors('Only 1 product is allowed in flexible widget call');
}

}

} else if ($this->getApiType() == Paymentwall_Config::API_CART) {

$external_ids = array();
$prices = array();
$currencies = array();
$names = array();
foreach ($this->products as $product) {
$external_ids[] = $product->getId();
$prices[] = $product->amount ?: 0;
$currencies[] = $product->currencyCode ?: '';
$names[] = $product->name ?: '';
}
$params['external_ids'] = $external_ids;
if (!empty($prices)) {
$params['prices'] = $prices;
}
if (!empty($currencies)) {
$params['currencies'] = $currencies;
}
if (array_filter($names)) {
$params['names'] = $names;
}
}

$params['sign_version'] = $signatureVersion = $this->getDefaultSignatureVersion();

if (!empty($this->extraParams['sign_version'])) {
$signatureVersion = $params['sign_version'] = $this->extraParams['sign_version'];
}

$params = array_merge($params, $this->extraParams);

$widgetSignatureModel = new Paymentwall_Signature_Widget();
$params['sign'] = $widgetSignatureModel->calculate(
$params,
$signatureVersion
);

return $this->getApiBaseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params);
}

public function getHtmlCode($attributes = array())
{
$defaultAttributes = array(
'frameborder' => '0',
'width' => '750',
'height' => '800'
);

$attributes = array_merge($defaultAttributes, $attributes);

$attributesQuery = '';
foreach ($attributes as $attr => $value) {
$attributesQuery .= ' ' . $attr . '="' . $value . '"';
}

return '<iframe src="' . $this->getUrl() . '" ' . $attributesQuery . '></iframe>';

}

protected function getDefaultSignatureVersion() {
return $this->getApiType() != Paymentwall_Config::API_CART ? Paymentwall_Signature_Abstract::DEFAULT_VERSION : Paymentwall_Signature_Abstract::VERSION_TWO;
}

protected function buildController($widget = '')
{
$controller = null;
$isPaymentWidget = !preg_match('/^w|s|mw/', $widget);

if ($this->getApiType()== Paymentwall_Config::API_VC) {
if ($isPaymentWidget) {
$controller = self::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY;
}
} else if ($this->getApiType() == Paymentwall_Config::API_GOODS) {
/**
* @todo cover case with offer widget for digital goods for non-flexible widget call
*/
$controller = self::CONTROLLER_PAYMENT_DIGITAL_GOODS;
} else {
$controller = self::CONTROLLER_PAYMENT_CART;
}

return $controller;
}
}

0 comments on commit 040cbd3

Please sign in to comment.