<commit_before><?php
namespace Braghetto\Midas\Payers;

use Braghetto\Midas\Interfaces\Payable;
use MP;

/**
* MercadoPago
*/

class MercadoPago extends AbstractPayer implements Payable
{
    private $client;

    private $payment_methods = [
        '101' => 'visa'
    ];

    private $status = [
        'approved' => '1',
        'pending' => '2',
        'authorized' => 'authorized',
        'in_process' => 'in_process',
        'in_mediation' => 'in_mediation',
        'rejected' => '4',
        'cancelled' => '5',
        'refunded' => 'refunded',
        'charged_back' => 'charged_back',
    ];

    // protected $attributes;

    protected $parsedOrder;

    // protected $items;

    // protected $parsedItems;

    // protected $customer;

    // protected $parsedCustomer;

    // protected $address;

    // protected $parsedAddress;

    protected $validationFields = [
        'order' => [
            'id',
            'total',
            'installments',
        ],
        'payment' => [
            'method',
        ],
        'items' => [
            [
                'id',
                'name',
                'image_url',
                'description',
                'quantity',
                'price',
            ]
        ],
        'shipping_address' => [
            'street',
            'number',
            'zip_code',
        ],
        'customer' => [
            'name',
            'last_name',
            'email',
            'phones' => [
                ['area_code', 'number'],
            ],
            'created_at',
        ],
    ];

    public function __construct($client_id, $client_secret = null)
    {
        $this->client = new MP($client_id, $client_secret);
    }

    public function fill(array $data)
    {
        if (!$this->validate($this->validationFields, $data)) {
            return false;
        }
        $this->parsedOrder = [
            'transaction_amount' => $data['order']['total'],
            'installments' => $data['order']['installments'],
            'payment_method_id' => $this->payment_methods[$data['payment']['method']],
            'payer' => [
                'email' => $data['customer']['email'],
            ],
            'external_reference' => $data['order']['id'],
            'additional_info' => [
                'items' => $this->parseItems($data['items']),
                'payer' => [
                    'first_name' => $data['customer']['name'],
                    'last_name' => $data['customer']['last_name'],
                    'registration_date' => $data['customer']['created_at'],
                    'phone' => [
                        'area_code' => $data['customer']['phones'][0]['area_code'],
                        'number' => $data['customer']['phones'][0]['number'],
                    ],
                    'address' => [
                        'street_name' => $data['shipping_address']['street'],
                        'street_number' => $data['shipping_address']['number'],
                        'zip_code' => $data['shipping_address']['zip_code'],
                    ],
                ],
                'shipments' => [
                    'receiver_address' => [
                        'street_name' => $data['shipping_address']['street'],
                        'street_number' => $data['shipping_address']['number'],
                        'zip_code' => $data['shipping_address']['zip_code'],
                    ],
                ],
            ],
        ];

        if (!empty($data['vendor'])) {
            $this->parsedOrder = array_merge($this->parsedOrder, $data['vendor']);
        }
    }

    public function pay()
    {
        $payment = $this->client->post('/v1/payments', $this->parsedOrder);
        foreach ($payment['reponse']['fee_details'] as $fee) {
            if ($fee['type'] == 'mercadopago_fee') {
                $vendor_fee = $fee['amount'];
            }
        }
        if ($payment['status'] == 201) {
            return [
                'vendor_id' => $payment['reponse']['id'],
                'status' => $this->status[$payment['reponse']['status']],
                'order_total' => $payment['reponse']['transaction_details']['total_paid_amount'],
                'received_amount' => $payment['reponse']['transaction_details']['net_received_amount'],
                'total_paid' => $payment['reponse']['transaction_details']['total_paid_amount'],
                'installments' => $payment['reponse']['installments'],
                'installment_value' => $payment['reponse']['transaction_details']['installment_amount'],
                'vendor_fee' => $vendor_fee,

                'http_status' => $payment['status'],
                'response' => $payment['reponse']
            ];
        }
        return false;
    }

    protected function parseItems(array $items)
    {
        $parsed = [];
        foreach ($items as $item) {
            $parsed[] = [
                'id' => $item['id'],
                'title' => $item['name'],
                'picture_url' => $item['image_url'],
                'description' => $item['description'],
                'quantity' => $item['quantity'],
                'unit_price' => $item['price'],
            ];
        }
        return $parsed;
    }
}
<commit_msg>MP Issues<commit_after><?php
namespace Braghetto\Midas\Payers;

