Skip to content

Commit

Permalink
Merge pull request #95 from Shoperti/fix-mercadopago-and-paypal-events
Browse files Browse the repository at this point in the history
Fix mercadopago and paypal events
  • Loading branch information
joecohens authored Oct 12, 2017
2 parents 9591842 + faac016 commit 18306f8
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/Gateways/MercadoPago/MercadoPagoGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function mapResponse($success, $response)
}

/**
* Map OpenPay response to status object.
* Map MercadoPago response to status object.
*
* @param array $response
*
Expand All @@ -189,25 +189,26 @@ protected function getStatus(array $response)
switch ($status = Arr::get($response, 'status', 'paid')) {
case 'authorized':
case 'refunded':
case 'partially_refunded':
case 'charged_back':
return new Status($status);
case 'approved':
case 'closed':
return new Status('paid');
case 'pending':
case 'in_process':
case 'in_mediation':
case 'open':
return new Status('pending');
case 'cancelled':
return new Status('canceled');
case 'rejected':
return new Status('failed');
default:
return new Status('pending');
}
}

/**
* Map OpenPay response to error code object.
* Map MercadoPago response to error code object.
*
* @param array $response
*
Expand Down
23 changes: 20 additions & 3 deletions src/Gateways/MercadoPagoBasic/Charges.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,26 @@ public function refund($amount, $reference, array $options = [])
*/
protected function addPaymentMethod(array $params, $payment, array $options)
{
$params['installments'] = isset($options['monthly_installments']) && in_array($options['monthly_installments'], [3, 6, 9, 12])
? (int) Arr::get($options, 'monthly_installments')
: 1;
$params['payment_methods'] = [];

if (isset($options['monthly_installments'])) {
$params['payment_methods']['installments'] = in_array($options['monthly_installments'], [3, 6, 9, 12])
? (int) Arr::get($options, 'monthly_installments')
: 1;
}

if (isset($options['enabled_brands'])) {
$paymentTypes = ['prepaid_card', 'digital_currency', 'credit_card', 'debit_card', 'ticket', 'atm', 'bank_transfer'];
$enabledTypes = Arr::get($options, 'enabled_brands', []);

$excludedTypes = array_map(function ($type) {
return ['id' => $type];
}, array_values(array_filter($paymentTypes, function ($type) use ($enabledTypes) {
return !in_array($type, $enabledTypes);
})));

$params['payment_methods']['excluded_payment_types'] = $excludedTypes;
}

return $params;
}
Expand Down
76 changes: 73 additions & 3 deletions src/Gateways/MercadoPagoBasic/MercadoPagoBasicGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function mapResponse($success, $response)
'isRedirect' => $response['isRedirect'],
'success' => $response['isRedirect'] ? false : $success,
'reference' => $success ? Arr::get($response, 'id') : null,
'message' => $success ? 'Transaction approved' : 'Redirect',
'message' => 'Redirect',
'test' => $this->config['test'],
'authorization' => $success ? Arr::get($response, 'init_point') : null,
'status' => $success ? new Status('pending') : new Status('failed'),
Expand All @@ -166,16 +166,86 @@ public function mapResponse($success, $response)
return (new Response())->setRaw($rawResponse)->map([
'isRedirect' => false,
'success' => $success,
'reference' => $success ? Arr::get($response, 'id') : null,
'reference' => $success ? $this->getReference($response) : null,
'message' => $success ? 'Transaction approved' : null,
'test' => $this->config['test'],
'authorization' => $success ? Arr::get($response, 'preference_id') : null,
'authorization' => $success ? Arr::get($response, 'id') : null,
'status' => $success ? $this->getStatus($response) : new Status('failed'),
'errorCode' => $success ? null : $this->getErrorCode($response),
'type' => Arr::get($response, 'topic'),
]);
}

/**
* Get MercadoPago authorization.
*
* @param array $response
*
* @return string|null
*/
protected function getReference(array $response)
{
$payments = Arr::get($response, 'payments');

if (!$payments) {
return Arr::get($response, 'preference_id');
}

$lastPayment = end($payments);

return Arr::get($lastPayment, 'id');
}

