【问题标题】:Yii2 paypal payment integrationYii2 贝宝支付集成
【发布时间】:2019-09-28 06:29:21
【问题描述】:

我正在使用 https://www.yiiframework.com/extension/bitcko/yii2-bitcko-paypal-api#usage 和 yii2 来启用付款,我的代码如下所示。

public function actionMakePayment(){
          if(!Yii::$app->user->getIsGuest()){
               // Setup order information array
              $params = [
                  'order'=>[
                      'description'=>'Payment description',
                      'subtotal'=>45,
                      'shippingCost'=>0,
                      'total'=>45,
                      'currency'=>'USD',
                  ]
              ];
            // In case of payment success this will return the payment object that contains all information about the order
            // In case of failure it will return Null

            Yii::$app->PayPalRestApi->processPayment($params);
        }else{
          Yii::$app->response->redirect(Url::to(['site/signup'], true));
        }

一切都按照我的预期进行,这个调用将这样的东西返回给 dom。

{ "id": "PAYID-LTKUAVA8WK14445NN137182H", "intent": "sale", "state": "approved", "cart": "9RE74926AX5730813", "payer": { "payment_method": "paypal", "status": "UNVERIFIED", "payer_info": { "first_name": "Susi", "last_name": "Flo", "payer_id": "KWPDGYRP2KCK4", "shipping_address": { "recipient_name": "Susi Flo", "line1": "Suso", "line2": "bldg", "city": "Spring hill", "state": "FL", "postal_code": "34604", "country_code": "US" }, "phone": "3526003902", "country_code": "US" } }, "transactions": [ { "amount": { "total": "45.00", "currency": "USD", "details": { "subtotal": "45.00", "shipping": "0.00", "insurance": "0.00", "handling_fee": "0.00", "shipping_discount": "0.00" } }, "payee": { "merchant_id": "NHN6S6KT4FF6N", "email": "arunwebber2-facilitator@gmail.com" }, "description": "Payment description", "invoice_number": "5cd5404d624a9", "soft_descriptor": "PAYPAL *TESTFACILIT", "item_list": { "items": [ { "name": "Item one", "price": "45.00", "currency": "USD", "tax": "0.00", "quantity": 1 } ], "shipping_address": { "recipient_name": "Susi Flo", "line1": "Suso", "line2": "bldg", "city": "Spring hill", "state": "FL", "postal_code": "34604", "country_code": "US" } }, "related_resources": [ { "sale": { "id": "6LN25215GP1183020", "state": "completed", "amount": { "total": "45.00", "currency": "USD", "details": { "subtotal": "45.00", "shipping": "0.00", "insurance": "0.00", "handling_fee": "0.00", "shipping_discount": "0.00" } }, "payment_mode": "INSTANT_TRANSFER", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "transaction_fee": { "value": "2.43", "currency": "USD" }, "receipt_id": "3896118010137330", "parent_payment": "PAYID-LTKUAVA8WK14445NN137182H", "create_time": "2019-05-10T09:30:10Z", "update_time": "2019-05-10T09:30:10Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/6LN25215GP1183020", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/6LN25215GP1183020/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LTKUAVA8WK14445NN137182H", "rel": "parent_payment", "method": "GET" } ], "soft_descriptor": "PAYPAL *TESTFACILIT" } } ] } ], "create_time": "2019-05-10T09:11:48Z", "update_time": "2019-05-10T09:30:10Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LTKUAVA8WK14445NN137182H", "rel": "self", "method": "GET" } ] }

如何将其存储到我的数据库中?对于特定的用户 ID,我可以使用此获取用户 ID。

echo Yii::$app->user->id;

我想将此值与用户 ID 一起存储,我该怎么做?并向用户发送付款成功消息:)

【问题讨论】:

    标签: php paypal yii2 yii2-advanced-app


    【解决方案1】:

    Paypal PHP-SDK 为您提供setCustom()添加自定义字段值,您可以使用它发送用户id,然后在执行支付后在交易对象中检索它。

    您使用的只是使用 Paypal SDK 功能的自定义组件,您应该扩展类 bitcko\paypalrestapi\PayPalRestApi.php 以覆盖函数 checkOut() 并将 ->setCustom(Yii::$app->user->id) 添加到此 line 中的链中,因为它不提供任何设置自定义字段的方法,因此只需将该方法的整个代码复制到您的新类中并添加上述内容。

    您的课程应如下所示。

    注意:将文件添加到 common/components 文件夹中。

    <?php
    namespace common\components;
    
    use bitcko\paypalrestapi\PayPalRestApi as PayPalBase;
    use PayPal\Api\Amount;
    use PayPal\Api\Details;
    use PayPal\Api\Item;
    use PayPal\Api\ItemList;
    use PayPal\Api\Payer;
    use PayPal\Api\Payment;
    use PayPal\Api\RedirectUrls;
    use PayPal\Api\Transaction;
    use PayPal\Exception\PayPalConnectionException;
    use yii\helpers\Url;
    use Yii;
    
    class PaypalRestApi extends PayPalBase
    {
    
        public function checkOut($params)
        {
            $payer = new Payer();
            $payer->setPaymentMethod($params['method']);
            $orderList = [];
    
            foreach ($params['order']['items'] as $orderItem) {
                $item = new Item();
                $item->setName($orderItem['name'])
                    ->setCurrency($orderItem['currency'])
                    ->setQuantity($orderItem['quantity'])
                    ->setPrice($orderItem['price']);
                $orderList[] = $item;
            }
            $itemList = new ItemList();
            $itemList->setItems($orderList);
            $details = new Details();
            $details->setShipping($params['order']['shippingCost'])
                ->setSubtotal($params['order']['subtotal']);
            $amount = new Amount();
            $amount->setCurrency($params['order']['currency'])
                ->setTotal($params['order']['total'])
                ->setDetails($details);
            $transaction = new Transaction();
            $transaction->setAmount($amount)
                ->setItemList($itemList)
                ->setDescription($params['order']['description'])
                ->setCustom(Yii::$app->user->id)
                ->setInvoiceNumber(uniqid());
    
            $redirectUrl = Url::to([$this->redirectUrl], true);
            $redirectUrls = new RedirectUrls();
            $redirectUrls->setReturnUrl("$redirectUrl?success=true")
                ->setCancelUrl("$redirectUrl?success=false");
            $payment = new Payment();
            $payment->setIntent($params['intent'])
                ->setPayer($payer)
                ->setRedirectUrls($redirectUrls)
                ->setTransactions(array($transaction));
            try {
                $payment->create($this->apiContext);
                return \Yii::$app->controller->redirect($payment->getApprovalLink());
            } catch (PayPalConnectionException $ex) {
                // This will print the detailed information on the exception.
                //REALLY HELPFUL FOR DEBUGGING
                \Yii::$app->response->format = \yii\web\Response::FORMAT_HTML;
                \Yii::$app->response->data = $ex->getData();
            }
        }
    }
    

    现在将common/config/main.phpfrontend/config/main.php 中的PayPalRestApi 组件类的配置更改为您创建的新类

    'components'=> [
        ...
     'PayPalRestApi'=>[
          'class'=>'common\components\PayPalRestApi',
      ]
        ...
    ]
    

    所以现在您可以使用

    获得相同的用户 ID
    $response = \yii\helpers\Json::decode( Yii::$app->PayPalRestApi->processPayment($params));
    $user_id = $response['transactions'][0]['custom'];
    

    【讨论】:

    • 不,我使用的是 yii2 高级版
    • 然后相应地将您的命名空间从@arunwebber 从app 更改为common,我认为应该不会那么难,但无论如何我已经更新了上面的代码。
    • 我在这里得到空值 $response = \yii\helpers\Json::decode( Yii::$app->PayPalRestApi->processPayment($params)); var_dump($response);
    • 奇怪的是,我们没有更改 processPayment() 中的任何内容
    • forum.yiiframework.com/t/yii2-paypal-integration/126032 也可以看到这个。此外,如果我直接从供应商处使用模块,它也会向我显示相同的 Aka,显示 null,而 return 显示 DOM 上的 json
    猜你喜欢
    • 2012-01-04
    • 2021-07-10
    • 2012-07-23
    • 1970-01-01
    • 2023-03-06
    • 2016-05-18
    • 2016-02-02
    • 1970-01-01
    • 2012-11-05
    相关资源
    最近更新 更多