我正在尝试使用PayumBundle的Omnipay Bridge捕获InvalidCreditCardException
和任何其他异常。
我试过:
try {
return $this->forward('PayumBundle:Capture:do', array(
'payum_token' => $captureToken,
));
} catch ('Exception $e) {
$exceptions = array();
do {
$exceptions[] = $e->getMessage();
} while ($e = $e->getPrevious());
$response = new Response(json_encode(array('status' => 'failed', 'Message' => $exceptions)));
return $response;
}
但在代码到达我的捕获点之前,Omnipay
抛出了自己的信用卡号无效的异常。
在评论中,它还说:
通常,如果你想用自定义错误消息自己验证信用卡,你应该使用框架的验证库,而不是这种方法。
这正是我想要做的,如何使用Symfony PayumBundle异常验证带有自定义错误的卡?
请不要像下面的第一个答案那样通过提供参考来回答这个问题。我非常感谢他的帮助,但这对我如何在代码中使用该异常没有帮助。
非常重要
我正在努力更好地理解PayumBundle,所以我需要了解我做错了什么,以及我应该采取的步骤(不是用英语,而是用带解释的真实示例代码),以抓住我的操作中的异常并显示用户友好的消息。请理解,我对symfony真的很陌生,我正在努力学习,如果你要让我创建一个服务并做这个或那个,那对我没有帮助,请看这里的例子,我真的很感激你的帮助,但我真的真的很失落
请注意,如果输入了正确的信用号码,交易就会顺利进行,这只是当输入了错误的卡号、卡过期或其他任何情况时我需要捕捉的例外
如果有帮助的话,这就是我的config.yml看起来像的样子
contexts:
paypal:
paypal_express_checkout_nvp:
username: %paypal_username%
password: %paypal_password%
signature: %paypal_signature%
sandbox: %paypal_sandbox%
stripe_omnipay:
omnipay:
type: Stripe
options:
apiKey: xxxx
testMode: true
extensions:
PaymentExtension:
class: Payum'Core'Extension'PaymentExtention
您可以使用payum扩展onException消息。在该方法中,您可以捕获异常并将其信息添加到付款详细信息中。
您需要添加一个将由Payum'Core'Payment
对象执行的扩展。正如您在这段代码中看到的,异常被捕获,然后适当的扩展将依次执行:https://github.com/Payum/Core/blob/master/Payment.php#L104-L108
您需要创建一个实现Payum'Core'Extension'ExtensionInterface
的新类,并在onException()
方法中实现所需的逻辑。
以下是一个扩展示例:
class YourExtension implements Payum'Core'Extension'ExtensionInterface
{
/**
* @param mixed $request
*/
public function onPreExecute($request) {}
/**
* @param mixed $request
* @param 'Payum'Core'Action'ActionInterface $action
*/
public function onExecute($request, ActionInterface $action) {}
/**
* @param mixed $request
* @param 'Payum'Core'Action'ActionInterface $action
*/
public function onPostExecute($request, ActionInterface $action) {}
/**
* @param 'Payum'Core'Reply'ReplyInterface $reply
* @param mixed $request
* @param 'Payum'Core'Action'ActionInterface $action
*
* @return null|'Payum'Core'Reply'ReplyInterface an extension able to change reply to something else.
*/
public function onReply(ReplyInterface $reply, $request, ActionInterface $action) {}
/**
* @param 'Exception $exception
* @param mixed $request
* @param 'Payum'Core'Action'ActionInterface|null $action
*/
public function onException('Exception $exception, $request, ActionInterface $action = null)
{
// Put your code here
}
}
然后,在你的应用程序配置文件中,你必须添加该扩展,这样它才能被框架加载和执行。确切的答案取决于您用作配置文件的格式类型(yml、php、ini、?)。
下面是yml的一个例子(官方文档在这里btw:https://github.com/Payum/Payum/blob/master/docs/symfony/container-tags.md):
payum:
contexts:
yourContextName:
PaymentFactoryName:
extensions:
YourCustomExtension:
class: Payum'Core'Extension'YourExtension
在这个配置文件中,您应该:
- 用您想要的任何内容替换
yourContextName
- 将
PaymentFactoryName
替换为您所在的支付工厂的名称使用,以下是一个列表:https://github.com/Payum/PayumBundle/tree/master/DependencyInjection/Factory/Payment - 将
YourCustomExtension
替换为您要为扩展