Skip to content

Commit

Permalink
Merge pull request #67 from Shoperti/new-conekta-api
Browse files Browse the repository at this point in the history
New conekta api
  • Loading branch information
joecohens authored Mar 28, 2017
2 parents bdede9f + 1d6e24d commit c4e1f9b
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 130 deletions.
7 changes: 4 additions & 3 deletions src/Gateways/Conekta/Cards.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class Cards extends AbstractApi implements CardInterface
public function create($creditcard, $options = [])
{
if (isset($options['customer'])) {
$params['token'] = $creditcard;
$params['type'] = 'card';
$params['token_id'] = $creditcard;

return $this->gateway->commit('post', $this->gateway->buildUrlFromString('customers/'.$options['customer'].'/cards'), $params);
return $this->gateway->commit('post', $this->gateway->buildUrlFromString('customers/'.$options['customer'].'/payment_sources'), $params);
} else {
$params['email'] = Arr::get($options, 'email');
$params['name'] = Arr::get($options, 'name');
Expand All @@ -47,7 +48,7 @@ public function create($creditcard, $options = [])
public function delete($id, $options = [])
{
if (isset($options['card_id'])) {
return $this->gateway->commit('delete', $this->gateway->buildUrlFromString('customers/'.$id.'/cards/'.$options['card_id']));
return $this->gateway->commit('delete', $this->gateway->buildUrlFromString('customers/'.$id.'/payment_sources/'.$options['card_id']));
} else {
return $this->gateway->commit('delete', $this->gateway->buildUrlFromString('customers/'.$id));
}
Expand Down
185 changes: 101 additions & 84 deletions src/Gateways/Conekta/Charges.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public function create($amount, $payment, $options = [])
$params = [];

$params = $this->addOrder($params, $amount, $options);
$params = $this->addPaymentMethod($params, $payment, $options);
$params = $this->addPaymentMethod($params, $amount, $payment, $options);
$params = $this->addOrderDetails($params, $options);

if (isset($options['monthly_installments']) && in_array($options['monthly_installments'], [3, 6, 9, 12])) {
$params['monthly_installments'] = $options['monthly_installments'];
}

return $this->gateway->commit('post', $this->gateway->buildUrlFromString('charges'), $params);
return $this->gateway->commit('post', $this->gateway->buildUrlFromString('orders'), $params);
}

/**
Expand All @@ -62,28 +62,29 @@ public function complete($options = [])
*/
public function refund($amount, $reference, array $options = [])
{
$url = sprintf($this->gateway->buildUrlFromString('charges').'/%s/refund', $reference);
$params = [];
$params['amount'] = $this->gateway->amount($amount);
$params = $this->addLineItems($params, $options);

return $this->gateway->commit('post', $url, [
'amount' => $this->gateway->amount($amount),
]);
$url = sprintf($this->gateway->buildUrlFromString('orders').'/%s/refund', $reference);

return $this->gateway->commit('post', $url, $options);
}

/**
* Add order params to request.
*
* @param string[] $params
* @param int $money
* @param int $amount
* @param string[] $options
*
* @return array
*/
protected function addOrder(array $params, $money, array $options)
protected function addOrder(array $params, $amount, array $options)
{
$params['description'] = Helper::ascii(Arr::get($options, 'description', 'PayMe Purchase'));
$params['reference_id'] = Arr::get($options, 'reference');
$params['metadata'] = ['reference' => Arr::get($options, 'reference')];
$params['currency'] = Arr::get($options, 'currency', $this->gateway->getCurrency());
$params['amount'] = $this->gateway->amount($money);
$params['amount'] = (int) $this->gateway->amount($amount);

return $params;
}
Expand All @@ -92,35 +93,46 @@ protected function addOrder(array $params, $money, array $options)
* Add payment method to request.
*
* @param string[] $params
* @param int $amount
* @param mixed $payment
* @param string[] $options
*
* @return array
*/
protected function addPaymentMethod(array $params, $payment, array $options)
protected function addPaymentMethod(array $params, $amount, $payment, array $options)
{
if (is_string($payment)) {
if ($payment == 'spei') {
$params['bank']['type'] = 'spei';
$params['bank']['expires_at'] = Arr::get($options, 'expires', date('Y-m-d', time() + 172800));
$params['charges'][0]['payment_method']['type'] = 'spei';
$params['charges'][0]['payment_method']['expires_at'] = Arr::get($options, 'expires', strtotime(date('Y-m-d H:i:s')) + 172800);
} elseif ($payment == 'oxxo') {
$params['cash']['type'] = 'oxxo';
$params['cash']['expires_at'] = Arr::get($options, 'expires', date('Y-m-d', time() + 172800));
$params['charges'][0]['payment_method']['type'] = 'oxxo';
$params['charges'][0]['payment_method']['expires_at'] = Arr::get($options, 'expires', strtotime(date('Y-m-d H:i:s')) + 172800);
} elseif ($payment == 'oxxo_cash') {
$params['charges'][0]['payment_method']['type'] = 'oxxo_cash';
$params['charges'][0]['payment_method']['expires_at'] = Arr::get($options, 'expires', strtotime(date('Y-m-d H:i:s')) + 36000);
} elseif (Helper::startsWith($payment, 'cus_')) {
$params['customer_info']['customer_id'] = $payment;
$params['charges'][0]['payment_method']['type'] = 'default';
} elseif (Helper::startsWith($payment, 'payee_')) {
$params['payee_id'] = $payment;
$params['charges'][0]['payment_method']['type'] = 'payout';
$params['charges'][0]['payment_method']['payee_id'] = $payment;
} else {
$params['card'] = $payment;
$params['charges'][0]['payment_method']['type'] = 'card';
$params['charges'][0]['payment_method']['token_id'] = $payment;
}
} elseif ($payment instanceof CreditCard) {
$params['card'] = [];
$params['card']['name'] = $payment->getName();
$params['card']['cvc'] = $payment->getCvv();
$params['card']['number'] = $payment->getNumber();
$params['card']['exp_month'] = $payment->getExpiryMonth();
$params['card']['exp_year'] = $payment->getExpiryYear();
$params['card'] = $this->addAddress($params['card'], $options);
$params['charges'][0]['payment_method']['card'] = [];
$params['charges'][0]['payment_method']['card']['name'] = $payment->getName();
$params['charges'][0]['payment_method']['card']['cvc'] = $payment->getCvv();
$params['charges'][0]['payment_method']['card']['number'] = $payment->getNumber();
$params['charges'][0]['payment_method']['card']['exp_month'] = $payment->getExpiryMonth();
$params['charges'][0]['payment_method']['card']['exp_year'] = $payment->getExpiryYear();
$params['charges'][0]['payment_method']['card'] = $this->addAddress($params['charges'][0]['source']['card'], $options);
}

$params['charges'][0]['amount'] = (int) $this->gateway->amount($amount);

return $params;
}

Expand All @@ -135,14 +147,20 @@ protected function addPaymentMethod(array $params, $payment, array $options)
protected function addAddress(array $params, array $options)
{
if ($address = Arr::get($options, 'address') ?: Arr::get($options, 'billing_address')) {
$params['address'] = [];
$params['address']['street1'] = Arr::get($address, 'address1');
$params['address']['street2'] = Arr::get($address, 'address2');
$params['address']['street3'] = Arr::get($address, 'address3');
if ($address2 = Arr::get($address, 'address2')) {
$params['address']['street2'] = $address2;
}
if ($address3 = Arr::get($address, 'address3')) {
$params['address']['street3'] = $address3;
}
if ($externalNumber = Arr::get($address, 'external_number')) {
$params['address']['external_number'] = $externalNumber;
}
$params['address']['city'] = Arr::get($address, 'city');
$params['address']['country'] = Arr::get($address, 'country');
$params['address']['state'] = Arr::get($address, 'state');
$params['address']['zip'] = Arr::get($address, 'zip');
$params['address']['postal_code'] = Arr::get($address, 'zip');

return $params;
}
Expand All @@ -159,48 +177,24 @@ protected function addAddress(array $params, array $options)
protected function addOrderDetails(array $params, array $options)
{
if (isset($options['name'])) {
$params['details']['name'] = Arr::get($options, 'name', '');
$params['customer_info']['name'] = Arr::get($options, 'name', '');
}

if (isset($options['email'])) {
$params['details']['email'] = Arr::get($options, 'email', '');
$params['customer_info']['email'] = Arr::get($options, 'email', '');
}

if (isset($options['phone'])) {
$params['details']['phone'] = Arr::get($options, 'phone', '');
$params['customer_info']['phone'] = Arr::get($options, 'phone', '');
}

$params = $this->addCustomer($params, $options);
$params = $this->addLineItems($params, $options);
$params = $this->addBillingAddress($params, $options);
$params = $this->addShippingAddress($params, $options);

return $params;
}

/**
* Add customer to request.
*
* @param string[] $params
* @param string[] $options
*
* @return array
*/
protected function addCustomer(array $params, array $options)
{
if ($customer = Arr::get($options, 'customer')) {
$params['details']['customer'] = [];
$params['details']['customer']['logged_in'] = Arr::get($customer, 'logged_in');
$params['details']['customer']['successful_purchases'] = Arr::get($customer, 'successful_purchases');
$params['details']['customer']['created_at'] = Arr::get($customer, 'created_at');
$params['details']['customer']['updated_at'] = Arr::get($customer, 'updated_at');
$params['details']['customer']['offline_payments'] = Arr::get($customer, 'offline_payments');
$params['details']['customer']['score'] = Arr::get($customer, 'score');
}

return $params;
}

/**
* Add order line items param.
*
Expand All @@ -211,17 +205,17 @@ protected function addCustomer(array $params, array $options)
*/
protected function addLineItems(array $params, array $options)
{
$params['details']['line_items'] = [];

if (isset($options['line_items']) && is_array($options['line_items'])) {
foreach ($options['line_items'] as $lineItem) {
$params['details']['line_items'][] = [
$params['line_items'][] = [
'name' => Arr::get($lineItem, 'name'),
'description' => Arr::get($lineItem, 'description'),
'unit_price' => $this->gateway->amount(Arr::get($lineItem, 'unit_price')),
'unit_price' => (int) $this->gateway->amount(Arr::get($lineItem, 'unit_price')),
'quantity' => Arr::get($lineItem, 'quantity', 1),
'sku' => Arr::get($lineItem, 'sku'),
'category' => Arr::get($lineItem, 'category'),
'type' => Arr::get($lineItem, 'type', 'physical'),
'tags' => ['none'],
];
}
}
Expand All @@ -239,19 +233,25 @@ protected function addLineItems(array $params, array $options)
*/
protected function addBillingAddress(array $params, array $options)
{
if ($address = Arr::get($options, 'billing_address')) {
$params['details']['billing_address'] = [];
$params['details']['billing_address']['street1'] = Arr::get($address, 'address1');
$params['details']['billing_address']['street2'] = Arr::get($address, 'address2');
$params['details']['billing_address']['street3'] = Arr::get($address, 'address3');
$params['details']['billing_address']['city'] = Arr::get($address, 'city');
$params['details']['billing_address']['country'] = Arr::get($address, 'country');
$params['details']['billing_address']['state'] = Arr::get($address, 'state');
$params['details']['billing_address']['zip'] = Arr::get($address, 'zip');
$params['details']['billing_address']['tax_id'] = Arr::get($address, 'tax_id');
$params['details']['billing_address']['company_name'] = Arr::get($address, 'company_name');
$params['details']['billing_address']['phone'] = Arr::get($address, 'phone');
$params['details']['billing_address']['email'] = Arr::get($address, 'email');
if ($address = Arr::get($options, 'billing_address') && $taxId = Arr::get($address, 'tax_id') && $companyName = Arr::get($address, 'company_name')) {
$params['fiscal_entity']['address']['street1'] = Arr::get($address, 'address1');
if ($address2 = Arr::get($address, 'address2')) {
$params['fiscal_entity']['address']['street2'] = $address2;
}
if ($address3 = Arr::get($address, 'address3')) {
$params['fiscal_entity']['address']['street3'] = $address3;
}
if ($externalNumber = Arr::get($address, 'external_number')) {
$params['fiscal_entity']['address']['external_number'] = $externalNumber;
}
$params['fiscal_entity']['address']['city'] = Arr::get($address, 'city');
$params['fiscal_entity']['address']['country'] = Arr::get($address, 'country');
$params['fiscal_entity']['address']['state'] = Arr::get($address, 'state');
$params['fiscal_entity']['address']['postal_code'] = Arr::get($address, 'zip');
$params['fiscal_entity']['phone'] = Arr::get($address, 'phone', Arr::get($options, 'phone', 'none'));
$params['fiscal_entity']['email'] = Arr::get($address, 'email', Arr::get($options, 'email', 'none'));
$params['fiscal_entity']['tax_id'] = $taxId;
$params['fiscal_entity']['company_name'] = $companyName;
}

return $params;
Expand All @@ -268,17 +268,34 @@ protected function addBillingAddress(array $params, array $options)
protected function addShippingAddress(array $params, array $options)
{
if ($address = Arr::get($options, 'shipping_address')) {
$params['details']['shipment'] = [];
$params['details']['shipment']['carrier'] = Arr::get($address, 'carrier');
$params['details']['shipment']['service'] = Arr::get($address, 'service');
$params['details']['shipment']['price'] = Arr::get($address, 'price');
$params['details']['shipment']['address']['street1'] = Arr::get($address, 'address1');
$params['details']['shipment']['address']['street2'] = Arr::get($address, 'address2');
$params['details']['shipment']['address']['street3'] = Arr::get($address, 'address3');
$params['details']['shipment']['address']['city'] = Arr::get($address, 'city');
$params['details']['shipment']['address']['state'] = Arr::get($address, 'state');
$params['details']['shipment']['address']['zip'] = Arr::get($address, 'zip');
$params['details']['shipment']['address']['country'] = Arr::get($address, 'country');
$params['shipping_contact']['address']['street1'] = Arr::get($address, 'address1');
if ($address2 = Arr::get($address, 'address2')) {
$params['shipping_contact']['address']['street2'] = $address2;
}
if ($address3 = Arr::get($address, 'address3')) {
$params['shipping_contact']['address']['street3'] = $address3;
}
if ($externalNumber = Arr::get($address, 'external_number')) {
$params['shipping_contact']['address']['external_number'] = $externalNumber;
}
$params['shipping_contact']['address']['external_number'] = Arr::get($address, 'external_number', '');
$params['shipping_contact']['address']['city'] = Arr::get($address, 'city');
$params['shipping_contact']['address']['state'] = Arr::get($address, 'state');
$params['shipping_contact']['address']['postal_code'] = Arr::get($address, 'zip');
$params['shipping_contact']['address']['country'] = Arr::get($address, 'country');
$params['shipping_contact']['receiver'] = Arr::get($address, 'name', Arr::get($options, 'name', ''));
$params['shipping_contact']['phone'] = Arr::get($address, 'phone', Arr::get($options, 'phone', ''));
$params['shipping_contact']['email'] = Arr::get($address, 'email', Arr::get($options, 'email', ''));

$params['shipping_lines'] = [];
$params['shipping_lines'][0]['description'] = Arr::get($address, 'carrier');
$params['shipping_lines'][0]['carrier'] = Arr::get($address, 'carrier');
$params['shipping_lines'][0]['method'] = Arr::get($address, 'service');
$params['shipping_lines'][0]['amount'] = (int) $this->gateway->amount(Arr::get($address, 'price'));

if ($trackingNumber = Arr::get($address, 'tracking_number')) {
$params['shipping_lines'][0]['tracking_number'] = $trackingNumber;
}
}

return $params;
Expand Down
Loading

0 comments on commit c4e1f9b

Please sign in to comment.