mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-31 05:57:44 -04:00 
			
		
		
		
	refactor paymentwebhook suite with new methods
This commit is contained in:
		
							parent
							
								
									2609e0027a
								
							
						
					
					
						commit
						c548bc2e0d
					
				| @ -13,8 +13,6 @@ | ||||
| namespace App\Http\Controllers; | ||||
| 
 | ||||
| use App\Http\Requests\Payments\PaymentWebhookRequest; | ||||
| use App\Models\Payment; | ||||
| use Illuminate\Support\Arr; | ||||
| 
 | ||||
| class PaymentWebhookController extends Controller | ||||
| { | ||||
| @ -23,36 +21,10 @@ class PaymentWebhookController extends Controller | ||||
|         $this->middleware('guest'); | ||||
|     } | ||||
| 
 | ||||
|     public function __invoke(PaymentWebhookRequest $request, string $company_key = null, string $gateway_key = null) | ||||
|     public function __invoke(PaymentWebhookRequest $request, string $gateway_key, string $company_key) | ||||
|     { | ||||
|         $transaction_reference = $this->getTransactionReference($request->all(), $request); | ||||
| 
 | ||||
|         $payment = Payment::where('transaction_reference', $transaction_reference)->first(); | ||||
| 
 | ||||
|         if (is_null($payment)) { | ||||
|             return response([ | ||||
|                 'message' => 'Sorry, we couldn\'t find requested payment.', | ||||
|                 'status_code' => 404, | ||||
|             ], 404); /* Record event, throw an exception.. */ | ||||
|         } | ||||
| 
 | ||||
|         return $request | ||||
|             ->companyGateway() | ||||
|             ->driver($payment->client) | ||||
|             ->setPaymentMethod($payment->gateway_type_id) | ||||
|             ->processWebhookRequest($request->all(), $request->company(), $request->companyGateway(), $payment); | ||||
|     } | ||||
| 
 | ||||
|     public function getTransactionReference(array $data, PaymentWebhookRequest $request) | ||||
|     { | ||||
|         $flatten = Arr::dot($data); | ||||
| 
 | ||||
|         if (isset($flatten['data.object.id'])) { | ||||
|             return $flatten['data.object.id']; // stripe.com
 | ||||
|         } | ||||
| 
 | ||||
|         if ($request->has('cko-session-id')) { | ||||
|             // checkout.com
 | ||||
|         } | ||||
|         return $request->getCompanyGateway() | ||||
|             ->driver($request->getClient()) | ||||
|             ->processWebhookRequest($request, $request->getPayment()); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| <?php | ||||
| 
 | ||||
| /** | ||||
|  * Invoice Ninja (https://invoiceninja.com). | ||||
|  * | ||||
| @ -9,12 +10,14 @@ | ||||
|  * @license https://opensource.org/licenses/AAL | ||||
|  */ | ||||
| 
 | ||||
| 
 | ||||
| namespace App\Http\Requests\Payments; | ||||
| 
 | ||||
| use App\Http\Requests\Request; | ||||
| use App\Models\Client; | ||||
| use App\Models\Company; | ||||
| use App\Models\CompanyGateway; | ||||
| use App\Models\Payment; | ||||
| use App\Models\PaymentHash; | ||||
| 
 | ||||
| class PaymentWebhookRequest extends Request | ||||
| { | ||||
| @ -30,28 +33,61 @@ class PaymentWebhookRequest extends Request | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     public function company() | ||||
|     /** | ||||
|      * Resolve company gateway. | ||||
|      *  | ||||
|      * @param mixed $id  | ||||
|      * @return null|\App\Models\CompanyGateway  | ||||
|      */ | ||||
|     public function getCompanyGateway(): ?CompanyGateway | ||||
|     { | ||||
|         if (! $this->company_key) { | ||||
|             return false; | ||||
|         return CompanyGateway::where('gateway_key', $this->gateway_key)->firstOrFail(); | ||||
|     } | ||||
| 
 | ||||
|         return Company::query() | ||||
|             ->where('company_key', $this->company_key) | ||||
|             ->firstOrFail(); | ||||
|     } | ||||
| 
 | ||||
|     public function companyGateway() | ||||
|     /** | ||||
|      * Resolve payment hash. | ||||
|      *  | ||||
|      * @param string $hash  | ||||
|      * @return null|\App\Http\Requests\Payments\PaymentHash  | ||||
|      */ | ||||
|     public function getPaymentHash(): ?PaymentHash | ||||
|     { | ||||
|         if (! $this->gateway_key || ! $this->company_key) { | ||||
|             return false; | ||||
|         if ($this->query('hash')) { | ||||
|             return PaymentHash::where('hash', $this->query('hash'))->firstOrFail(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|         $company = $this->company(); | ||||
|     /** | ||||
|      * Resolve possible payment in the request. | ||||
|      *  | ||||
|      * @return null|\App\Models\Payment  | ||||
|      */ | ||||
|     public function getPayment(): ?Payment | ||||
|     { | ||||
|         $hash = $this->getPaymentHash(); | ||||
| 
 | ||||
|         return CompanyGateway::query() | ||||
|             ->where('gateway_key', $this->gateway_key) | ||||
|             ->where('company_id', $company->id) | ||||
|             ->firstOrFail(); | ||||
|         return $hash->payment; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Resolve client from payment hash. | ||||
|      *  | ||||
|      * @return null|\App\Models\Client  | ||||
|      */ | ||||
|     public function getClient(): ?Client | ||||
|     { | ||||
|         $hash = $this->getPaymentHash(); | ||||
| 
 | ||||
|         return Client::find($hash->data->client_id)->firstOrFail(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Resolve company from company_key parameter. | ||||
|      *  | ||||
|      * @return null|\App\Models\Company  | ||||
|      */ | ||||
|     public function getCompany(): ?Company | ||||
|     { | ||||
|         return Company::where('company_key', $this->company_key)->firstOrFail(); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user