use Braghetto\Midas\Interfaces\Payable;
use MP;

/**
* MercadoPago
*/

class MercadoPago extends AbstractPayer implements Payable
{
    private $client;

    private $payment_methods = [
        '101' => 'visa'
    ];

    private $status = [
        'approved' => '1',
        'pending' => '2',
        'authorized' => 'authorized',
        'in_process' => 'in_process',
        'in_mediation' => 'in_mediation',
        'rejected' => '4',
        'cancelled' => '5',
        'refunded' => 'refunded',
        'charged_back' => 'charged_back',
    ];

    // protected $attributes;

    protected $parsedOrder;

    // protected $items;

    // protected $parsedItems;

    // protected $customer;

    // protected $parsedCustomer;

    // protected $address;

    // protected $parsedAddress;

    protected $validationFields = [
        'order' => [
            'id',
            'total',
            'installments',
        ],
        'payment' => [
            'method',
        ],
        'items' => [
            [
                'id',
                'name',
                'image_url',
                'description',
                'quantity',
                'price',
            ]
        ],
        'shipping_address' => [
            'street',
            'number',
            'zip_code',
        ],
        'customer' => [
            'name',
            'last_name',
            'email',
            'phones' => [
                ['area_code', 'number'],
            ],
        ],
    ];

    public function __construct($client_id, $client_secret = null, $sandbox = true)
    {
        if (isset($client_secret)) {
            $this->client = new MP($client_id, $client_secret);
        } else {
            $this->client = new MP($client_id);
        }
        $this->client->sandbox_mode($sandbox);
    }

    public function fill(array $data)
    {
        if (!$this->validate($this->validationFields, $data)) {
            return false;
        }
        $this->parsedOrder = [
            'transaction_amount' => $data['order']['total'],
            'installments' => $data['order']['installments'],
            'payment_method_id' => $this->payment_methods[$data['payment']['method']],
            'payer' => [
                'email' => $data['customer']['email'],
            ],
            'external_reference' => $data['order']['id'],
            'additional_info' => [
                'items' => $this->parseItems($data['items']),
                'payer' => [
                    'first_name' => $data['customer']['name'],
                    'last_name' => $data['customer']['last_name'],
                    'phone' => [
                        'area_code' => $data['customer']['phones'][0]['area_code'],
                        'number' => $data['customer']['phones'][0]['number'],
                    ],
                    'address' => [
                        'street_name' => $data['shipping_address']['street'],
                        'street_number' => $data['shipping_address']['number'],
                        'zip_code' => $data['shipping_address']['zip_code'],
                    ],
                ],
                'shipments' => [
                    'receiver_address' => [
                        'street_name' => $data['shipping_address']['street'],
                        'street_number' => $data['shipping_address']['number'],
                        'zip_code' => $data['shipping_address']['zip_code'],
                    ],
                ],
            ],
        ];

        if (!empty($data['vendor'])) {
            $this->parsedOrder = array_merge($this->parsedOrder, $data['vendor']);
        }
        return true;
    }

    public function pay()
    {
        $payment = $this->client->post('/v1/payments', $this->parsedOrder);
        $response = $payment['response'];
        foreach ($response['fee_details'] as $fee) {
            if ($fee['type'] == 'mercadopago_fee') {
                $vendor_fee = $fee['amount'];
            }
        }
        if ($payment['status'] == 201) {
            return [
                'vendor_id' => $response['id'],
                'status' => $this->status[$response['status']],
                'order_total' => $response['transaction_details']['total_paid_amount'],
                'received_amount' => $response['transaction_details']['net_received_amount'],
                'total_paid' => $response['transaction_details']['total_paid_amount'],
                'installments' => $response['installments'],
                'installment_value' => $response['transaction_details']['installment_amount'],
                'vendor_fee' => $vendor_fee,

                'http_status' => $payment['status'],
                'response' => $response
            ];
        }
        return false;
    }

    protected function parseItems(array $items)
    {
        $parsed = [];
        foreach ($items as $item) {
            $parsed[] = [
                'id' => $item['id'],
                'title' => $item['name'],
                'picture_url' => $item['image_url'],
                'description' => $item['description'],
                'quantity' => $item['quantity'],
                'unit_price' => $item['price'],
            ];
        }
        return $parsed;
    }
}

{'fraction_non_alphanumeric': 0.1711525837006471, 'fraction_numerical': 0.002438338178748945, 'mean_word_length': 1.5827076774037296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7558601', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2841, 'n_words': 437}