Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/Payplug/Core/APIRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class APIRoutes
public static $API_BASE_URL;
public static $SERVICE_BASE_URL;

/**
* @var string the root URL of the Hosted Fields
*/
public static $HOSTED_FIELDS_RESOURCE;

/**
* @var string the root URL of the Hosted Fields
*/
public static $HOSTED_FIELDS_RESOURCE_RETRIEVE;

const API_VERSION = 1;

// Resources routes
Expand Down Expand Up @@ -98,6 +108,22 @@ public static function setServiceBaseUrl($serviceBaseUrl)
self::$SERVICE_BASE_URL = $serviceBaseUrl;
}

/**
* @param $hostedFieldsUrl
* @return void
*/
public static function setHostedFieldsResource($hostedFieldsUrl){
self::$HOSTED_FIELDS_RESOURCE = $hostedFieldsUrl;
}

/**
* @param $hostedFieldsRetrieveUrl
* @return void
*/
public static function setHostedFieldsResourceRetrieve($hostedFieldsRetrieveUrl){
self::$HOSTED_FIELDS_RESOURCE_RETRIEVE = $hostedFieldsRetrieveUrl;
}

/**
* Gets a route that allows to check whether the remote API is up.
*
Expand All @@ -108,6 +134,7 @@ public static function getTestRoute()
return APIRoutes::$API_BASE_URL . '/test';
}
}

APIRoutes::$API_BASE_URL = 'https://api.payplug.com';
APIRoutes::$SERVICE_BASE_URL = 'https://retail.service.payplug.com';
APIRoutes::$HOSTED_FIELDS_RESOURCE = 'https://payment.dalenys.com/';
APIRoutes::$HOSTED_FIELDS_RESOURCE_RETRIEVE = 'https://secure-magenta.dalenys.com/front/service/rest/export';
6 changes: 5 additions & 1 deletion lib/Payplug/Core/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function exec()
*/
public function close()
{
curl_close($this->_curl);
// curl_close() is a no-op since PHP 8.0 and deprecated in PHP 8.5.
// Calling it under PHP 8.5 with developer mode throws (deprecations -> exceptions).
if (PHP_VERSION_ID < 80000) {
curl_close($this->_curl);
}
}

/**
Expand Down
26 changes: 14 additions & 12 deletions lib/Payplug/Core/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,21 @@ private function request(
}

$userAgent = self::getUserAgent();

$headers = array();

if ($headersParams) {
foreach ($headersParams as $header) {
$headers[] = $header;
}
if (is_array($data) && isset($data['params']['OPERATIONTYPE']) && $data['params']['OPERATIONTYPE'] === "getTransaction")
{
$headers = array();
}
elseif (is_array($data) && isset($data['params'])) {
$headers = array(
'Content-Type: application/x-www-form-urlencoded',
);
} else {
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'User-Agent: ' . $userAgent
);
}

$headers[] = 'User-Agent: ' . $userAgent;
if ($authenticated) {
$headers[] = 'Authorization: Bearer ' . $this->_configuration->getToken();
$headers[] = 'PayPlug-Version: ' . $this->_configuration->getApiVersion();
Expand All @@ -279,7 +281,7 @@ private function request(
$request->setopt(CURLOPT_CAINFO, self::$CACERT_PATH);
$request->setopt(CURLOPT_FOLLOWLOCATION, true);
if (!empty($data)) {
if ('json' == $data_type) {
if (in_array('Content-Type: application/json', $headers)) {
$request->setopt(CURLOPT_POSTFIELDS, json_encode($data));
} else {
$request->setopt(CURLOPT_POSTFIELDS, http_build_query($data));
Expand Down
36 changes: 31 additions & 5 deletions lib/Payplug/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ class Payment
/**
* Retrieves a Payment.
*
* @param string $paymentId the payment ID
* @param $data
* @param Payplug $payplug the client configuration
* @param $isHostedField
*
* @return null|Resource\Payment the retrieved payment or null on error
*
* @throws Exception\ConfigurationNotSetException
*/
public static function retrieve($paymentId, $payplug = null)


public static function retrieve($data, Payplug $payplug = null, $isHostedField = false)
{
return Resource\Payment::retrieve($paymentId, $payplug);
return Resource\Payment::retrieve($data, $payplug, $isHostedField);
}

