srmklive / laravel-paypal

Laravel plugin for processing payments through PayPal.
https://laravel-paypal-demo.srmk.info/
MIT License
1.03k stars 314 forks source link

Add custom id for invoice id #603

Closed buzzclue closed 6 months ago

buzzclue commented 9 months ago

Allow to add custom id for invoice id.

srmklive commented 9 months ago

Can you specify why we need to add it? Any example you can share?

buzzclue commented 9 months ago

You can set any custom data like Invoice ID, Transaction ID so you can tract the resource on webhook.

For example:

$invoice = Invoice::create([...]);
$response = $provider->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
            ->addPlanTrialPricing('DAY', 7)
            ->addWeeklyPlan('Demo Plan', 'Demo Plan', 30)
            ->addCustomId($invoice->id)
            ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
            ->setupSubscription('John Doe', 'john@example.com', '2021-12-10');

Now on webhook i can track this custom_id and process the invoice.

One can even pass the http_build_query query data in custom_id. This way you don't even have to add temporary records in database and process everything on webhooks

$response = $provider->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
            ->addPlanTrialPricing('DAY', 7)
            ->addWeeklyPlan('Demo Plan', 'Demo Plan', 30)
            ->addCustomId(http_build_query(['user' => $user->id, 'plan' => $plan->id, ... 'something_else' => 'other data']))
            ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
            ->setupSubscription('John Doe', 'john@example.com', '2021-12-10');
buzzclue commented 8 months ago

For some reasons the WH verification fails when $request->all() is sent in webhook_event. I have tested the verification successfully with json_decode($request->getContent())

If you want to test you can try the webhook PAYMENT.SALE.COMPLETED