1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Tests for rehydrating order for payment

This commit is contained in:
David Bomba 2023-12-13 20:01:34 +11:00
parent 37cc0cf442
commit 95a8c2510f
4 changed files with 175 additions and 20 deletions

View File

@ -39,12 +39,22 @@ class PayPalWebhook implements ShouldQueue
private $gateway_key = '80af24a6a691230bbec33e930ab40666';
private string $test_endpoint = 'https://api-m.sandbox.paypal.com';
private string $endpoint = 'https://api-m.paypal.com';
public function __construct(protected array $webhook_request, protected array $headers, protected string $access_token)
{
}
public function handle()
{
//testing
$this->endpoint = $this->test_endpoint;
//this can cause problems verifying the webhook, so unset it if it exists
if(isset($this->webhook_request['q']))
unset($this->webhook_request['q']);
if($this->verifyWebhook()) {
nlog('verified');
@ -53,9 +63,10 @@ class PayPalWebhook implements ShouldQueue
'CHECKOUT.ORDER.COMPLETED' => $this->checkoutOrderCompleted(),
};
return;
}
nlog(" NOT VERIFIED ");
}
/*
'id' => 'WH-COC11055RA711503B-4YM959094A144403T',
@ -293,27 +304,26 @@ class PayPalWebhook implements ShouldQueue
//--------------------------------------------------------------------------------------//
private function verifyWebhook(): bool
{
{nlog($this->headers);
$request = [
'auth_algo' => $this->headers['paypal-auth-algo'],
'cert_url' => $this->headers['paypal-cert-url'],
'transmission_id' => $this->headers['paypal-transmission-id'],
'transmission_sig' => $this->headers['paypal-transmission-sig'],
'transmission_time' => $this->headers['paypal-transmission-time'],
'auth_algo' => $this->headers['paypal-auth-algo'][0],
'cert_url' => $this->headers['paypal-cert-url'][0],
'transmission_id' => $this->headers['paypal-transmission-id'][0],
'transmission_sig' => $this->headers['paypal-transmission-sig'][0],
'transmission_time' => $this->headers['paypal-transmission-time'][0],
'webhook_id' => config('ninja.paypal.webhook_id'),
'webhook_event' => $this->webhook_request,
'webhook_event' => $this->webhook_request
];
nlog($request);
$headers = [
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Accept-Language' => 'en_US',
'PayPal-Partner-Attribution-Id' => 'invoiceninja_SP_PPCP',
];
$r = Http::withToken($this->access_token)
->withHeaders($headers)
->post("https://api-m.paypal.com/v1/notifications/verify-webhook-signature", $request);
->post("{$this->endpoint}/v1/notifications/verify-webhook-signature", $request);
nlog($r);
nlog($r->json());

View File

@ -443,7 +443,7 @@ class PayPalPPCPPaymentDriver extends BaseDriver
$r = $this->gatewayRequest('/v2/checkout/orders', 'post', $order);
nlog($r->json());
// nlog($r->json());
return $r->json()['id'];
@ -499,8 +499,8 @@ class PayPalPPCPPaymentDriver extends BaseDriver
->withHeaders($this->getHeaders($headers))
->{$verb}("{$this->api_endpoint_url}{$uri}", $data);
nlog($r);
nlog($r->json());
// nlog($r);
// nlog($r->json());
if($r->successful()) {
return $r;
@ -538,8 +538,8 @@ class PayPalPPCPPaymentDriver extends BaseDriver
public function processWebhookRequest(Request $request)
{
// nlog($request->all());
// nlog($request->headers->all());
nlog(json_encode($request->all()));
$this->init();
PayPalWebhook::dispatch($request->all(), $request->headers->all(), $this->access_token);

View File

@ -53,11 +53,9 @@
return actions.restart();
}
// return actions.order.capture().then(function(details) {
document.getElementById("gateway_response").value =JSON.stringify( data );
// document.getElementById("gateway_response").value =JSON.stringify( details );
document.getElementById("server_response").submit();
// });
},
onCancel: function() {
window.location.href = "/client/invoices/";
@ -65,6 +63,9 @@
onError: function(error) {
document.getElementById("gateway_response").value = error;
document.getElementById("server_response").submit();
},
onClick: function (){
document.getElementById('paypal-button-container').hidden = true;
}
}).render('#paypal-button-container').catch(function(err) {

View File

@ -0,0 +1,144 @@
<?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://www.elastic.co/licensing/elastic-license
*/
namespace Tests\Feature\PayPal;
use stdClass;
use Tests\TestCase;
use App\Models\Invoice;
use Tests\MockAccountData;
use App\Models\GatewayType;
use App\Models\PaymentHash;
use Illuminate\Support\Str;
use App\Models\CompanyGateway;
use App\DataMapper\FeesAndLimits;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class WebhookTest extends TestCase
{
use DatabaseTransactions;
use MockAccountData;
private $webhook_string = '{"id":"WH-8WP702374D398111T-81807959NA3371206","event_version":"1.0","create_time":"2023-12-13T08:36:03.961Z","resource_type":"checkout-order","resource_version":"2.0","event_type":"CHECKOUT.ORDER.COMPLETED","summary":"Checkout Order Completed","resource":{"update_time":"2023-12-13T08:35:27Z","create_time":"2023-12-13T08:35:18Z","purchase_units":[{"reference_id":"default","amount":{"currency_code":"USD","value":"1285.13","breakdown":{"item_total":{"currency_code":"USD","value":"1285.13"}}},"payee":{"merchant_id":"KDCGGYWFNWTAN"},"payment_instruction":{"disbursement_mode":"INSTANT"},"description":"Invoice Number# fq30028","custom_id":"xLqrlFTUHJONFhSDhSUZBp0ckeZnpdFq","invoice_id":"fq30028","soft_descriptor":"NOVEMBER 6","items":[{"name":"Invoice Number# fq30028","unit_amount":{"currency_code":"USD","value":"1285.13"},"quantity":"1","description":"Ut totam facilis.Ut totam facilis.Ut totam facilis."}],"shipping":{"name":{"full_name":"John Doe"},"address":{"address_line_1":"1 Main St","admin_area_2":"San Jose","admin_area_1":"CA","postal_code":"95131","country_code":"US"}},"payments":{"captures":[{"id":"40A1323403146010F","status":"COMPLETED","amount":{"currency_code":"USD","value":"1285.13"},"final_capture":true,"disbursement_mode":"INSTANT","seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"USD","value":"1285.13"},"paypal_fee":{"currency_code":"USD","value":"45.34"},"net_amount":{"currency_code":"USD","value":"1239.79"}},"invoice_id":"fq30028","custom_id":"xLqrlFTUHJONFhSDhSUZBp0ckeZnpdFq","links":[{"href":"https:\\/\\/api.sandbox.paypal.com\\/v2\\/payments\\/captures\\/40A1323403146010F","rel":"self","method":"GET"},{"href":"https:\\/\\/api.sandbox.paypal.com\\/v2\\/payments\\/captures\\/40A1323403146010F\\/refund","rel":"refund","method":"POST"},{"href":"https:\\/\\/api.sandbox.paypal.com\\/v2\\/checkout\\/orders\\/5WX67707S1265192L","rel":"up","method":"GET"}],"create_time":"2023-12-13T08:35:27Z","update_time":"2023-12-13T08:35:27Z"}]}}],"links":[{"href":"https:\\/\\/api.sandbox.paypal.com\\/v2\\/checkout\\/orders\\/5WX67707S1265192L","rel":"self","method":"GET"}],"id":"5WX67707S1265192L","payment_source":{"paypal":{"email_address":"sb-0kvkf26397832@personal.example.com","account_id":"4X5WHWAP5GQ3Y","account_status":"VERIFIED","name":{"given_name":"John","surname":"Doe"},"address":{"address_line_1":"62158","address_line_2":"341 Colton Canyon","admin_area_2":"Port Lisandro","admin_area_1":"New Jersey","postal_code":"08127","country_code":"GR"}}},"intent":"CAPTURE","payer":{"name":{"given_name":"John","surname":"Doe"},"email_address":"sb-0kvkf26397832@personal.example.com","payer_id":"4X5WHWAP5GQ3Y","address":{"address_line_1":"62158","address_line_2":"341 Colton Canyon","admin_area_2":"Port Lisandro","admin_area_1":"New Jersey","postal_code":"08127","country_code":"GR"}},"status":"COMPLETED"},"links":[{"href":"https:\\/\\/api.sandbox.paypal.com\\/v1\\/notifications\\/webhooks-events\\/WH-8WP702374D398111T-81807959NA3371206","rel":"self","method":"GET"},{"href":"https:\\/\\/api.sandbox.paypal.com\\/v1\\/notifications\\/webhooks-events\\/WH-8WP702374D398111T-81807959NA3371206\\/resend","rel":"resend","method":"POST"}],"q":"\\/api\\/v1\\/ppcp\\/webhook"}';
private string $merchant_id = 'KDCGGYWFNWTAN';
private string $invoice_number = 'fq30028';
private float $amount = 1285.13;
protected function setUp() :void
{
parent::setUp();
$this->makeTestData();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
public function testWebhooks()
{
$hook = json_decode($this->webhook_string, true);
$this->assertIsArray($hook);
}
public function testPaymentCreation()
{
$hook = json_decode($this->webhook_string, true);
$company_gateway = $this->buildGateway();
$invoice = Invoice::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'number' => $this->invoice_number,
'status_id' => 2,
'amount' => $this->amount,
'balance' => $this->amount,
]);
$hash_data = [
'invoices' => [
['invoice_id' => $invoice->hashed_id, 'amount' => $this->amount],
],
'credits' => 0,
'amount_with_fee' => $this->amount,
'pre_payment' => false,
'frequency_id' => null,
'remaining_cycles' => null,
'is_recurring' => false,
];
$payment_hash = new PaymentHash;
$payment_hash->hash = Str::random(32);
$payment_hash->data = $hash_data;
$payment_hash->fee_total = 0;
$payment_hash->fee_invoice_id = $invoice->id;
$payment_hash->save();
$driver = $company_gateway->driver($this->client)->init();
$driver->setPaymentHash($payment_hash);
$source = 'paypal';
$transaction_reference = $hook['resource']['purchase_units'][0]['payments']['captures'][0]['id'];
$amount = $hook['resource']['purchase_units'][0]['payments']['captures'][0]['amount']['value'];
$data = [
'payment_type' => 3,
'amount' => $amount,
'transaction_reference' => $transaction_reference,
'gateway_type_id' => GatewayType::PAYPAL,
];
$payment = $driver->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
$this->assertNotNull($payment);
}
private function buildGateway()
{
$config = new \stdClass;
$config->merchantId = $this->merchant_id;
$config->status = 'activated';
$config->consent = 'true';
$config->emailVerified = 'true';
$config->permissions = 'true';
$config->returnMessage = 'true';
$config->paymentsReceivable = 'Yes';
$cg = new CompanyGateway;
$cg->company_id = $this->company->id;
$cg->user_id = $this->user->id;
$cg->gateway_key = '80af24a6a691230bbec33e930ab40666';
$cg->require_cvv = true;
$cg->require_billing_address = true;
$cg->require_shipping_address = true;
$cg->update_details = true;
$cg->config = encrypt($config);
$cg->save();
$fees_and_limits = new stdClass;
$fees_and_limits->{3} = new FeesAndLimits;
$cg->fees_and_limits = $fees_and_limits;
$cg->save();
return $cg;
}
}