2015-09-10 19:50:09 +02:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
use Utils;
|
2016-03-16 00:08:00 +01:00
|
|
|
use Auth;
|
2015-09-10 19:50:09 +02:00
|
|
|
use URL;
|
|
|
|
use DateTime;
|
|
|
|
use Event;
|
2016-04-29 23:50:21 +02:00
|
|
|
use Cache;
|
2015-09-10 19:50:09 +02:00
|
|
|
use Omnipay;
|
|
|
|
use Session;
|
|
|
|
use CreditCard;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Country;
|
2016-03-16 03:07:11 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
2016-04-29 23:50:21 +02:00
|
|
|
use App\Http\Controllers\PaymentController;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Models\AccountGatewayToken;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Services\BaseService;
|
|
|
|
use App\Events\PaymentWasCreated;
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
class PaymentService extends BaseService
|
|
|
|
{
|
2015-09-10 19:50:09 +02:00
|
|
|
public $lastError;
|
2015-11-05 23:37:04 +01:00
|
|
|
protected $datatableService;
|
2016-04-23 22:40:19 +02:00
|
|
|
|
|
|
|
protected static $refundableGateways = array(
|
|
|
|
GATEWAY_STRIPE
|
|
|
|
);
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
public function __construct(PaymentRepository $paymentRepo, AccountRepository $accountRepo, DatatableService $datatableService)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
$this->datatableService = $datatableService;
|
2015-10-28 20:22:07 +01:00
|
|
|
$this->paymentRepo = $paymentRepo;
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
protected function getRepo()
|
|
|
|
{
|
|
|
|
return $this->paymentRepo;
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
public function createGateway($accountGateway)
|
|
|
|
{
|
|
|
|
$gateway = Omnipay::create($accountGateway->gateway->provider);
|
2016-04-19 22:02:46 +02:00
|
|
|
$gateway->initialize((array) $accountGateway->getConfig());
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-10-21 06:30:23 +02:00
|
|
|
if ($accountGateway->isGateway(GATEWAY_DWOLLA)) {
|
2015-09-10 19:50:09 +02:00
|
|
|
if ($gateway->getSandbox() && isset($_ENV['DWOLLA_SANDBOX_KEY']) && isset($_ENV['DWOLLA_SANSBOX_SECRET'])) {
|
|
|
|
$gateway->setKey($_ENV['DWOLLA_SANDBOX_KEY']);
|
|
|
|
$gateway->setSecret($_ENV['DWOLLA_SANSBOX_SECRET']);
|
|
|
|
} elseif (isset($_ENV['DWOLLA_KEY']) && isset($_ENV['DWOLLA_SECRET'])) {
|
|
|
|
$gateway->setKey($_ENV['DWOLLA_KEY']);
|
|
|
|
$gateway->setSecret($_ENV['DWOLLA_SECRET']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gateway;
|
|
|
|
}
|
|
|
|
|
2015-10-21 06:30:23 +02:00
|
|
|
public function getPaymentDetails($invitation, $accountGateway, $input = null)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$account = $invoice->account;
|
|
|
|
$key = $invoice->account_id.'-'.$invoice->invoice_number;
|
|
|
|
$currencyCode = $invoice->client->currency ? $invoice->client->currency->code : ($invoice->account->currency ? $invoice->account->currency->code : 'USD');
|
|
|
|
|
|
|
|
if ($input) {
|
|
|
|
$data = self::convertInputForOmnipay($input);
|
2016-04-11 21:09:05 +02:00
|
|
|
$data['email'] = $invitation->contact->email;
|
2015-09-10 19:50:09 +02:00
|
|
|
Session::put($key, $data);
|
|
|
|
} elseif (Session::get($key)) {
|
|
|
|
$data = Session::get($key);
|
|
|
|
} else {
|
2015-09-20 23:05:02 +02:00
|
|
|
$data = $this->createDataForClient($invitation);
|
2015-09-10 19:50:09 +02:00
|
|
|
}
|
|
|
|
|
2016-04-24 04:10:51 +02:00
|
|
|
$card = !empty($data['number']) ? new CreditCard($data) : null;
|
2015-10-21 06:30:23 +02:00
|
|
|
$data = [
|
2015-09-10 19:50:09 +02:00
|
|
|
'amount' => $invoice->getRequestedAmount(),
|
|
|
|
'card' => $card,
|
|
|
|
'currency' => $currencyCode,
|
|
|
|
'returnUrl' => URL::to('complete'),
|
|
|
|
'cancelUrl' => $invitation->getLink(),
|
|
|
|
'description' => trans('texts.' . $invoice->getEntityType()) . " {$invoice->invoice_number}",
|
2015-09-20 23:05:02 +02:00
|
|
|
'transactionId' => $invoice->invoice_number,
|
|
|
|
'transactionType' => 'Purchase',
|
2015-09-10 19:50:09 +02:00
|
|
|
];
|
2015-10-21 06:30:23 +02:00
|
|
|
|
|
|
|
if ($accountGateway->isGateway(GATEWAY_PAYPAL_EXPRESS) || $accountGateway->isGateway(GATEWAY_PAYPAL_PRO)) {
|
|
|
|
$data['ButtonSource'] = 'InvoiceNinja_SP';
|
|
|
|
};
|
|
|
|
|
2016-05-01 04:45:51 +02:00
|
|
|
if($input && $accountGateway->isGateway(GATEWAY_STRIPE)) {
|
|
|
|
if (!empty($input['stripeToken'])) {
|
|
|
|
$data['token'] = $input['stripeToken'];
|
|
|
|
unset($details['card']);
|
|
|
|
} elseif (!empty($input['plaidPublicToken'])) {
|
|
|
|
$data['plaidPublicToken'] = $input['plaidPublicToken'];
|
|
|
|
$data['plaidAccountId'] = $input['plaidAccountId'];
|
|
|
|
unset($data['card']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 06:30:23 +02:00
|
|
|
return $data;
|
2015-09-10 19:50:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 22:02:22 +02:00
|
|
|
public function convertInputForOmnipay($input)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'firstName' => $input['first_name'],
|
|
|
|
'lastName' => $input['last_name'],
|
2015-11-29 21:13:50 +01:00
|
|
|
'number' => isset($input['card_number']) ? $input['card_number'] : null,
|
|
|
|
'expiryMonth' => isset($input['expiration_month']) ? $input['expiration_month'] : null,
|
|
|
|
'expiryYear' => isset($input['expiration_year']) ? $input['expiration_year'] : null,
|
|
|
|
'cvv' => isset($input['cvv']) ? $input['cvv'] : '',
|
2015-09-10 19:50:09 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if (isset($input['country_id'])) {
|
|
|
|
$country = Country::find($input['country_id']);
|
|
|
|
|
|
|
|
$data = array_merge($data, [
|
|
|
|
'billingAddress1' => $input['address1'],
|
|
|
|
'billingAddress2' => $input['address2'],
|
|
|
|
'billingCity' => $input['city'],
|
|
|
|
'billingState' => $input['state'],
|
|
|
|
'billingPostcode' => $input['postal_code'],
|
|
|
|
'billingCountry' => $country->iso_3166_2,
|
|
|
|
'shippingAddress1' => $input['address1'],
|
|
|
|
'shippingAddress2' => $input['address2'],
|
|
|
|
'shippingCity' => $input['city'],
|
|
|
|
'shippingState' => $input['state'],
|
|
|
|
'shippingPostcode' => $input['postal_code'],
|
|
|
|
'shippingCountry' => $country->iso_3166_2
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-09-20 23:05:02 +02:00
|
|
|
public function createDataForClient($invitation)
|
|
|
|
{
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$client = $invoice->client;
|
|
|
|
$contact = $invitation->contact ?: $client->contacts()->first();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'email' => $contact->email,
|
|
|
|
'company' => $client->getDisplayName(),
|
|
|
|
'firstName' => $contact->first_name,
|
|
|
|
'lastName' => $contact->last_name,
|
|
|
|
'billingAddress1' => $client->address1,
|
|
|
|
'billingAddress2' => $client->address2,
|
|
|
|
'billingCity' => $client->city,
|
|
|
|
'billingPostcode' => $client->postal_code,
|
|
|
|
'billingState' => $client->state,
|
2015-09-29 12:21:57 +02:00
|
|
|
'billingCountry' => $client->country ? $client->country->iso_3166_2 : '',
|
2015-09-20 23:05:02 +02:00
|
|
|
'billingPhone' => $contact->phone,
|
|
|
|
'shippingAddress1' => $client->address1,
|
|
|
|
'shippingAddress2' => $client->address2,
|
|
|
|
'shippingCity' => $client->city,
|
|
|
|
'shippingPostcode' => $client->postal_code,
|
|
|
|
'shippingState' => $client->state,
|
2015-09-29 12:21:57 +02:00
|
|
|
'shippingCountry' => $client->country ? $client->country->iso_3166_2 : '',
|
2015-09-20 23:05:02 +02:00
|
|
|
'shippingPhone' => $contact->phone,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-04-29 23:50:21 +02:00
|
|
|
public function getClientPaymentMethods($client) {
|
|
|
|
$token = $client->getGatewayToken($accountGateway);
|
|
|
|
if (!$token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$gateway = $this->createGateway($accountGateway);
|
|
|
|
|
|
|
|
$paymentMethods = array();
|
|
|
|
if ($accountGateway->gateway_id == GATEWAY_STRIPE) {
|
|
|
|
$response = $gateway->fetchCustomer(array('customerReference' => $token))->send();
|
|
|
|
if (!$response->isSuccessful()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $response->getData();
|
|
|
|
$default_source = $data['default_source'];
|
|
|
|
$sources = isset($data['sources']) ? $data['sources']['data'] : $data['cards']['data'];
|
|
|
|
|
|
|
|
$paymentTypes = Cache::get('paymentTypes');
|
|
|
|
$currencies = Cache::get('currencies');
|
|
|
|
foreach ($sources as $source) {
|
|
|
|
if ($source['object'] == 'bank_account') {
|
|
|
|
$paymentMethods[] = array(
|
|
|
|
'id' => $source['id'],
|
|
|
|
'default' => $source['id'] == $default_source,
|
|
|
|
'type' => $paymentTypes->find(PAYMENT_TYPE_ACH),
|
|
|
|
'currency' => $currencies->where('code', strtoupper($source['currency']))->first(),
|
|
|
|
'last4' => $source['last4'],
|
|
|
|
'routing_number' => $source['routing_number'],
|
|
|
|
'bank_name' => $source['bank_name'],
|
|
|
|
'status' => $source['status'],
|
|
|
|
);
|
|
|
|
} elseif ($source['object'] == 'card') {
|
|
|
|
$paymentMethods[] = array(
|
|
|
|
'id' => $source['id'],
|
|
|
|
'default' => $source['id'] == $default_source,
|
|
|
|
'type' => $paymentTypes->find($this->parseCardType($source['brand'])),
|
|
|
|
'last4' => $source['last4'],
|
|
|
|
'expiration' => $source['exp_year'] . '-' . $source['exp_month'] . '-00',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($accountGateway->gateway_id == GATEWAY_BRAINTREE) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $paymentMethods;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verifyClientPaymentMethod($client, $sourceId, $amount1, $amount2) {
|
|
|
|
$token = $client->getGatewayToken($accountGateway);
|
|
|
|
if ($accountGateway->gateway_id != GATEWAY_STRIPE) {
|
|
|
|
return 'Unsupported gateway';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-30 23:54:56 +02:00
|
|
|
// Omnipay doesn't support verifying payment methods
|
|
|
|
// Also, it doesn't want to urlencode without putting numbers inside the brackets
|
|
|
|
return $this->makeStripeCall(
|
|
|
|
$accountGateway,
|
|
|
|
'POST',
|
|
|
|
'customers/'.$token.'/sources/'.$sourceId.'/verify',
|
|
|
|
'amounts[]='.intval($amount1).'&amounts[]='.intval($amount2)
|
|
|
|
);
|
2016-04-29 23:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function removeClientPaymentMethod($client, $sourceId) {
|
|
|
|
$token = $client->getGatewayToken($accountGateway/* return parameter */);
|
|
|
|
if (!$token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$gateway = $this->createGateway($accountGateway);
|
|
|
|
|
|
|
|
if ($accountGateway->gateway_id == GATEWAY_STRIPE) {
|
|
|
|
$response = $gateway->deleteCard(array('customerReference' => $token, 'cardReference'=>$sourceId))->send();
|
|
|
|
if (!$response->isSuccessful()) {
|
|
|
|
return $response->getMessage();
|
|
|
|
}
|
|
|
|
} elseif ($accountGateway->gateway_id == GATEWAY_BRAINTREE) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setClientDefaultPaymentMethod($client, $sourceId) {
|
|
|
|
$token = $client->getGatewayToken($accountGateway/* return parameter */);
|
|
|
|
if (!$token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$gateway = $this->createGateway($accountGateway);
|
|
|
|
|
|
|
|
if ($accountGateway->gateway_id == GATEWAY_STRIPE) {
|
|
|
|
|
2016-04-30 23:54:56 +02:00
|
|
|
return $this->makeStripeCall(
|
|
|
|
$accountGateway,
|
|
|
|
'POST',
|
|
|
|
'customers/'.$token,
|
|
|
|
'default_card='.$sourceId
|
|
|
|
);
|
2016-04-29 23:50:21 +02:00
|
|
|
} elseif ($accountGateway->gateway_id == GATEWAY_BRAINTREE) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:10:51 +02:00
|
|
|
public function createToken($gateway, $details, $accountGateway, $client, $contactId, &$customerReference = null)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
2016-04-29 23:50:21 +02:00
|
|
|
$customerReference = $client->getGatewayToken();
|
|
|
|
|
|
|
|
if ($customerReference) {
|
|
|
|
$details['customerReference'] = $customerReference;
|
|
|
|
|
|
|
|
$customerResponse = $gateway->fetchCustomer(array('customerReference'=>$customerReference))->send();
|
|
|
|
|
|
|
|
if (!$customerResponse->isSuccessful()){
|
|
|
|
$customerReference = null; // The customer might not exist anymore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 01:59:52 +02:00
|
|
|
if ($accountGateway->gateway->id == GATEWAY_STRIPE) {
|
2016-05-01 04:45:51 +02:00
|
|
|
if (!empty($details['plaidPublicToken'])) {
|
|
|
|
$plaidResult = $this->getPlaidToken($accountGateway, $details['plaidPublicToken'], $details['plaidAccountId']);
|
|
|
|
|
|
|
|
if (is_string($plaidResult)) {
|
|
|
|
$this->lastError = $plaidResult;
|
|
|
|
return;
|
|
|
|
} elseif (!$plaidResult) {
|
|
|
|
$this->lastError = 'No token received from Plaid';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($details['plaidPublicToken']);
|
|
|
|
unset($details['plaidAccountId']);
|
|
|
|
$details['token'] = $plaidResult['stripe_bank_account_token'];
|
|
|
|
}
|
|
|
|
|
2016-04-27 01:59:52 +02:00
|
|
|
$tokenResponse = $gateway->createCard($details)->send();
|
|
|
|
|
2016-05-01 04:45:51 +02:00
|
|
|
if ($tokenResponse->isSuccessful()) {
|
|
|
|
$sourceReference = $tokenResponse->getCardReference();
|
|
|
|
if (!$customerReference) {
|
|
|
|
$customerReference = $tokenResponse->getCustomerReference();
|
2016-04-30 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 04:45:51 +02:00
|
|
|
if (!$sourceReference) {
|
|
|
|
$responseData = $tokenResponse->getData();
|
|
|
|
if (!empty($responseData['object']) && ($responseData['object'] == 'bank_account' || $responseData['object'] == 'card')) {
|
|
|
|
$sourceReference = $responseData['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($customerReference == $sourceReference) {
|
|
|
|
// This customer was just created; find the card
|
|
|
|
$data = $tokenResponse->getData();
|
|
|
|
if (!empty($data['default_source'])) {
|
|
|
|
$sourceReference = $data['default_source'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-27 01:59:52 +02:00
|
|
|
$data = $tokenResponse->getData();
|
2016-05-01 04:45:51 +02:00
|
|
|
if ($data && $data['error'] && $data['error']['type'] == 'invalid_request_error') {
|
|
|
|
$this->lastError = $data['error']['message'];
|
|
|
|
return;
|
2016-04-27 01:59:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($accountGateway->gateway->id == GATEWAY_BRAINTREE) {
|
2016-04-29 23:50:21 +02:00
|
|
|
if (!$customerReference) {
|
|
|
|
$tokenResponse = $gateway->createCustomer(array('customerData' => array()))->send();
|
|
|
|
if ($tokenResponse->isSuccessful()) {
|
|
|
|
$customerReference = $tokenResponse->getCustomerData()->id;
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 01:59:52 +02:00
|
|
|
|
2016-04-29 23:50:21 +02:00
|
|
|
if ($customerReference) {
|
|
|
|
$details['customerId'] = $customerReference;
|
2016-04-27 01:59:52 +02:00
|
|
|
|
|
|
|
$tokenResponse = $gateway->createPaymentMethod($details)->send();
|
2016-05-01 04:45:51 +02:00
|
|
|
$sourceReference = $tokenResponse->getData()->paymentMethod->token;
|
2016-04-24 04:10:51 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2016-04-24 04:10:51 +02:00
|
|
|
if ($customerReference) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$token = AccountGatewayToken::where('client_id', '=', $client->id)
|
|
|
|
->where('account_gateway_id', '=', $accountGateway->id)->first();
|
|
|
|
|
|
|
|
if (!$token) {
|
|
|
|
$token = new AccountGatewayToken();
|
|
|
|
$token->account_id = $client->account->id;
|
|
|
|
$token->contact_id = $contactId;
|
|
|
|
$token->account_gateway_id = $accountGateway->id;
|
|
|
|
$token->client_id = $client->id;
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:10:51 +02:00
|
|
|
$token->token = $customerReference;
|
2015-09-10 19:50:09 +02:00
|
|
|
$token->save();
|
|
|
|
} else {
|
|
|
|
$this->lastError = $tokenResponse->getMessage();
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:54:56 +02:00
|
|
|
return $sourceReference;
|
2015-09-10 19:50:09 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 09:30:42 +01:00
|
|
|
public function getCheckoutComToken($invitation)
|
|
|
|
{
|
|
|
|
$token = false;
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$client = $invoice->client;
|
|
|
|
$account = $invoice->account;
|
|
|
|
|
|
|
|
$accountGateway = $account->getGatewayConfig(GATEWAY_CHECKOUT_COM);
|
|
|
|
$gateway = $this->createGateway($accountGateway);
|
|
|
|
|
|
|
|
$response = $gateway->purchase([
|
|
|
|
'amount' => $invoice->getRequestedAmount(),
|
|
|
|
'currency' => $client->currency ? $client->currency->code : ($account->currency ? $account->currency->code : 'USD')
|
|
|
|
])->send();
|
|
|
|
|
|
|
|
if ($response->isRedirect()) {
|
|
|
|
$token = $response->getTransactionReference();
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-18 09:30:42 +01:00
|
|
|
Session::set($invitation->id . 'payment_type', PAYMENT_TYPE_CREDIT_CARD);
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2016-04-27 01:59:52 +02:00
|
|
|
public function getBraintreeClientToken($account)
|
|
|
|
{
|
|
|
|
$token = false;
|
|
|
|
|
|
|
|
$accountGateway = $account->getGatewayConfig(GATEWAY_BRAINTREE);
|
|
|
|
$gateway = $this->createGateway($accountGateway);
|
|
|
|
|
|
|
|
$token = $gateway->clientToken()->send()->getToken();
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:06:27 +02:00
|
|
|
public function createPayment($invitation, $accountGateway, $ref, $payerId = null, $paymentDetails = null, $purchaseResponse = null)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
|
|
|
$payment = Payment::createNew($invitation);
|
|
|
|
$payment->invitation_id = $invitation->id;
|
|
|
|
$payment->account_gateway_id = $accountGateway->id;
|
|
|
|
$payment->invoice_id = $invoice->id;
|
|
|
|
$payment->amount = $invoice->getRequestedAmount();
|
|
|
|
$payment->client_id = $invoice->client_id;
|
|
|
|
$payment->contact_id = $invitation->contact_id;
|
|
|
|
$payment->transaction_reference = $ref;
|
|
|
|
$payment->payment_date = date_create()->format('Y-m-d');
|
2016-04-25 16:06:27 +02:00
|
|
|
|
2016-04-27 01:59:52 +02:00
|
|
|
if (!empty($paymentDetails['card'])) {
|
2016-04-25 16:06:27 +02:00
|
|
|
$card = $paymentDetails['card'];
|
|
|
|
$payment->last4 = substr($card->number, -4);
|
|
|
|
$year = $card->expiryYear;
|
|
|
|
if (strlen($year) == 2) {
|
|
|
|
$year = '20' . $year;
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment->expiration = $year . '-' . $card->expiryMonth . '-00';
|
2016-04-28 04:13:51 +02:00
|
|
|
$payment->payment_type_id = $this->detectCardType($card->number);
|
2016-04-25 16:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($accountGateway->gateway_id == GATEWAY_STRIPE) {
|
2016-04-29 23:50:21 +02:00
|
|
|
$data = $purchaseResponse->getData();
|
|
|
|
$source = !empty($data['source'])?$data['source']:$data['card'];
|
2016-04-25 16:06:27 +02:00
|
|
|
|
2016-04-30 23:54:56 +02:00
|
|
|
$payment->payment_status_id = $data['status'] == 'succeeded' ? PAYMENT_STATUS_COMPLETED : PAYMENT_STATUS_PENDING;
|
|
|
|
|
2016-04-29 23:50:21 +02:00
|
|
|
if ($source) {
|
|
|
|
$payment->last4 = $source['last4'];
|
2016-04-25 16:06:27 +02:00
|
|
|
|
2016-04-29 23:50:21 +02:00
|
|
|
if ($source['object'] == 'bank_account') {
|
|
|
|
$payment->routing_number = $source['routing_number'];
|
|
|
|
$payment->payment_type_id = PAYMENT_TYPE_ACH;
|
|
|
|
}
|
|
|
|
else{
|
2016-05-03 00:04:57 +02:00
|
|
|
$payment->expiration = $source['exp_year'] . '-' . $source['exp_month'] . '-00';
|
|
|
|
$payment->payment_type_id = $this->parseCardType($source['brand']);
|
2016-04-25 16:06:27 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-27 01:59:52 +02:00
|
|
|
} elseif ($accountGateway->gateway_id == GATEWAY_BRAINTREE) {
|
|
|
|
$card = $purchaseResponse->getData()->transaction->creditCardDetails;
|
|
|
|
$payment->last4 = $card->last4;
|
|
|
|
$payment->expiration = $card->expirationYear . '-' . $card->expirationMonth . '-00';
|
2016-04-29 23:50:21 +02:00
|
|
|
$payment->payment_type_id = $this->parseCardType($card->cardType);
|
2016-04-25 16:06:27 +02:00
|
|
|
}
|
2016-04-17 00:34:39 +02:00
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
if ($payerId) {
|
|
|
|
$payment->payer_id = $payerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment->save();
|
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
// enable pro plan for hosted users
|
|
|
|
if ($invoice->account->account_key == NINJA_ACCOUNT_KEY) {
|
|
|
|
foreach ($invoice->invoice_items as $invoice_item) {
|
|
|
|
// Hacky, but invoices don't have meta fields to allow us to store this easily
|
|
|
|
if (1 == preg_match('/^Plan - (.+) \((.+)\)$/', $invoice_item->product_key, $matches)) {
|
|
|
|
$plan = strtolower($matches[1]);
|
|
|
|
$term = strtolower($matches[2]);
|
|
|
|
} elseif ($invoice_item->product_key == 'Pending Monthly') {
|
|
|
|
$pending_monthly = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:09:01 +02:00
|
|
|
if (!empty($plan)) {
|
2016-04-17 00:34:39 +02:00
|
|
|
$account = Account::with('users')->find($invoice->client->public_id);
|
2016-04-17 04:09:01 +02:00
|
|
|
|
|
|
|
if(
|
|
|
|
$account->company->plan != $plan
|
|
|
|
|| DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create('-7 days')
|
|
|
|
) {
|
|
|
|
// Either this is a different plan, or the subscription expired more than a week ago
|
|
|
|
// Reset any grandfathering
|
|
|
|
$account->company->plan_started = date_create()->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$account->company->plan == $plan
|
|
|
|
&& $account->company->plan_term == $term
|
|
|
|
&& DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create()
|
|
|
|
) {
|
|
|
|
// This is a renewal; mark it paid as of when this term expires
|
|
|
|
$account->company->plan_paid = $account->company->plan_expires;
|
|
|
|
} else {
|
|
|
|
$account->company->plan_paid = date_create()->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
$account->company->payment_id = $payment->id;
|
|
|
|
$account->company->plan = $plan;
|
|
|
|
$account->company->plan_term = $term;
|
2016-04-17 04:09:01 +02:00
|
|
|
$account->company->plan_expires = DateTime::createFromFormat('Y-m-d', $account->company->plan_paid)
|
|
|
|
->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d');
|
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
if (!empty($pending_monthly)) {
|
|
|
|
$account->company->pending_plan = $plan;
|
|
|
|
$account->company->pending_term = PLAN_TERM_MONTHLY;
|
|
|
|
} else {
|
|
|
|
$account->company->pending_plan = null;
|
|
|
|
$account->company->pending_term = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account->company->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
return $payment;
|
|
|
|
}
|
2016-04-29 23:50:21 +02:00
|
|
|
|
|
|
|
private function parseCardType($cardName) {
|
|
|
|
$cardTypes = array(
|
|
|
|
'Visa' => PAYMENT_TYPE_VISA,
|
|
|
|
'American Express' => PAYMENT_TYPE_AMERICAN_EXPRESS,
|
|
|
|
'MasterCard' => PAYMENT_TYPE_MASTERCARD,
|
|
|
|
'Discover' => PAYMENT_TYPE_DISCOVER,
|
|
|
|
'JCB' => PAYMENT_TYPE_JCB,
|
|
|
|
'Diners Club' => PAYMENT_TYPE_DINERS,
|
|
|
|
'Carte Blanche' => PAYMENT_TYPE_CARTE_BLANCHE,
|
|
|
|
'China UnionPay' => PAYMENT_TYPE_UNIONPAY,
|
|
|
|
'Laser' => PAYMENT_TYPE_LASER,
|
|
|
|
'Maestro' => PAYMENT_TYPE_MAESTRO,
|
|
|
|
'Solo' => PAYMENT_TYPE_SOLO,
|
|
|
|
'Switch' => PAYMENT_TYPE_SWITCH
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($cardTypes[$cardName])) {
|
|
|
|
return $cardTypes[$cardName];
|
|
|
|
} else {
|
|
|
|
return PAYMENT_TYPE_CREDIT_CARD_OTHER;
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 16:06:27 +02:00
|
|
|
|
|
|
|
private function detectCardType($number)
|
|
|
|
{
|
|
|
|
if (preg_match('/^3[47][0-9]{13}$/',$number)) {
|
2016-04-28 04:13:51 +02:00
|
|
|
return PAYMENT_TYPE_AMERICAN_EXPRESS;
|
2016-04-25 16:06:27 +02:00
|
|
|
} elseif (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',$number)) {
|
2016-04-28 04:13:51 +02:00
|
|
|
return PAYMENT_TYPE_DINERS;
|
2016-04-25 16:06:27 +02:00
|
|
|
} elseif (preg_match('/^6(?:011|5[0-9][0-9])[0-9]{12}$/',$number)) {
|
2016-04-28 04:13:51 +02:00
|
|
|
return PAYMENT_TYPE_DISCOVER;
|
2016-04-25 16:06:27 +02:00
|
|
|
} elseif (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/',$number)) {
|
2016-04-28 04:13:51 +02:00
|
|
|
return PAYMENT_TYPE_JCB;
|
2016-04-25 16:06:27 +02:00
|
|
|
} elseif (preg_match('/^5[1-5][0-9]{14}$/',$number)) {
|
2016-04-28 04:13:51 +02:00
|
|
|
return PAYMENT_TYPE_MASTERCARD;
|
2016-04-25 16:06:27 +02:00
|
|
|
} elseif (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/',$number)) {
|
2016-04-28 04:13:51 +02:00
|
|
|
return PAYMENT_TYPE_VISA;
|
2016-04-25 16:06:27 +02:00
|
|
|
}
|
2016-04-28 04:13:51 +02:00
|
|
|
return PAYMENT_TYPE_CREDIT_CARD_OTHER;
|
2016-04-25 16:06:27 +02:00
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-12-21 20:57:55 +01:00
|
|
|
public function completePurchase($gateway, $accountGateway, $details, $token)
|
|
|
|
{
|
|
|
|
if ($accountGateway->isGateway(GATEWAY_MOLLIE)) {
|
|
|
|
$details['transactionReference'] = $token;
|
|
|
|
$response = $gateway->fetchTransaction($details)->send();
|
|
|
|
return $gateway->fetchTransaction($details)->send();
|
|
|
|
} else {
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2015-12-21 20:57:55 +01:00
|
|
|
return $gateway->completePurchase($details)->send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
public function autoBillInvoice($invoice)
|
|
|
|
{
|
|
|
|
$client = $invoice->client;
|
|
|
|
$account = $invoice->account;
|
|
|
|
$invitation = $invoice->invitations->first();
|
2016-04-27 01:59:52 +02:00
|
|
|
$accountGateway = $account->getTokenGateway();
|
2015-10-11 16:41:09 +02:00
|
|
|
$token = $client->getGatewayToken();
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
if (!$invitation || !$accountGateway || !$token) {
|
2015-09-10 19:50:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the gateway/payment info
|
|
|
|
$gateway = $this->createGateway($accountGateway);
|
2015-10-21 14:40:31 +02:00
|
|
|
$details = $this->getPaymentDetails($invitation, $accountGateway);
|
2015-11-27 13:55:28 +01:00
|
|
|
$details['customerReference'] = $token;
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2016-04-27 01:59:52 +02:00
|
|
|
if ($accountGateway->gateway_id == GATEWAY_STRIPE) {
|
|
|
|
$details['customerReference'] = $token;
|
|
|
|
|
|
|
|
} elseif ($accountGateway->gateway_id == GATEWAY_BRAINTREE) {
|
|
|
|
$details['customerId'] = $token;
|
|
|
|
$customer = $gateway->findCustomer($token)->send();
|
|
|
|
$details['paymentMethodToken'] = $customer->getData()->paymentMethods[0]->token;
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
// submit purchase/get response
|
|
|
|
$response = $gateway->purchase($details)->send();
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-02-22 11:26:26 +01:00
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
$ref = $response->getTransactionReference();
|
2016-04-27 01:59:52 +02:00
|
|
|
return $this->createPayment($invitation, $accountGateway, $ref, null, $details, $response);
|
2016-02-22 11:26:26 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
|
|
|
|
public function getDatatable($clientPublicId, $search)
|
|
|
|
{
|
|
|
|
$query = $this->paymentRepo->find($clientPublicId, $search);
|
|
|
|
|
2016-03-16 00:08:00 +01:00
|
|
|
if(!Utils::hasPermission('view_all')){
|
|
|
|
$query->where('payments.user_id', '=', Auth::user()->id);
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:10:51 +02:00
|
|
|
return $this->createDatatable(ENTITY_PAYMENT, $query, !$clientPublicId, false,
|
|
|
|
['invoice_number', 'transaction_reference', 'payment_type', 'amount', 'payment_date']);
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDatatableColumns($entityType, $hideClient)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'invoice_number',
|
|
|
|
function ($model) {
|
2016-04-26 03:53:39 +02:00
|
|
|
if(!Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->invoice_user_id])){
|
2016-03-16 03:07:11 +01:00
|
|
|
return $model->invoice_number;
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:36:42 +01:00
|
|
|
return link_to("invoices/{$model->invoice_public_id}/edit", $model->invoice_number, ['class' => Utils::getEntityRowClass($model)])->toHtml();
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'client_name',
|
|
|
|
function ($model) {
|
2016-04-26 03:53:39 +02:00
|
|
|
if(!Auth::user()->can('viewByOwner', [ENTITY_CLIENT, $model->client_user_id])){
|
2016-03-16 03:07:11 +01:00
|
|
|
return Utils::getClientDisplayName($model);
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:36:42 +01:00
|
|
|
return $model->client_public_id ? link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml() : '';
|
2015-11-05 23:37:04 +01:00
|
|
|
},
|
|
|
|
! $hideClient
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'transaction_reference',
|
|
|
|
function ($model) {
|
|
|
|
return $model->transaction_reference ? $model->transaction_reference : '<i>Manual entry</i>';
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'payment_type',
|
|
|
|
function ($model) {
|
2016-04-28 04:13:51 +02:00
|
|
|
return ($model->payment_type && !$model->last4) ? $model->payment_type : ($model->account_gateway_id ? $model->gateway_name : '');
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
2016-04-24 04:10:51 +02:00
|
|
|
[
|
|
|
|
'source',
|
2016-04-29 23:50:21 +02:00
|
|
|
function ($model) {
|
2016-04-28 04:13:51 +02:00
|
|
|
if (!$model->last4) return '';
|
|
|
|
$code = str_replace(' ', '', strtolower($model->payment_type));
|
|
|
|
$card_type = trans("texts.card_" . $code);
|
2016-04-29 23:50:21 +02:00
|
|
|
if ($model->payment_type_id != PAYMENT_TYPE_ACH) {
|
|
|
|
$expiration = trans('texts.card_expiration', array('expires' => Utils::fromSqlDate($model->expiration, false)->format('m/y')));
|
|
|
|
return '<img height="22" src="' . URL::to('/images/credit_cards/' . $code . '.png') . '" alt="' . htmlentities($card_type) . '"> •••' . $model->last4 . ' ' . $expiration;
|
|
|
|
} else {
|
|
|
|
$bankData = PaymentController::getBankData($model->routing_number);
|
|
|
|
if (is_array($bankData)) {
|
|
|
|
return $bankData['name'].' •••' . $model->last4;
|
|
|
|
} else {
|
|
|
|
return '<img height="22" src="' . URL::to('/images/credit_cards/ach.png') . '" alt="' . htmlentities($card_type) . '"> •••' . $model->last4;
|
|
|
|
}
|
|
|
|
}
|
2016-04-24 04:10:51 +02:00
|
|
|
}
|
|
|
|
],
|
2015-11-05 23:37:04 +01:00
|
|
|
[
|
|
|
|
'amount',
|
|
|
|
function ($model) {
|
2015-12-07 14:34:55 +01:00
|
|
|
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'payment_date',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::dateToString($model->payment_date);
|
|
|
|
}
|
2016-04-23 22:40:19 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'payment_status_name',
|
|
|
|
function ($model) use ($entityType) {
|
|
|
|
return self::getStatusLabel($entityType, $model);
|
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDatatableActions($entityType)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
trans('texts.edit_payment'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("payments/{$model->public_id}/edit");
|
2016-03-16 00:08:00 +01:00
|
|
|
},
|
|
|
|
function ($model) {
|
2016-04-26 03:53:39 +02:00
|
|
|
return Auth::user()->can('editByOwner', [ENTITY_PAYMENT, $model->user_id]);
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
2016-04-23 22:40:19 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
trans('texts.refund_payment'),
|
|
|
|
function ($model) {
|
|
|
|
$max_refund = number_format($model->amount - $model->refunded, 2);
|
|
|
|
$formatted = Utils::formatMoney($max_refund, $model->currency_id, $model->country_id);
|
|
|
|
$symbol = Utils::getFromCache($model->currency_id, 'currencies')->symbol;
|
|
|
|
return "javascript:showRefundModal({$model->public_id}, '{$max_refund}', '{$formatted}', '{$symbol}')";
|
|
|
|
},
|
|
|
|
function ($model) {
|
2016-05-03 00:04:57 +02:00
|
|
|
return Payment::canEditItem($model) && $model->payment_status_id != PAYMENT_STATUS_FAILED &&
|
|
|
|
$model->refunded < $model->amount &&
|
|
|
|
(
|
2016-04-23 22:40:19 +02:00
|
|
|
($model->transaction_reference && in_array($model->gateway_id , static::$refundableGateways))
|
|
|
|
|| $model->payment_type_id == PAYMENT_TYPE_CREDIT
|
|
|
|
);
|
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2016-04-23 22:40:19 +02:00
|
|
|
|
|
|
|
public function bulk($ids, $action, $params = array())
|
|
|
|
{
|
|
|
|
if ($action == 'refund') {
|
|
|
|
if ( ! $ids ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
|
2016-04-23 22:40:19 +02:00
|
|
|
$payments = $this->getRepo()->findByPublicIdsWithTrashed($ids);
|
2015-11-05 23:37:04 +01:00
|
|
|
|
2016-04-23 22:40:19 +02:00
|
|
|
foreach ($payments as $payment) {
|
|
|
|
if($payment->canEdit()){
|
|
|
|
if(!empty($params['amount'])) {
|
|
|
|
$this->refund($payment, floatval($params['amount']));
|
|
|
|
} else {
|
|
|
|
$this->refund($payment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($payments);
|
|
|
|
} else {
|
|
|
|
return parent::bulk($ids, $action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getStatusLabel($entityType, $model)
|
|
|
|
{
|
|
|
|
$label = trans("texts.status_" . strtolower($model->payment_status_name));
|
|
|
|
$class = 'default';
|
|
|
|
switch ($model->payment_status_id) {
|
|
|
|
case PAYMENT_STATUS_PENDING:
|
|
|
|
$class = 'info';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_COMPLETED:
|
|
|
|
$class = 'success';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_FAILED:
|
|
|
|
$class = 'danger';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_PARTIALLY_REFUNDED:
|
|
|
|
$label = trans('texts.status_partially_refunded_amount', [
|
|
|
|
'amount' => Utils::formatMoney($model->refunded, $model->currency_id, $model->country_id),
|
|
|
|
]);
|
|
|
|
$class = 'primary';
|
|
|
|
break;
|
|
|
|
case PAYMENT_STATUS_REFUNDED:
|
|
|
|
$class = 'default';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refund($payment, $amount = null) {
|
|
|
|
if (!$amount) {
|
|
|
|
$amount = $payment->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
$amount = min($amount, $payment->amount - $payment->refunded);
|
|
|
|
|
|
|
|
if (!$amount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($payment->payment_type_id != PAYMENT_TYPE_CREDIT) {
|
|
|
|
$accountGateway = $this->createGateway($payment->account_gateway);
|
|
|
|
$refund = $accountGateway->refund(array(
|
|
|
|
'transactionReference' => $payment->transaction_reference,
|
|
|
|
'amount' => $amount,
|
|
|
|
));
|
|
|
|
$response = $refund->send();
|
|
|
|
|
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
$payment->recordRefund($amount);
|
|
|
|
} else {
|
|
|
|
$this->error('Unknown', $response->getMessage(), $accountGateway);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$payment->recordRefund($amount);
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 23:54:56 +02:00
|
|
|
|
|
|
|
public function makeStripeCall($accountGateway, $method, $url, $body = null) {
|
|
|
|
$apiKey = $accountGateway->getConfig()->apiKey;
|
|
|
|
|
|
|
|
if (!$apiKey) {
|
|
|
|
return 'No API key set';
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
$options = [
|
|
|
|
'headers' => ['content-type' => 'application/x-www-form-urlencoded'],
|
|
|
|
'auth' => [$accountGateway->getConfig()->apiKey,''],
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($body) {
|
|
|
|
$options['body'] = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = (new \GuzzleHttp\Client(['base_uri'=>'https://api.stripe.com/v1/']))->request(
|
|
|
|
$method,
|
|
|
|
$url,
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
return json_decode($response->getBody(), true);
|
|
|
|
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
|
|
|
|
$response = $e->getResponse();
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
|
|
|
|
|
|
if ($body && $body['error'] && $body['error']['type'] == 'invalid_request_error') {
|
|
|
|
return $body['error']['message'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
2016-05-01 04:45:51 +02:00
|
|
|
|
|
|
|
private function getPlaidToken($accountGateway, $publicToken, $accountId) {
|
|
|
|
$clientId = $accountGateway->getPlaidClientId();
|
|
|
|
$secret = $accountGateway->getPlaidSecret();
|
|
|
|
|
|
|
|
if (!$clientId) {
|
|
|
|
return 'No client ID set';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$secret) {
|
|
|
|
return 'No secret set';
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
$subdomain = $accountGateway->getPlaidEnvironment() == 'production' ? 'api' : 'tartan';
|
|
|
|
$response = (new \GuzzleHttp\Client(['base_uri'=>"https://{$subdomain}.plaid.com"]))->request(
|
|
|
|
'POST',
|
|
|
|
'exchange_token',
|
|
|
|
[
|
|
|
|
'allow_redirects' => false,
|
|
|
|
'headers' => ['content-type' => 'application/x-www-form-urlencoded'],
|
|
|
|
'body' => http_build_query(array(
|
|
|
|
'client_id' => $clientId,
|
|
|
|
'secret' => $secret,
|
|
|
|
'public_token' => $publicToken,
|
|
|
|
'account_id' => $accountId,
|
|
|
|
))
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return json_decode($response->getBody(), true);
|
|
|
|
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
|
|
|
|
$response = $e->getResponse();
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
|
|
|
|
|
|
if ($body && !empty($body['message'])) {
|
|
|
|
return $body['message'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
}
|