1 package TestComplexApp::Model::PaymentProvider;
3 use Moose::Meta::Class;
4 use namespace::autoclean;
6 extends 'Catalyst::Model';
10 PaymentApp::Model::PaymentProvider - Provider Model factory
14 This module creates dynamic Catalyst Models based on configured
15 payment providers and the requested payment profile.
17 The Model instance is created at request time, not at setup time, and
18 so we can inspect the message to see which enterprise and profile is
19 required. We then load that profile using the ProfileDB Model, and
20 instantiate the Model. The corresponding generated controller then
21 invokes the appropriate method on this Model.
23 We create a Model class per configured payment provider, and we
24 require this configuration:
26 Model::PaymentProvider:
28 - PaymentProvider::Datacash
29 - PaymentProvider::Cybersource
30 - PaymentProvider::Null
32 The Model instances may be accessed like so:
34 $c->model('PaymentProvider::Datacash')
36 with the enterprise and profile names in the stash.
40 More graceful handling of missing enterprise and/or profile. We should
41 do something other than just not return a model.
45 PaymentApp::Controller::PaymentProvider - this module's partner in crime.
50 my $self = shift->next::method(@_);
52 my $provider_classes = $self->{providers};
54 for my $provider_class (@$provider_classes) {
55 eval "require $provider_class";
57 die "loading $provider_class: $@";
60 my $provider_model_name = "PaymentApp::Model::${provider_class}";
61 my $provider = Moose::Meta::Class->create($provider_model_name);
62 $provider->add_method('ACCEPT_CONTEXT',
65 my $profile = $self->load_profile($c);
66 my $provider_model = $provider_class->new($profile);
67 return $provider_model;
74 # Load the given profile and instantiate the provider
78 my $profile_name = $c->stash->{profile};
79 my $enterprise_name = $c->stash->{enterprise};
81 my $enterprise = $c->model('ProfileDB::Enterprise')->single(
82 {name => $enterprise_name}
84 my $profile = $c->model('ProfileDB::Profile')->single(
85 {name => $profile_name, enterprise_id => $enterprise->id}
88 my $profile_data = {};
89 my $set_rs = $c->model('ProfileDB::ProfileSetting')->search(
90 {profile_id => $profile->id}
92 while (my $set = $set_rs->next) {
93 $profile_data->{$set->name} = $set->value;