Skip to content

Commit

Permalink
Merge pull request #259 from mercadopago/release/1.9.0
Browse files Browse the repository at this point in the history
Release 1.9.0
  • Loading branch information
delias-silva authored Apr 6, 2020
2 parents cb98165 + 166951d commit 53de8d7
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/MercadoPago/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function set($key, $value)
if ($key == "ACCESS_TOKEN") {
$user = $this->getUserId($value);
parent::set('USER_ID', $user['id']);
parent::set('COUNTRY_ID', $user['country_id']);
}

if (parent::get('CLIENT_ID') != "" && parent::get('CLIENT_SECRET') != "" && empty(parent::get('ACCESS_TOKEN'))) {
Expand Down
138 changes: 138 additions & 0 deletions src/MercadoPago/Entities/AdvancedPayments/AdvancedPayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php


namespace MercadoPago\AdvancedPayments;

use MercadoPago\Annotation\RestMethod;
use MercadoPago\Annotation\RequestParam;
use MercadoPago\Annotation\Attribute;
use MercadoPago\Entity;

/**
* @RestMethod(resource="/v1/advanced_payments", method="create")
* @RestMethod(resource="/v1/advanced_payments/:id", method="read")
* @RestMethod(resource="/v1/advanced_payments/search", method="search")
* @RestMethod(resource="/v1/advanced_payments/:id", method="update")
* @RestMethod(resource="/v1/advanced_payments/:id/refunds", method="refund")
* @RequestParam(param="access_token")
*/
class AdvancedPayment extends Entity
{

/**
* @var
* @Attribute()
*/
protected $id;

/**
* @var
* @Attribute()
*/
protected $application_id;

/**
* @var
*/
protected $payments;

/**
* @var
*/
protected $disbursements;

/**
* @var
*/
protected $payer;

/**
* @var
*/
protected $external_reference;

/**
* @var
*/
protected $description;

/**
* @var
*/
protected $binary_mode;

/**
* @var
*/
protected $status;

/**
* @var
*/
protected $capture;

/**
* @return mixed
* @throws \Exception
*/
public function cancel() {
$this->status = 'cancelled';

return $this->update();
}

/**
* @return mixed
* @throws \Exception
*/
public function capture()
{
$this->capture = true;

return $this->update();
}

/**
* @param int $amount
* @return bool
* @throws \Exception
*/
public function refund($amount = 0){
$refund = new Refund(["advanced_payment_id" => $this->id]);
if ($amount > 0){
$refund->amount = $amount;
}

if ($refund->save()){
$advanced_payment = self::get($this->id);
$this->_fillFromArray($this, $advanced_payment->toArray());
return true;
}else{
$this->error = $refund->error;
return false;
}
}


/**
* @param $disbursement_id
* @param int $amount
* @return bool
* @throws \Exception
*/
public function refundDisbursement($disbursement_id, $amount = 0){
$refund = new DisbursementRefund(["advanced_payment_id" => $this->id, "disbursement_id" => $disbursement_id]);
if ($amount > 0){
$refund->amount = $amount;
}

if ($refund->save()){
$advanced_payment = self::get($this->id);
$this->_fillFromArray($this, $advanced_payment->toArray());
return true;
}else{
$this->error = $refund->error;
return false;
}
}
}
54 changes: 54 additions & 0 deletions src/MercadoPago/Entities/AdvancedPayments/DisbursementRefund.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace MercadoPago\AdvancedPayments;

use MercadoPago\Annotation\RestMethod;
use MercadoPago\Annotation\RequestParam;
use MercadoPago\Annotation\Attribute;
use MercadoPago\Entity;

/**
* @RestMethod(resource="/v1/advanced_payments/:advanced_payment_id/disbursements/:disbursement_id/refunds", method="create")
* @RestMethod(resource="/v1/advanced_payments/:advanced_payment_id/disbursements/:disbursement_id/refunds/:refund_id", method="read")
* @RequestParam(param="access_token")
*/
class DisbursementRefund extends Entity {

/**
* @Attribute()
*/
protected $id;
/**
* @Attribute(serialize=false)
*/
protected $payment_id;
/**
* @Attribute()
*/
protected $amount;
/**
* @Attribute()
*/
protected $metadata;
/**
* @Attribute()
*/
protected $source;
/**
* @Attribute(readOnly=true)
*/
protected $date_created;

/**
* @Attribute(serialize=false)
*/
protected $advanced_payment_id;

/**
* @Attribute(serialize=false)
*/
protected $disbursement_id;

}

?>
44 changes: 44 additions & 0 deletions src/MercadoPago/Entities/AdvancedPayments/Refund.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MercadoPago\AdvancedPayments;

use MercadoPago\Annotation\RestMethod;
use MercadoPago\Annotation\RequestParam;
use MercadoPago\Annotation\Attribute;
use MercadoPago\Entity;

/**
* @RestMethod(resource="/v1/advanced_payments/:advanced_payment_id/refunds", method="create")
* @RestMethod(resource="/v1/advanced_payments/:advanced_payment_id/refunds/:refund_id", method="read")
* @RequestParam(param="access_token")
*/
class Refund extends Entity {

/**
* @Attribute()
*/
protected $id;
/**
* @Attribute(serialize=false)
*/
protected $payment_id;
/**
* @Attribute()
*/
protected $amount;
/**
* @Attribute()
*/
protected $metadata;
/**
* @Attribute()
*/
protected $source;
/**
* @Attribute(readOnly=true)
*/
protected $date_created;

}

?>
20 changes: 19 additions & 1 deletion src/MercadoPago/Entities/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,23 @@ class OAuth extends Entity
*/
protected $scope;


/**
* @param $app_id
* @param $redirect_uri
* @return string
*/
public function getAuthorizationURL($app_id, $redirect_uri){
return "https://auth.mercadopago.com.br/authorization?client_id=${app_id}&response_type=code&platform_id=mp&redirect_uri=${redirect_uri}";
$county_id = strtolower(SDK::getCountryId());
return "https://auth.mercadopago.com.${county_id}/authorization?client_id=${app_id}&response_type=code&platform_id=mp&redirect_uri=${redirect_uri}";
}

/**
* @param $authorization_code
* @param $redirect_uri
* @return mixed
* @throws \Exception
*/
public function getOAuthCredentials($authorization_code, $redirect_uri){
$this->client_secret = SDK::getAccessToken();
$this->grant_type = 'authorization_code';
Expand All @@ -72,6 +85,11 @@ public function getOAuthCredentials($authorization_code, $redirect_uri){
return $this->save();
}

/**
* @param $refresh_token
* @return mixed
* @throws \Exception
*/
public function refreshOAuthCredentials($refresh_token){
$this->client_secret = SDK::getAccessToken();
$this->grant_type = 'refresh_token';
Expand Down
4 changes: 4 additions & 0 deletions src/MercadoPago/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public static function getAccessToken(){
return self::$_config->get('ACCESS_TOKEN');
}

public static function getCountryId(){
return self::$_config->get('COUNTRY_ID');
}

public static function cleanCredentials(){
if (self::$_config == null) {
// do nothing
Expand Down

0 comments on commit 53de8d7

Please sign in to comment.