|
<?php |
|
/** |
|
* MercadoPago php SDK install: |
|
* composer require "mercadopago/dx-php" |
|
* |
|
* Mercadopago Checkout Pro documentation: |
|
* https://www.mercadopago.com.uy/developers/es/guides/online-payments/checkout-pro/introduction |
|
* |
|
*/ |
|
|
|
/** |
|
* Class MpWrapper |
|
*/ |
|
class MpWrapper { |
|
const ClientId = ''; |
|
const ClientSecret = ''; |
|
const PublicKey = ''; |
|
const AccessToken = ''; |
|
const back_url_success = ''; |
|
const back_url_failure = ''; |
|
const back_url_pending = ''; |
|
const Statement_Descriptor = ''; |
|
private $preference; |
|
private array $items = []; |
|
|
|
public function __construct() { |
|
MercadoPago\SDK::setAccessToken(self::AccessToken); |
|
$this->makePreference(); |
|
} |
|
|
|
public function makePreference(): MpWrapper { |
|
$this->preference = new MercadoPago\Preference(); |
|
$this->preference->back_urls = [ |
|
'success' => self::back_url_success, |
|
'failure' => self::back_url_failure, |
|
'pending' => self::back_url_pending |
|
]; |
|
$this->preference->auto_return = 'all'; |
|
$this->preference->statement_descriptor = self::Statement_Descriptor; |
|
return $this; |
|
} |
|
|
|
public function addItem(array $ItemRow) { |
|
$item = new MercadoPago\Item(); |
|
$item->title = $ItemRow['ProductName']; |
|
$item->quantity = $ItemRow['ProductQuantity']; |
|
$item->unit_price = $ItemRow['UnitPrice']; |
|
$item->currency_id = $ItemRow['CurrencyIso3']; |
|
$item->picture_url = $ItemRow['UrlProductImage']; |
|
$this->items[] = $item; |
|
} |
|
|
|
public function addShippingCost( |
|
float $Amount, |
|
string $title = 'Shipping Cost', |
|
string $CurrencyId = 'UYU' |
|
): MpWrapper { |
|
$item = new MercadoPago\Item(); |
|
$item->title = $title; |
|
$item->quantity = 1; |
|
$item->unit_price = $Amount; |
|
$item->currency_id = $CurrencyId; |
|
$this->items[] = $item; |
|
return $this; |
|
} |
|
|
|
public function setDiscount(float $amount): MpWrapper { |
|
$this->preference->coupon_amount = $amount; |
|
return $this; |
|
} |
|
|
|
public function saveItems(): MpWrapper { |
|
$this->preference->items = $this->items; |
|
return $this; |
|
} |
|
|
|
public function generatePayer(array $User): MpWrapper { |
|
$payer = new MercadoPago\Payer(); |
|
$payer->type = 'customer'; |
|
$payer->id = $User['id']; |
|
$payer->name = $User['Name']; |
|
$payer->surname = $User['Surname']; |
|
$payer->email = $User['Email']; |
|
$payer->date_created = date(DATE_ATOM); |
|
$payer->phone = [ |
|
'area_code' => '', |
|
'number' => $User['Phone'] |
|
]; |
|
$payer->identification = [ |
|
'type' => 'DNI', |
|
'number' => $User['DocumentNumber'] |
|
]; |
|
$this->preference->payer = $payer; |
|
return $this; |
|
} |
|
|
|
/** |
|
* @throws Exception |
|
*/ |
|
public function PreferenceSave(): bool { |
|
return $this->preference->save(); |
|
} |
|
|
|
public function getPreferenceId():string { |
|
return $this->preference->id; |
|
} |
|
|
|
public function getPreferenceError(): \MercadoPago\RecuperableError{ |
|
return $this->preference->Error(); |
|
} |
|
} |