diff --git a/app/Http/Controllers/AccountGatewayController.php b/app/Http/Controllers/AccountGatewayController.php index 9b02bfe56a24..765fbc78a9d6 100644 --- a/app/Http/Controllers/AccountGatewayController.php +++ b/app/Http/Controllers/AccountGatewayController.php @@ -403,7 +403,6 @@ class AccountGatewayController extends BaseController 'original_device' => \Request::server('HTTP_USER_AGENT'), 'tos_acceptance_time' => time(), 'redirect_uri' => URL::to('gateways'), - 'callback_uri' => URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$account->account_key.'/'.GATEWAY_WEPAY), 'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money', ); @@ -418,6 +417,7 @@ class AccountGatewayController extends BaseController 'name' => Input::get('company_name'), 'description' => Input::get('description'), 'theme_object' => json_decode(WEPAY_THEME), + 'callback_uri' => $accountGateway->getWebhookUrl(), ); if (WEPAY_ENABLE_CANADA) { @@ -453,6 +453,7 @@ class AccountGatewayController extends BaseController 'tokenType' => $wepayUser->token_type, 'tokenExpires' => $accessTokenExpires, 'accountId' => $wepayAccount->account_id, + 'state' => $wepayAccount->state, 'testMode' => WEPAY_ENVIRONMENT == WEPAY_STAGE, 'country' => WEPAY_ENABLE_CANADA ? Input::get('country') : 'US', )); diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 3e89f3091910..6a5742fabe2b 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -839,6 +839,53 @@ class PaymentController extends BaseController $this->paymentService->convertPaymentMethodFromWePay($source, null, $paymentMethod)->save(); } + return array('message' => 'Processed successfully'); + } elseif ($objectType == 'account') { + $config = $accountGateway->getConfig(); + if ($config->accountId != $objectId) { + return array('message' => 'Unknown account'); + } + + $wepay = \Utils::setupWePay($accountGateway); + $wepayAccount = $wepay->request('account', array( + 'account_id' => intval($objectId), + )); + + if ($wepayAccount->state == 'deleted') { + $accountGateway->delete(); + } else { + $config->state = $wepayAccount->state; + $accountGateway->setConfig($config); + $accountGateway->save(); + } + + return array('message' => 'Processed successfully'); + } elseif ($objectType == 'checkout') { + $payment = Payment::scope(false, $accountId)->where('transaction_reference', '=', $objectId)->first(); + + if (!$payment) { + return array('message' => 'Unknown payment'); + } + + $wepay = \Utils::setupWePay($accountGateway); + $checkout = $wepay->request('checkout', array( + 'checkout_id' => intval($objectId), + )); + + if ($checkout->state == 'refunded') { + $payment->recordRefund(); + } elseif (!empty($checkout->refund) && !empty($checkout->refund->amount_refunded) && ($checkout->refund->amount_refunded - $payment->refunded) > 0) { + $payment->recordRefund($checkout->refund->amount_refunded - $payment->refunded); + } + + if ($checkout->state == 'captured') { + $payment->markComplete(); + } elseif ($checkout->state == 'cancelled') { + $payment->markCancelled(); + } elseif ($checkout->state == 'failed') { + $payment->markFailed(); + } + return array('message' => 'Processed successfully'); } else { return array('message' => 'Ignoring event'); @@ -904,6 +951,8 @@ class PaymentController extends BaseController } } elseif ($eventType == 'charge.succeeded') { $payment->markComplete(); + } elseif ($eventType == 'charge.refunded') { + $payment->recordRefund($charge['amount_refunded'] / 100 - $payment->refunded); } } elseif($eventType == 'customer.source.updated' || $eventType == 'customer.source.deleted') { $source = $eventDetails['data']['object']; diff --git a/app/Models/AccountGateway.php b/app/Models/AccountGateway.php index 0c281856be66..563fe16720fb 100644 --- a/app/Models/AccountGateway.php +++ b/app/Models/AccountGateway.php @@ -124,5 +124,11 @@ class AccountGateway extends EntityModel return substr(trim($stripe_key), 0, 8) == 'pk_test_' ? 'tartan' : 'production'; } + + public function getWebhookUrl() + { + $account = $this->account ? $this->account : Account::find($this->account_id); + return \URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$account->account_key.'/'.$this->gateway_id.env('WEBHOOK_SUFFIX','')); + } } diff --git a/app/Services/AccountGatewayService.php b/app/Services/AccountGatewayService.php index a992da5c82c9..88ee177321ec 100644 --- a/app/Services/AccountGatewayService.php +++ b/app/Services/AccountGatewayService.php @@ -48,16 +48,17 @@ class AccountGatewayService extends BaseService return link_to("gateways/{$model->public_id}/edit", $model->name)->toHtml(); } else { $accountGateway = AccountGateway::find($model->id); + $config = $accountGateway->getConfig(); $endpoint = WEPAY_ENVIRONMENT == WEPAY_STAGE ? 'https://stage.wepay.com/' : 'https://www.wepay.com/'; - $wepayAccountId = $accountGateway->getConfig()->accountId; + $wepayAccountId = $config->accountId; + $wepayState = isset($config->state)?$config->state:null; $linkText = $model->name; $url = $endpoint.'account/'.$wepayAccountId; $wepay = \Utils::setupWepay($accountGateway); $html = link_to($url, $linkText, array('target'=>'_blank'))->toHtml(); try { - $wepayAccount = $wepay->request('/account', array('account_id' => $wepayAccountId)); - if ($wepayAccount->state == 'action_required') { + if ($wepayState == 'action_required') { $updateUri = $wepay->request('/account/get_update_uri', array( 'account_id' => $wepayAccountId, 'redirect_uri' => URL::to('gateways'), @@ -67,7 +68,7 @@ class AccountGatewayService extends BaseService $url = $updateUri->uri; $html = "{$linkText}"; $model->setupUrl = $url; - } elseif ($wepayAccount->state == 'pending') { + } elseif ($wepayState == 'pending') { $linkText .= ' ('.trans('texts.resend_confirmation_email').')'; $model->resendConfirmationUrl = $url = URL::to("gateways/{$accountGateway->public_id}/resend_confirmation"); $html = link_to($url, $linkText)->toHtml(); diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index ae014577a603..51cbccc57177 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -413,7 +413,7 @@ class PaymentService extends BaseService 'client_secret' => WEPAY_CLIENT_SECRET, 'credit_card_id' => intval($details['token']), 'auto_update' => WEPAY_AUTO_UPDATE, - 'callback_uri' => URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$client->account->account_key.'/'.GATEWAY_WEPAY), + 'callback_uri' => $accountGateway->getWebhookUrl(), )); $tokenResponse = $wepay->request('credit_card', array( 'client_id' => WEPAY_CLIENT_ID, @@ -1199,6 +1199,7 @@ class PaymentService extends BaseService if ($accountGateway->gateway_id == GATEWAY_WEPAY) { $details['applicationFee'] = $this->calculateApplicationFee($accountGateway, $details['amount']); $details['feePayer'] = WEPAY_FEE_PAYER; + $details['callbackUri'] = $accountGateway->getWebhookUrl(); } $response = $gateway->purchase($details)->send();