1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/PaymentDrivers/WePay/CreditCard.php

200 lines
5.7 KiB
PHP
Raw Normal View History

2021-05-05 06:29:58 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\WePay;
2021-06-16 05:26:33 +02:00
use App\Exceptions\PaymentFailed;
2021-06-19 06:58:45 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
2021-06-16 05:26:33 +02:00
use App\Models\GatewayType;
2021-05-05 06:29:58 +02:00
use App\PaymentDrivers\WePayPaymentDriver;
class CreditCard
{
2021-06-15 13:37:16 +02:00
public $wepay_payment_driver;
2021-05-05 06:29:58 +02:00
2021-06-15 13:37:16 +02:00
public function __construct(WePayPaymentDriver $wepay_payment_driver)
2021-05-05 06:29:58 +02:00
{
2021-06-15 13:37:16 +02:00
$this->wepay_payment_driver = $wepay_payment_driver;
2021-05-05 06:29:58 +02:00
}
public function authorizeView($data)
{
2021-06-15 13:37:16 +02:00
$data['gateway'] = $this->wepay_payment_driver;
2021-06-13 14:49:43 +02:00
2021-06-11 10:30:39 +02:00
return render('gateways.wepay.authorize.authorize', $data);
2021-05-05 06:29:58 +02:00
}
2021-06-11 10:30:39 +02:00
2021-06-15 07:56:23 +02:00
public function authorizeResponse($request)
2021-06-11 10:30:39 +02:00
{
//https://developer.wepay.com/api/api-calls/credit_card#authorize
2021-06-15 07:56:23 +02:00
$data = $request->all();
2021-06-11 10:30:39 +02:00
// authorize the credit card
2021-06-15 13:37:16 +02:00
nlog($data);
2021-06-16 05:26:33 +02:00
/*
'_token' => '1Fk5CRj34up5ntKPvrFyMIAJhDdUNF3boqT3iIN3',
'company_gateway_id' => '39',
'payment_method_id' => '1',
'gateway_response' => NULL,
'is_default' => NULL,
'credit_card_id' => '180642154638',
'q' => '/client/payment_methods',
'method' => '1',
*/
2021-06-15 13:37:16 +02:00
$response = $this->wepay_payment_driver->wepay->request('credit_card/authorize', array(
2021-06-15 07:56:23 +02:00
'client_id' => config('ninja.wepay.client_id'),
'client_secret' => config('ninja.wepay.client_secret'),
2021-06-16 05:26:33 +02:00
'credit_card_id' => (int)$data['credit_card_id'],
2021-06-11 10:30:39 +02:00
));
// display the response
2021-06-16 08:41:29 +02:00
// nlog($response);
2021-06-16 05:26:33 +02:00
if(in_array($response->state, ['new', 'authorized'])){
$this->storePaymentMethod($response, GatewayType::CREDIT_CARD);
return redirect()->route('client.payment_methods.index');
}
throw new PaymentFailed("There was a problem adding this payment method.", 400);
/*
[credit_card_id] => 348084962473
[credit_card_name] => Visa xxxxxx4018
[state] => authorized
[user_name] => Joey Diaz
[email] => user@example.com
[create_time] => 1623798172
[expiration_month] => 10
[expiration_year] => 2023
[last_four] => 4018
[input_source] => card_keyed
[virtual_terminal_mode] => none
[card_on_file] =>
[recurring] =>
[cvv_provided] => 1
[auto_update] =>
*/
}
2021-06-16 12:12:04 +02:00
public function paymentView(array $data)
{
$data['gateway'] = $this->wepay_payment_driver;
return render('gateways.wepay.credit_card.pay', $data);
}
public function paymentResponse(PaymentResponseRequest $request)
{
2021-06-19 06:58:45 +02:00
// USD, CAD, and GBP.
nlog($request->all());
// charge the credit card
$response = $this->wepay_payment_driver->wepay->request('checkout/create', array(
'account_id' => $this->wepay_payment_driver->company_gateway->getConfigField('accountId'),
'amount' => $this->wepay_payment_driver->payment_hash->data->amount_with_fee,
'currency' => $this->wepay_payment_driver->client->getCurrencyCode(),
'short_description' => 'A vacation home rental',
'type' => 'goods',
'payment_method' => array(
'type' => 'credit_card',
'credit_card' => array(
'id' => $data['token']
)
)
));
2021-06-16 12:12:04 +02:00
}
2021-06-19 06:58:45 +02:00
/*
https://developer.wepay.com/api/api-calls/checkout
{
"checkout_id": 649945633,
"account_id": 1548718026,
"type": "donation",
"short_description": "test checkout",
"currency": "USD",
"amount": 20,
"state": "authorized",
"soft_descriptor": "WPY*Wolverine",
"auto_release": true,
"create_time": 1463589958,
"gross": 20.88,
"reference_id": null,
"callback_uri": null,
"long_description": null,
"delivery_type": null,
"initiated_by": "merchant",
"in_review": false,
"fee": {
"app_fee": 0,
"processing_fee": 0.88,
"fee_payer": "payer"
},
"chargeback": {
"amount_charged_back": 0,
"dispute_uri": null
},
"refund": {
"amount_refunded": 0,
"refund_reason": null
},
"payment_method": {
"type": "credit_card",
"credit_card": {
"id": 1684847614,
"data": {
"emv_receipt": null,
"signature_url": null
},
"auto_release": false
}
},
"hosted_checkout": null,
"payer": {
"email": "test@example.com",
"name": "Mr Smith",
"home_address": null
},
"npo_information": null,
"payment_error": null
}
*/
2021-06-16 05:26:33 +02:00
private function storePaymentMethod($response, $payment_method_id)
{
$payment_meta = new \stdClass;
$payment_meta->exp_month = (string) $response->expiration_month;
$payment_meta->exp_year = (string) $response->expiration_year;
$payment_meta->brand = (string) $response->credit_card_name;
$payment_meta->last4 = (string) $response->last_four;
$payment_meta->type = GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $response->credit_card_id,
'payment_method_id' => $payment_method_id,
];
$this->wepay_payment_driver->storeGatewayToken($data);
}
2021-06-16 12:12:04 +02:00
2021-05-05 06:29:58 +02:00
}
2021-06-16 05:26:33 +02:00