/**
Expand Down Expand Up @@ -47,12 +50,34 @@ public static function abort($paymentId, $payplug = null)
*
* @throws Exception\ConfigurationNotSetException
*/
public static function capture($paymentId, $payplug = null)

/**
* Capture a payment by its ID or data array.
*
* @param string|array $paymentId The payment ID as a string, or an array of payment data.
* @param Payplug|null $payplug The client configuration (optional).
* @return Resource\Payment|null The captured payment or null on error.
* @throws Exception\ConfigurationNotSetException
*/
public static function capture($paymentId, Payplug $payplug = null)
{
$payment = Resource\Payment::fromAttributes(array('id' => $paymentId));
return $payment->capture($payplug);
}

/**
* @description Authorize a Payment.
* @param $data
* @param Payplug|null $payplug
* @param $is_hosted_field
* @return mixed
*/
public static function authorize($data, Payplug $payplug = null, $is_hosted_field = false)
{
return Resource\Payment::authorize($data, $payplug, $is_hosted_field);

}

/**
* Creates a Payment.
*
Expand All @@ -65,7 +90,8 @@ public static function capture($paymentId, $payplug = null)
*/
public static function create(array $data, $payplug = null)
{
return Resource\Payment::create($data, $payplug);
return Resource\Payment::create($data, $payplug);

}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/Payplug/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class Refund {
* @param string|Payment $payment the payment id or the payment object
* @param array $data API data for refund
* @param Payplug $payplug the client configuration
* @param $is_hosted_field indicates if the payment is using hosted fields
*
* @return null|Refund the refund object
* @throws Exception\ConfigurationNotSetException
*/
public static function create($payment, $data = null, $payplug = null)
public static function create($payment, array $data = null, Payplug $payplug = null, $is_hosted_field = false)
{
return Resource\Refund::create($payment, $data, $payplug);
return Resource\Refund::create($payment, $data, $payplug, $is_hosted_field);
}

/**
Expand Down
87 changes: 70 additions & 17 deletions lib/Payplug/Resource/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ public function abort($payplug = null)

/**
* Captures a Payment.
*
*
* @param Payplug\Payplug $payplug the client configuration
*
* @return null|Payment the captured payment or null on error
*
* @throws Payplug\Exception\ConfigurationNotSetException
*/
public function capture($payplug = null)
public function capture(Payplug\Payplug $payplug = null)
{
if ($payplug === null) {
$payplug = Payplug\Payplug::getDefaultConfiguration();
Expand All @@ -148,31 +148,74 @@ public function capture($payplug = null)
}

/**
* Retrieves a Payment.
*
* @param string $paymentId the payment ID
* @param Payplug\Payplug $payplug the client configuration
*
* @return Payment the retrieved payment
*
* @throws Payplug\Exception\ConfigurationNotSetException
* @throws Payplug\Exception\UndefinedAttributeException
* @throws Payplug\Exception\NotFoundException
* @description Authorize a Payment.
* @param $data
* @param Payplug\Payplug|null $payplug
* @param $is_hosted_field
* @return mixed|void
* @throws Payplug\Exception\ConfigurationNotSetException
* @throws Payplug\Exception\ConnectionException
* @throws Payplug\Exception\HttpException
* @throws Payplug\Exception\UndefinedAttributeException
* @throws Payplug\Exception\UnexpectedAPIResponseException
*/
public static function retrieve($paymentId, $payplug = null)
public static function authorize($data, Payplug\Payplug $payplug = null, $is_hosted_field = false)
{
if ($payplug === null) {
$payplug = Payplug\Payplug::getDefaultConfiguration();
}

if (!$paymentId) {
if (empty($data)) {
throw new Payplug\Exception\UndefinedAttributeException('The parameter paymentId is not set.');
}

$httpClient = new Payplug\Core\HttpClient($payplug);
$response = $httpClient->get(
Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::PAYMENT_RESOURCE, $paymentId)
);
if ($is_hosted_field) {
$response = $httpClient->post(
Payplug\Core\APIRoutes::$HOSTED_FIELDS_RESOURCE,
$data,
false
);
return $response['httpResponse'];
}
}
/**
* @param $data
* @param Payplug\Payplug|null $payplug
* @param $is_hosted_field
* @return array|Payment
* @throws Payplug\Exception\ConfigurationNotSetException
* @throws Payplug\Exception\ConnectionException
* @throws Payplug\Exception\HttpException
* @throws Payplug\Exception\UndefinedAttributeException
* @throws Payplug\Exception\UnexpectedAPIResponseException
*/
public static function retrieve($data, Payplug\Payplug $payplug = null, $is_hosted_field = false)
{
if ($payplug === null) {
$payplug = Payplug\Payplug::getDefaultConfiguration();
}

if (empty($data)) {
throw new Payplug\Exception\UndefinedAttributeException('The parameter $data is not set.');
}

$httpClient = new Payplug\Core\HttpClient($payplug);
if ($is_hosted_field) {
$response = $httpClient->post(
Payplug\Core\APIRoutes::$HOSTED_FIELDS_RESOURCE_RETRIEVE,
$data,
false
);

$hostedField_resource = new Payplug\Responses\HostedFieldTransactionResource($response['httpResponse']);
$response['httpResponse'] = get_object_vars($hostedField_resource);

}else{
$response = $httpClient->get(
Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::PAYMENT_RESOURCE, $data)
);
}

return Payment::fromAttributes($response['httpResponse']);
}
Expand Down Expand Up @@ -234,6 +277,15 @@ public static function create(array $data, $payplug = null)
}

