diff --git a/README.md b/README.md index 4b14ea9..5ff8afc 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Paymentwall_Base::setSecretKey('YOUR_SECRET_KEY'); // available in your Paymentw ``` #### Widget Call -[Web API details](http://www.paymentwall.com/en/documentation/Digital-Goods-API/710#paymentwall_widget_call_flexible_widget_call) +[Web API details](https://docs.paymentwall.com/integration/widget/digital-goods#widget-call) The widget is a payment page hosted by Paymentwall that embeds the entire payment flow: selecting the payment method, completing the billing details, and providing customer support via the Help section. You can redirect the users to this page or embed it via iframe. Below is an example that renders an iframe with Paymentwall Widget. @@ -91,6 +91,72 @@ if ($pingback->validate()) { } ``` +## Digital Checkout API + +#### Initializing Paymentwall + +Using Paymentwall PHP Library v2: +```php +require_once('/path/to/paymentwall-php/lib/paymentwall.php'); +Paymentwall_Config::getInstance()->set(array( + 'api_type' => Paymentwall_Config::API_CHECKOUT, + 'public_key' => 'YOUR_PUBLIC_KEY', + 'private_key' => 'YOUR_PRIVATE_KEY' +)); +``` + +#### Widget Call +[Web API details](https://docs.paymentwall.com/integration/widget/digital-goods#widget-call) + +The widget is a payment page hosted by Paymentwall that embeds the entire payment flow: selecting the payment method, completing the billing details, and providing customer support via the Help section. You can redirect the users to this page or embed it via iframe. Below is an example that renders an iframe with Paymentwall Widget. + +```php +$widget = new Paymentwall_Widget( + 'user40012', // id of the end-user who's making the payment + 'pw', // widget code, e.g. pw; can be picked inside of your merchant account + array( // product details for Flexible Widget Call. To let users select the product on Paymentwall's end, leave this array empty + new Paymentwall_Product( + 'product301', // id of the product in your system + 9.99, // price + 'USD', // currency code + 'Gold Membership', // product name + Paymentwall_Product::TYPE_SUBSCRIPTION, // this is a time-based product; for one-time products, use Paymentwall_Product::TYPE_FIXED and omit the following 3 array elements + 1, // duration is 1 + Paymentwall_Product::PERIOD_TYPE_MONTH, // month + true // recurring + ) + ), + array('email' => 'user@hostname.com') // additional parameters +); +echo $widget->getHtmlCode(); +``` + +#### Pingback Processing + +The Pingback is a webhook notifying about a payment being made. Pingbacks are sent via HTTP/HTTPS to your servers. To process pingbacks use the following code: +```php +require_once('/path/to/paymentwall-php/lib/paymentwall.php'); +Paymentwall_Config::getInstance()->set(array( + 'api_type' => Paymentwall_Config::API_CHECKOUT, + 'public_key' => 'YOUR_PUBLIC_KEY', + 'private_key' => 'YOUR_PRIVATE_KEY' +)); +$pingback = new Paymentwall_Pingback($_GET, $_SERVER['REMOTE_ADDR']); +if ($pingback->validate()) { + $productId = $pingback->getProduct()->getId(); + if ($pingback->isDeliverable()) { + // deliver the product + } else if ($pingback->isCancelable()) { + // withdraw the product + } else if ($pingback->isUnderReview()) { + // set "pending" status to order + } + echo 'OK'; // Paymentwall expects response to be OK, otherwise the pingback will be resent +} else { + echo $pingback->getErrorSummary(); +} +``` + ## Virtual Currency API #### Initializing Paymentwall diff --git a/lib/Paymentwall/Config.php b/lib/Paymentwall/Config.php index 144a678..b5749ab 100644 --- a/lib/Paymentwall/Config.php +++ b/lib/Paymentwall/Config.php @@ -2,14 +2,19 @@ class Paymentwall_Config { - const VERSION = '2.0.0'; - - const API_BASE_URL = 'https://api.paymentwall.com/api'; + const VERSION = '2.2.2'; + const BASE_URL = 'https://api.paymentwall.com'; + /** + * @deprecated 2.2.2 + */ + const API_BASE_URL = self::BASE_URL . '/api'; const API_VC = 1; const API_GOODS = 2; const API_CART = 3; + const API_CHECKOUT = 4; + protected $baseUrl = self::BASE_URL; protected $apiType = self::API_GOODS; protected $publicKey; protected $privateKey; @@ -17,11 +22,24 @@ class Paymentwall_Config private static $instance; + public function setBaseUrl($url) + { + $this->baseUrl = $url; + } + + public function getBaseUrl() + { + return $this->baseUrl; + } + public function getApiBaseUrl() { - return $this->apiBaseUrl; + return $this->apiBaseUrl ? $this->apiBaseUrl : $this->getBaseUrl() . '/api'; } + /** + * @deprecated 2.2.2 Should use setBaseUrl instead + */ public function setApiBaseUrl($url = '') { $this->apiBaseUrl = $url; @@ -69,9 +87,15 @@ public function isTest() public function set($config = array()) { + /** + * @deprecated 2.2.2 + */ if (isset($config['api_base_url'])) { $this->setApiBaseUrl($config['api_base_url']); } + if (isset($config['base_url'])) { + $this->setBaseUrl($config['base_url']); + } if (isset($config['api_type'])) { $this->setLocalApiType($config['api_type']); } @@ -84,8 +108,8 @@ public function set($config = array()) } /** - * @return $this Returns class instance. - */ + * @return $this Returns class instance. + */ public static function getInstance() { if (!isset(self::$instance)) { diff --git a/lib/Paymentwall/Instance.php b/lib/Paymentwall/Instance.php index aeb2fd6..5ba2bac 100644 --- a/lib/Paymentwall/Instance.php +++ b/lib/Paymentwall/Instance.php @@ -18,6 +18,11 @@ protected function getConfig() return $this->config; } + protected function getBaseUrl() + { + return $this->getConfig()->getBaseUrl(); + } + protected function getApiBaseUrl() { return $this->getConfig()->getApiBaseUrl(); diff --git a/lib/Paymentwall/Pingback.php b/lib/Paymentwall/Pingback.php index 1bbaec8..41fad0a 100644 --- a/lib/Paymentwall/Pingback.php +++ b/lib/Paymentwall/Pingback.php @@ -64,6 +64,9 @@ public function isSignatureValid() $signatureParams = array('uid', 'goodsid', 'slength', 'speriod', 'type', 'ref'); + } else if ($this->getApiType() == Paymentwall_Config::API_CHECKOUT) { + $signatureParams = array('uid', 'goodsid', 'slength', 'speriod', 'type', 'ref'); + } else { // API_CART $signatureParams = array('uid', 'goodsid', 'type', 'ref'); @@ -116,6 +119,8 @@ public function isParametersValid() $requiredParams = array('uid', 'currency', 'type', 'ref', 'sig'); } else if ($this->getApiType() == Paymentwall_Config::API_GOODS) { $requiredParams = array('uid', 'goodsid', 'type', 'ref', 'sig'); + } else if ($this->getApiType() == Paymentwall_Config::API_CHECKOUT) { + $requiredParams = array('uid', 'goodsid', 'type', 'ref', 'sig'); } else { // Cart API $requiredParams = array('uid', 'goodsid', 'type', 'ref', 'sig'); } diff --git a/lib/Paymentwall/Widget.php b/lib/Paymentwall/Widget.php index 831cd8e..a9143f3 100644 --- a/lib/Paymentwall/Widget.php +++ b/lib/Paymentwall/Widget.php @@ -4,6 +4,7 @@ class Paymentwall_Widget extends Paymentwall_Instance { const CONTROLLER_PAYMENT_VIRTUAL_CURRENCY = 'ps'; const CONTROLLER_PAYMENT_DIGITAL_GOODS = 'subscription'; + const CONTROLLER_PAYMENT_CHECKOUT = 'v1/checkout/orders'; const CONTROLLER_PAYMENT_CART = 'cart'; protected $userId; @@ -28,7 +29,9 @@ public function getUrl() $productsNumber = count($this->products); - if ($this->getApiType() == Paymentwall_Config::API_GOODS) { + $apiTypes = array(Paymentwall_Config::API_GOODS, Paymentwall_Config::API_CHECKOUT); + + if (in_array($this->getApiType(), $apiTypes)) { if (!empty($this->products)) { @@ -105,7 +108,11 @@ public function getUrl() $signatureVersion ); - return $this->getApiBaseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params); + $baseUrl = function () { + return $this->getApiType() == Paymentwall_Config::API_CHECKOUT ? $this->getBaseUrl() : $this->getApiBaseUrl() ; + }; + + return $baseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params); } public function getHtmlCode($attributes = array()) @@ -131,24 +138,26 @@ 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; - } + 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 if ($this->getApiType() == Paymentwall_Config::API_CHECKOUT) { + $controller = self::CONTROLLER_PAYMENT_CHECKOUT; + } else { + $controller = self::CONTROLLER_PAYMENT_CART; + } + + return $controller; + } }