/**
* Map MercadoPago response to status object.
*
* @param array $response
*
* @return \Shoperti\PayMe\Status|null
*/
protected function getStatus(array $response)
{
$payments = Arr::get($response, 'payments');

if (!$payments) {
return parent::getStatus($response);
}

$newResponse = $response;

if (count($payments) > 1) {
$totalPaid = 0;
$totalRefund = 0;
$total = $newResponse['shipping_cost'] + $newResponse['total_amount'];

foreach ($payments as $payment) {
if ($payment['status'] === 'approved') {
// Get the total paid amount, considering only approved incomings.
$totalPaid += $payment['total_paid_amount'] - $payment['amount_refunded'];
} elseif ($payment['status'] === 'refunded') {
// Get the total refunded amount.
$totalRefund += $payment['amount_refunded'];
}
}

if ($totalPaid >= $total) {
$newResponse['status'] = 'approved';
} elseif ($totalRefund >= $total) {
$newResponse['status'] = 'refunded';
} elseif ($totalRefund > 0) {
$newResponse['status'] = 'partially_refunded';
} else {
$newResponse['status'] = 'pending';
}

return parent::getStatus($newResponse);
}

$newResponse['status'] = $payments[0]['amount_refunded'] > 0 ? 'partially_refunded' : $payments[0]['status'];

return parent::getStatus($newResponse);
}

/**
* Get the request url.
*
Expand Down
42 changes: 38 additions & 4 deletions src/Gateways/PaypalExpress/PaypalExpressGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,20 @@ public function mapResponse($success, $response)
'isRedirect' => false,
'success' => $success ? true : false,
'reference' => $success ? Arr::get($response, 'invoice') : false,
'message' => $success ? 'Transaction approved' : 'Transaction failed',
'message' => $success ? 'VERIFIED' : 'INVALID',
'test' => $this->config['test'],
'authorization' => $success ? Arr::get($response, 'txn_id') : '',
'status' => $success ? new Status('paid') : new Status('failed'),
'status' => $success ? $this->getPaymentStatus($response) : new Status('failed'),
'errorCode' => null,
'type' => $success ? Arr::get($response, 'payment_status') : null,
'type' => $success ? Arr::get($response, 'txn_type') : null,
]);
}

return (new Response())->setRaw($rawResponse)->map([
'isRedirect' => $response['isRedirect'],
'success' => $response['isRedirect'] ? false : $success,
'reference' => $success ? $this->getReference($response, $response['isRedirect']) : false,
'message' => $success ? 'Transaction approved' : $response['L_LONGMESSAGE0'],
'message' => $success ? Arr::get($response, 'ACK', 'Transaction approved') : $response['L_LONGMESSAGE0'],
'test' => $this->config['test'],
'authorization' => $this->getAuthorization($response, $success, $response['isRedirect']),
'status' => $success ? $this->getStatus($response, $response['isRedirect']) : new Status('failed'),
Expand Down Expand Up @@ -311,6 +311,40 @@ protected function getStatus($response, $isRedirect)
return new Status('paid');
}

/**
* Map PayPal payment response to status object.
*
* @param array $response
*
* @return \Shoperti\PayMe\Status
*/
protected function getPaymentStatus($response)
{
switch ($status = Arr::get($response, 'payment_status', 'paid')) {
case 'Completed':
case 'Processed':
return new Status('paid');
case 'Created':
case 'Pending':
return new Status('pending');
case 'Canceled_Reversal':
return new Status('canceled');
case 'Failed':
case 'Denied':
return new Status('failed');
case 'Declined':
return new Status('declined');
case 'Expired':
return new Status('expired');
case 'Refunded':
return new Status('refunded');
case 'Reversed':
return new Status('charged_back');
case 'Voided':
return new Status('voided');
}
}

/**
* Map PayPalExpress response to error code object.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Status
'canceled',
'trial',
'charged_back',
'expired',
'declined',
];

/**
Expand Down

0 comments on commit 18306f8

Please sign in to comment.