colinodell / omnipay-bundle

Omnipay bundle for Symfony 2.3+ and 3.0+
MIT License
7 stars 12 forks source link

Question: How to get parameters for a custom gateway? #10

Open pbalan opened 6 years ago

pbalan commented 6 years ago

Hi,

Thanks for the great work. Really appreciate it.

I am trying to use this bundle in one of my project as follows:

config.yml

omnipay:
methods:
WechatPay_Mweb:
app_id: '%wechat_app_id%' #app_id
app_key: '%wechat_app_key%' #app_key
mch_id: '%wechat_mch_id%' #merchant_id
notify_url: '%wechat_notify_url%' #notify_url
Alipay_AopPage:
appId: '%alipay_app_id%' #app-id
appKey: '%alipay_app_key%' #appKey
signType: '%alipay_sign_type%' #signType
privateKey: '%alipay_private_key%' #privateKey
alipayPublicKey: '%alipay_public_key%' #publicKey
notifyUrl: '%alipay_notify_url%' #returnUrl

services.yml


service.pay.alipay:
class: Omnipay\Alipay\AopPageGateway
tags:
- { name: omnipay.gateway, alias: Alipay_AopPage }

service.pay.wechat: class: Omnipay\WechatPay\MwebGateway tags:

service.process.payment: class: AppBundle\Service\ProcessPayment arguments:

PaymentController.php This controller has a route which determines transaction and paymentType and passes to ProcessPayment Service

$gatewayResponse = $this->processPaymentService->pay($transaction, $paymentType);

ProcessPayment.php


/**
* ProcessPayment constructor.
* @param DocumentManager $dm
* @param Omnipay $omnipay
*/
public function __construct(
DocumentManager $dm,
Omnipay $omnipay,
) {
$this->dm = $dm;
$this->omnipay = $omnipay;
}
/**
 * @param Document\Transaction $transaction
 * @param Document\PaymentType $paymentType
 * @throws \Exception
 * @return array
 */
public function pay(Document\Transaction $transaction, Document\PaymentType $paymentType)
{
     $paymentGateway = null;
     switch ($paymentType) {
           case: 'WECHAT': $paymentGateway = $this->omnipay->get('WechatPay_Mweb');
                  break;
           case: 'ALIPAY': $paymentGateway = $this->omnipay->get('Alipay_AopPage');
                  break;
           default:
                  throw new \Exception('Requested payment gateway is not implemented.', 401);
                  break;
     }
    if (null !== $paymentGateway) {
            $params = $paymentGateway->getDefaultParameters();
            dump($params);
            $order = [
                 'app_id'            => '',
                 'mch_id'            => '',
                 'notify_url'        => '',
                 'body'              => 'The test order',
                 'out_trade_no'      => $transaction->getId(),
                 'total_fee'         => 1, //=0.01
                 'spbill_create_ip'  => '127.0.0.1',
                 'fee_type'          => 'CNY'
            ];
            $request = $paymentGateway->purchase($order);
            $response = $request->send();
            dump($response->getData());
    }
}

### Current result:
dump($params) returns [] (An empty array)

### Expected result:
   Array of parameters configured in `config.yml`
colinodell commented 6 years ago

What happens if you replace getDefaultParameters() with getParameters()?

         if (null !== $paymentGateway) {
-                $params = $paymentGateway->getDefaultParameters();
+                $params = $paymentGateway->getParameters();
                 dump($params);

Does that return the output you'd expect?

pbalan commented 6 years ago

@colinodell It doesn't either. Not sure if its an issue with the custom gateway. https://github.com/lokielse/omnipay-wechatpay/blob/master/src/MwebGateway.php There's no way to set parameters. Although https://github.com/lokielse/omnipay-wechatpay/blob/master/src/BaseAbstractGateway.php has a way to set params.

I ended up changing my service definitions as below:

service.pay.alipay:
        class: Omnipay\Alipay\AopPageGateway
        calls:
            - ['setAppId', ['%alipay_app_id%']]
            - ['setSignType', ['%alipay_sign_type%']]
            - ['setPrivateKey', ['%alipay_private_key%']]
            - ['setAlipayPublicKey', ['%alipay_public_key%']]
            - ['setNotifyUrl', ['%alipay_notify_url%']]
        tags:
            - { name: omnipay.gateway, alias: Alipay_AopPage }

    service.pay.wechat:
        class: Omnipay\WechatPay\MwebGateway
        calls:
            - ['setAppId', ['%wechat_app_id%']]
            - ['setApiKey', ['%wechat_api_key%']]
            - ['setMchId', ['%wechat_mch_id%']]
            - ['setNotifyUrl', ['%wechat_notify_url%']]
        tags:
            - { name: omnipay.gateway, alias: WechatPay_Mweb }

And for now, I am passing in these services manually to ProcessPayment service.

Ideally, I would prefer using one interface that is your bundle to set parameters.

pbalan commented 6 years ago

Any ideas?

colinodell commented 6 years ago

Sorry @pbalan, things have been quite busy for me lately. I'll try to take a closer look at this over the next few days if I get some time.

Meanwhile, if you'd like to continue debugging this, I'd recommend trying to test your code with the same payment gateway and configuration in our README file - see if you're able to access those parameters in your ProcessPayment class. If that does work, I'd guess the problem is one of these two things:

  1. The 2 gateways you're using don't work like the others
  2. Double-check your config.yml for any small typos, indentation issues, etc.

Also, could you tell me if this issue is affecting both of the payment gateways or just one of them?