1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Ninja/PaymentDrivers/WePayPaymentDriver.php

209 lines
6.6 KiB
PHP
Raw Normal View History

2016-06-20 16:14:43 +02:00
<?php namespace App\Ninja\PaymentDrivers;
2016-06-21 18:10:22 +02:00
use Session;
use Utils;
2016-06-21 11:07:16 +02:00
use Exception;
2016-06-20 16:14:43 +02:00
class WePayPaymentDriver extends BasePaymentDriver
{
protected function gatewayTypes()
{
return [
GATEWAY_TYPE_CREDIT_CARD,
GATEWAY_TYPE_BANK_TRANSFER,
GATEWAY_TYPE_TOKEN
];
}
2016-06-22 11:22:38 +02:00
/*
2016-06-20 21:26:48 +02:00
public function startPurchase($input = false, $sourceId = false)
2016-06-20 16:14:43 +02:00
{
$data = parent::startPurchase($input, $sourceId);
if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) {
if ( ! $sourceId) {
throw new Exception();
}
}
return $data;
}
2016-06-22 11:22:38 +02:00
*/
2016-06-20 16:14:43 +02:00
public function tokenize()
{
return true;
}
protected function checkCustomerExists($customer)
{
return true;
}
public function rules()
{
$rules = parent::rules();
if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) {
$rules = array_merge($rules, [
'authorize_ach' => 'required',
'tos_agree' => 'required',
]);
}
return $rules;
}
protected function paymentDetails($paymentMethod = false)
{
$data = parent::paymentDetails($paymentMethod);
2016-06-21 18:10:22 +02:00
if ($transactionId = Session::get($this->invitation->id . 'payment_ref')) {
2016-06-20 16:14:43 +02:00
$data['transaction_id'] = $transactionId;
}
$data['applicationFee'] = $this->calculateApplicationFee($data['amount']);
$data['feePayer'] = WEPAY_FEE_PAYER;
$data['callbackUri'] = $this->accountGateway->getWebhookUrl();
if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) {
$data['paymentMethodType'] = 'payment_bank';
}
return $data;
}
2016-06-21 18:10:22 +02:00
public function createToken()
{
$wepay = Utils::setupWePay($this->accountGateway);
$token = intval($this->input['sourceToken']);
if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) {
// Persist bank details
$this->tokenResponse = $wepay->request('/payment_bank/persist', array(
'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET,
'payment_bank_id' => $token,
));
} else {
// Authorize credit card
$tokenResponse = $wepay->request('credit_card/authorize', array(
'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET,
'credit_card_id' => $token,
));
// Update the callback uri and get the card details
$tokenResponse = $wepay->request('credit_card/modify', array(
'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET,
'credit_card_id' => $token,
'auto_update' => WEPAY_AUTO_UPDATE,
'callback_uri' => $this->accountGateway->getWebhookUrl(),
));
$this->tokenResponse = $wepay->request('credit_card', array(
'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET,
'credit_card_id' => $token,
));
}
return parent::createToken();
}
/*
public function creatingCustomer($customer)
{
if ($gatewayResponse instanceof \Omnipay\WePay\Message\CustomCheckoutResponse) {
$wepay = \Utils::setupWePay($accountGateway);
$paymentMethodType = $gatewayResponse->getData()['payment_method']['type'];
$gatewayResponse = $wepay->request($paymentMethodType, array(
'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET,
$paymentMethodType.'_id' => $gatewayResponse->getData()['payment_method'][$paymentMethodType]['id'],
));
}
}
*/
protected function creatingPaymentMethod($paymentMethod)
{
$source = $this->tokenResponse;
if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) {
$paymentMethod->payment_type_id = PAYMENT_TYPE_ACH;
$paymentMethod->last4 = $source->account_last_four;
$paymentMethod->bank_name = $source->bank_name;
$paymentMethod->source_reference = $source->payment_bank_id;
switch($source->state) {
case 'new':
case 'pending':
$paymentMethod->status = 'new';
break;
case 'authorized':
$paymentMethod->status = 'verified';
break;
}
} else {
$paymentMethod->last4 = $source->last_four;
$paymentMethod->payment_type_id = $this->parseCardType($source->credit_card_name);
$paymentMethod->expiration = $source->expiration_year . '-' . $source->expiration_month . '-01';
$paymentMethod->source_reference = $source->credit_card_id;
}
return $paymentMethod;
}
2016-06-20 16:14:43 +02:00
public function removePaymentMethod($paymentMethod)
{
$wepay = Utils::setupWePay($this->accountGateway);
2016-06-21 18:10:22 +02:00
$response = $wepay->request('/credit_card/delete', [
2016-06-20 16:14:43 +02:00
'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET,
'credit_card_id' => intval($paymentMethod->source_reference),
]);
2016-06-21 18:10:22 +02:00
if ($response->state == 'deleted') {
2016-06-20 16:14:43 +02:00
return parent::removePaymentMethod($paymentMethod);
} else {
2016-06-22 11:22:38 +02:00
throw new Exception(trans('texts.failed_remove_payment_method'));
2016-06-20 16:14:43 +02:00
}
}
protected function refundDetails($payment, $amount)
{
2016-06-21 18:10:22 +02:00
$data = parent::refundDetails($payment, $amount);
2016-06-20 16:14:43 +02:00
$data['refund_reason'] = 'Refund issued by merchant.';
// WePay issues a full refund when no amount is set. If an amount is set, it will try
// to issue a partial refund without refunding any fees. However, the Stripe driver
// (but not the API) requires the amount parameter to be set no matter what.
if ($data['amount'] == $payment->getCompletedAmount()) {
unset($data['amount']);
}
return $data;
}
protected function attemptVoidPayment($response, $payment, $amount)
{
if ( ! parent::attemptVoidPayment($response, $payment, $amount)) {
return false;
}
return $response->getCode() == 4004;
}
private function calculateApplicationFee($amount)
{
$fee = WEPAY_APP_FEE_MULTIPLIER * $amount + WEPAY_APP_FEE_FIXED;
return floor(min($fee, $amount * 0.2));// Maximum fee is 20% of the amount.
}
}