$httpClient = new Payplug\Core\HttpClient($payplug);
if ((isset($data['params']['HFTOKEN']) && $data['params']['HFTOKEN']) || (isset($data['params']['ALIAS']) && $data['params']['ALIAS']))
{
$response = $httpClient->post(
Payplug\Core\APIRoutes::$HOSTED_FIELDS_RESOURCE,
$data,
false
);
return $response['httpResponse'];
}
$response = $httpClient->post(
Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::PAYMENT_RESOURCE),
$data
Expand All @@ -242,6 +294,7 @@ public static function create(array $data, $payplug = null)
return Payment::fromAttributes($response['httpResponse']);
}


/**
* Update a Payment.
*
Expand Down
40 changes: 32 additions & 8 deletions lib/Payplug/Resource/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,55 @@ public static function fromAttributes(array $attributes)
/**
* Creates a refund on a payment.
*
* @param string|Payment $payment the payment id or the payment object
* @param string|Payment $refund_data the payment id or the payment object
* @param array $data API data for refund
* @param Payplug\Payplug $payplug the client configuration
* @param $is_hosted_field
*
* @return null|Refund the refund object
* @throws Payplug\Exception\ConfigurationNotSetException
*/
public static function create($payment, $data = null, $payplug = null)
public static function create($refund_data, array $data = null, Payplug\Payplug $payplug = null, $is_hosted_field = false)
{
if ($payplug === null) {
$payplug = Payplug\Payplug::getDefaultConfiguration();
}
if ($payment instanceof Payment) {
$payment = $payment->id;

// Always resolve $payment from $refund_data
if ($refund_data instanceof Payment) {
$payment = $refund_data->id;
} elseif (is_string($refund_data)) {
$payment = $refund_data;
} elseif (is_array($refund_data) && isset($refund_data['id'])) {
$payment = $refund_data['id'];
} else {
throw new \InvalidArgumentException('A valid payment id or Payment object must be provided.');
}

$httpClient = new Payplug\Core\HttpClient($payplug);
$response = $httpClient->post(
Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::REFUND_RESOURCE, null, array('PAYMENT_ID' => $payment)),
$data
);
if ($is_hosted_field){
$response = $httpClient->post(
Payplug\Core\APIRoutes::$HOSTED_FIELDS_RESOURCE,
$refund_data,
false
);
$hostedField_resource = new Payplug\Responses\HostedFieldRefundTransaction($response['httpResponse']);
$response['httpResponse'] = get_object_vars($hostedField_resource);
}else {
$response = $httpClient->post(
Payplug\Core\APIRoutes::getRoute(
Payplug\Core\APIRoutes::REFUND_RESOURCE,
null,
array('PAYMENT_ID' => $payment)
),
$data
);
}

return Refund::fromAttributes($response['httpResponse']);
}


/**
* Retrieves a refund object on a payment.
*
Expand Down
Loading
Loading