Skip to content

Commit

Permalink
allow to override thank you url via query param (#40)
Browse files Browse the repository at this point in the history
* allow to override thank you url via query param

* [docs][api][Subscriptions] document redirect query param

* [docs][api][Subscriptions] document redirect query param

* improved subscriptions api documentation

* make url aware of redirect query param in index.html.twig
  • Loading branch information
takeit committed Nov 28, 2017
1 parent 339fdb8 commit 9dc0a80
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/api/source/includes/public_api/_subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Once the payment transaction is finalized successfully the subscription state is
```shell
curl -X GET \
http://localhost/public-api/v1/subscriptions/lKD1QhGtjW/pay/ \
http://localhost/public-api/v1/subscriptions/lKD1QhGtjW/pay/?redirect=http://example.com/sub \
-H 'content-type: application/json' \
```

Expand All @@ -165,6 +165,7 @@ curl -X GET \
Name | Type | Description
--------- | ------- | -----------
tokenValue \(`required`)| string | The token value of the subscription to be paid (can be retrieved from Subscriptions API). It is generated once the subscription is created.
redirect \(`required`)| string | The URL you provide to redirect the customer to a url after payment is completed, failed or cancelled. Providing this parameter will override the default redirect urls.

### Returns

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ ph_public_api_payum_subscription_pay:
route: ph_public_api_payum_subscription_after_pay
parameters:
version: $version
redirect: $redirect

ph_public_api_payum_subscription_after_pay:
path: /subscriptions/after-pay/
Expand Down
3 changes: 3 additions & 0 deletions src/PH/Bundle/CoreBundle/Resources/views/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
dataType: "json"
}).done(function( data ) {
window.location.replace('/public-api/v1/subscriptions/'+data.token_value+'/pay/');
// or
// window.location.replace('/public-api/v1/subscriptions/'+data.token_value+'/pay/?redirect=http://example.com');
// to redirect always to the given url
console.log('done');
}).fail(function () {
alert('error when making a purchase');
Expand Down
15 changes: 11 additions & 4 deletions src/PH/Bundle/PayumBundle/Controller/PayumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,25 @@ public function afterCaptureAction(Request $request): Response
$status = new GetStatus($token);

$this->payum->getGateway($token->getGatewayName())->execute($status);
$resolveNextUrl = $this->resolveNextUrlFactory->createNewWithModel($status->getFirstModel());
$this->payum->getGateway($token->getGatewayName())->execute($resolveNextUrl);

if (!$request->query->has('redirect')) {
$resolveNextUrl = $this->resolveNextUrlFactory->createNewWithModel($status->getFirstModel());
$this->payum->getGateway($token->getGatewayName())->execute($resolveNextUrl);
$redirect = $resolveNextUrl->getUrl();
} else {
$redirect = $request->query->get('redirect');
}

$this->getHttpRequestVerifier()->invalidate($token);

$view = View::createRedirect($resolveNextUrl->getUrl());
$view = View::createRedirect($redirect);

return $this->viewHandler->handle($configuration, $view);
}

/**
* @param Request $request
* @param string $subscription
* @param string $subscriptionId
* @param string $id
*
* @return Response
Expand Down

0 comments on commit 9dc0a80

Please sign in to comment.