bitpay / php-bitpay-client-v2

PHP implementation for the BitPay Cryptographically Secure RESTful API
MIT License
41 stars 41 forks source link

Redirection Issue: BitPay redirects to "example.com" #103

Closed VikBlockEater closed 1 year ago

VikBlockEater commented 2 years ago

I am currently working on BitPays Test environment. I have successfully instantiated BitPay client. After creating the invoice object using BitPay client, I am setting redirectURL, closeURL and autoRedirection attributes. As you can see in below code:

// creating BitPay client. Client instantiated successfully
$bitpay = BitPaySDK\Client::create()->withFile(base_path() . '/config/bitpay.json');

// creating invoice
$invoice = $bitpay->createInvoice(new \BitPaySDK\Model\Invoice\Invoice($this>balanceAmount, "USD"));

//setting rediect URL
$invoice->setRedirectURL("https://myappurl.com/wallet");

//setting close URL
$invoice->setCloseURL("https://myappurl.com/wallet");

// enabling auto redirection
$invoice->setAutoRedirect(true);

// get redirection url from invoice
$invoice_redirect_url = $invoice->getURL();

// upon click on livewire component redirect to invoice BitPay URL
return redirect($invoice_redirect_url);

In debug mode when I watch the $invoice object. I can see all the above parameters set correctly.

On the frontend when I click on the component, it redirects me to Bitpay's payment gateway interface. Here comes the issue, when I close the invoice without payment, it redirects me to https://example.com instead of my URL mentioned above.

VikBlockEater commented 2 years ago

@BitPay Team, I am officially awaited for your prompt response. I am in middle of a project and stuck at this point. I request you to please expedite.

bobbrodie commented 1 year ago

Hi there @VikBlockEater -- we're taking a look at this. Could you share the following?

Questions

Regarding the following:

On the frontend when I click on the component, it redirects me to Bitpay's payment gateway interface. Here comes the issue, when I close the invoice without payment, it redirects me to https://example.com/ instead of my URL mentioned above.

Could you describe which frontend you're referring to?

Notes on the provided example

I do believe I see a few other issues in your example as well.

There may be a typo in this part here, where it should be $this->balanceAmount where you have $this>balanceAmount:

// creating invoice
$invoice = $bitpay->createInvoice(new \BitPaySDK\Model\Invoice\Invoice($this>balanceAmount, "USD"));

It's also important to note that this line is what creates the invoice, and you're setting values on it after it's created, which will not trigger an update. It would also be a best practice to pass a currency constant rather than a direct string. You don't have to, it's preferable to pass Currency::USD instead of "USD".

I believe what you may want to consider is creating an Invoice object, setting its values, and then passing that entire object into the createInvoice function. This is an abbreviated example, but you could consider:

// ...
$invoice = new Invoice($this->balanceAmount, Currency::USD);
$invoice->setRedirectURL("https://myappurl.com/wallet");
$invoice->setCloseURL("https://myappurl.com/wallet");
$invoice->setAutoRedirect(true);
// Set any other properties you need
$bitpay->createInvoice($invoice);
// ...

Please let me know if this is helpful, thanks!

bobbrodie commented 1 year ago

Hi @VikBlockEater can you confirm if your issue has been resolved? If so, we will close this out. Thank you!

bobbrodie commented 1 year ago

Hi @VikBlockEater we're going to close this for now, but if you have any questions please create another issue or reach out to support@bitpay.com.

VikBlockEater commented 1 year ago

First of all apologies for delayed response. I actually waited for your response but after that I got busy in some other professional commitments. Anyhow please consider below details.

Are you using dev-master or a tagged version?

Its v6.0x.

Which version of PHP are you on?

8.0 on staging and 8.1 on local (but issue is same on both versions).

Are you using the SDK directly, or are any other frameworks / platforms involved? I noticed you mentioned livewire, is that this framework, specifically?

Yes its Laravel Livewire.

There may be a typo in this part here, where it should be $this->balanceAmount where you have $this>balanceAmount

Yes I realised it earlier and rectified that but issue was still there.

I believe what you may want to consider is creating an Invoice object, setting its values, and then passing that entire object into the createInvoice function.

As per your suggestion I refactored my code as below

`// creating BitPay client. Client instantiated successfully $bitpay = BitPaySDK\Client::create()->withFile(base_path() . '/config/bitpay.json');

// creating invoice object $invoice = new \BitPaySDK\Model\Invoice\Invoice($this->balanceAmount, \BitPaySDK\Model\Currency::USD);

// setting redirect url on invoice $invoice->setRedirectURL("https://myapp.com/");

// setting redirect url on closing the invoice $invoice->setCloseURL("https://myapp.com/");

// setting auto redirect to true $invoice->setAutoRedirect(true);

// creating invoice $bitpay->createInvoice($invoice);

// the url of the invoice to be redirected to BitPay $invoice_redirect_url = $invoice->getURL();

// upon click on livewire component redirect to invoice BitPay URL return redirect($invoice_redirect_url); ` After implementing the above change, I observed difference in the Invoice object. Detailed after vs before comparison could be found here https://docs.google.com/spreadsheets/d/1Vxnuh3RTPuSY4EBMSOlufPG0wPv661qetab_oM6QHGs/edit?usp=sharing

Conclusively new code (along with other differences in response) is not generating the invoice URL.

If you need some other clarification then feel free to communicate back.

@bobbrodie Thank you very much your supportive response.

VikBlockEater commented 1 year ago

@bobbrodie Your precious response is awaited.